This article is about Game Development
How to Develop Your Own Game Using Python
By NIIT Editorial
Published on 15/06/2021
10 minutes
Python is the most flexible programming language. This makes it a favorable programming language for each and every field including Web-Improvement, Machine Learning, Artificial Intelligence, GUI Application, and Game Development.
Python gives an in-built library called pygame, which is used to foster the game. When we comprehend the fundamental ideas of Python programming, we can utilize the pygame library to make games with alluring designs, appropriate animation, and impressive sound. To learn about game development using python you can check out NIIT game developer course.
Getting Started: Background and Setup
The “pygame” is a Python covering for the SDL library, representing the Simple Direct Media Layer. SDL gives cross-stage admittance to your framework's basic interactive media equipment parts, like sound, video, mouse, console, and joystick. Pygame came to life as a trade for the slowed-down PySDL project. The cross-stage nature of both SDL and pygame implies you can compose games and rich mixed media Python programs for each stage in the platform that upholds them.
To install the pygame in the platform you have, use the pip command:
To verify if the pygame has been installed in your platform, load one of the samples that come with the library:
Concepts of PyGame
As pygame and the SDL library are convenient across various stages and gadgets, the two of them need to characterise and work with deliberations for different hardware realities. Understanding those ideas and deliberations will help you plan and foster your own games.
- Modules and Initialization
The pygame library is composed out of various Python constructs, which incorporate a few distinct modules. These modules give abstract admittance to explicit hardware on your framework, just as uniform strategies to work with that hardware. For instance, display permits uniform admittance to your video display, while joystick permits unique control of your joystick.
After successfully importing the pygame library in the model above, the initial thing you did was introduce PyGame using pygame.init(). Now the function calls separate init() elements of all the included pygame modules. Since these modules are reflections for specific hardware, this initial step is required so you can work with similar code on Linux, Windows, and Mac.
- Surfaces and Displays
In addition to the modules, pygame likewise incorporates a few Python classes, which embody non-hardware and dependent concepts. One of which is the Surface that, at its basic, characterizes a rectangular region on which you can draw. Surface articles are utilized in numerous settings in pygame. Later you'll perceive how to stack a picture into a Surface and show it on the screen. To understand it better you can try to take up some game making courses.
In pygame, everything is seen on a single user-created display, which can be a window or a whole screen. The display is made utilizing .set_mode(), which returns a Surface addressing the visible part of the window. It is this Surface that you pass into functions of drawing like pygame.draw.circle(), and the content of that Surface is pushed to the display when you call the pygame.display.flip().
- Rects and Images
Your fundamental pygame program draws a shape directly onto the display's surface. However, you can also work with pictures on the disk. The image module permits you to load and save the images in an assortment of well-known configurations. The images are stacked into Surface objects, which can be manipulated and then displayed in various ways.
As referenced above, Surface objects are addressed by rectangles, as are other different objects in pygame, like windows and images. Rectangles are so intensely utilized that there is an uncommon Rect class just to deal with them. You'll utilize Rect images and objects in your game to draw enemies and players and to oversee impacts between them.
Initializing and Importing PyGame
After you import pygame, you'll likewise have to introduce it. This permits pygame to associate its deliberations to your particular hardware:
The pygame library characterizes numerous things other than modules and classes. It additionally characterizes some local constants for things like keystrokes, mouse movements, and show credits. You reference these constants utilizing the syntax pygame.<CONSTANT>. Importing particular constants from pygame.locals, you can use <CONSTANT> instead. This will save you a few keystrokes and improve the overall readability.
Display setting
You make the display screen to use by calling pygame.display.set_mode() and passing a list with the ideal width and height.
For this situation, the window is 800x600, as characterized by the constants SCREEN_WIDTH and SCREEN_HEIGHT on lines 20 and 21. This returns a Surface that addresses within measurements of the window. This is the segment of the window you can handle, while the OS controls the window boundaries and title bar. NIIT game development courses, provide proper know-how on how to utilize display settings optimally.
Events
Key presses, mouse movements, and even movements of the joystick are a portion of the manners by which a client can give input. All client input brings about an occasion being produced. You can access the rundown of all active events in the queue by calling pygame.event.get().
Use of .blit() / .flip() and Drawing on the Screen
The term blit represents Block Transfer, and .blit() is how you copy the content of one Surface to another.
The call to pygame.display.flip() after the call to blit() updates the whole screen with all that has been drawn since the last flip. Without the call to .flip(), nothing is shown.
Sprites
In terms of programming, Sprite is a 2D portrayal of something on the screen. Basically, it's an image. Pygame gives a Sprite class, which is intended to hold one or a few graphical portrayals of any game item that you need to show on the screen.
- Players
Use the Sprite object in the game to define the player.
(Insert the code after line 18)
- User Input
Pygame additionally gives pygame.event.get_pressed(), which returns a reference containing all the current KEYDOWN events in the queue.
Now call .update() each casing to move the player sprite in response to the keypresses. Add this call just after the call to .get_pressed() :
- Enemies
Use the previously used techniques to create a basic enemy class. Before that, import a random library:
Now following the pattern of the Player create a new sprite class called Enemy:
Sprite Groups and Custom Events
Another very helpful class that pygame gives is the Sprite Group. This is an object that holds a gathering of Sprite objects.
Since you have an all_sprites group, you can change how articles are drawn. Rather than calling .blit() on Player, you can repeat over everything in all_sprites :
The plan calls for enemies to show up at regular intervals. This implies that at set intervals, you need to complete two things:
- Make a new Enemy.
- Add it to all_sprites and enemies.
Insert the new event into the queue at regular intervals throughout the game:
Collision Detection
For collision detection, use the method called .spritecollideany(), which accepts a Sprite and a Group as parameters.
Game Speed
The module time contains a Clock. Utilizing Clock to build up a playable frame rate requires only two lines of code. The first make another Clock before the game loop starts:
The subsequent calls .tick() to illuminate pygame that the program has arrived at the end of the frame:
Sound Effects
Pygame gives a mixer to deal with all sound-related activities. You'll utilize this module's classes and strategies to give background sound and audio cues for different actions.
pygame.mixer.init() acknowledges various contentions; however, the defaults work fine in some cases. Note that if you need to change the defaults, you need to call pygame.mixer.init() prior to calling pygame.init(). Otherwise, the defaults are going to be in effect regardless of your changes.
After the initialization of the system, you can get your sounds arrangement:
Now you have to add a call .play() to add sound to the player’s movements:
Now you play the sound when collisions are detected between the player and an enemy:
When the game is finished, all sounds should stop because of a crash or the client exits manually. To do this, add the accompanying lines toward the finish of the program after the loop:
Conclusion
Throughout this article, you have learnt how the game programming with pygame differs
from the basic procedural game programming.
Apart from this, you have also learnt how to:
● Implement different event loops
● Draw the items on the screen
● Adding sound effects and music
● To handle user input
And to do this, you’ve used a subset of the pygame modules, incorporating the display,
mixer and music, time, image, event, as well as key modules. You have also used various
pygame classes, including Rect, Sprite, Sound, and Surface. Game programming courses can help you excel at game development and allow you to develop your own games.
Game Development Course
Becoming a Game Developer: A Course for Programmers. Design and Build Your Own Game using Unity.
Job Placement Assistance
Course duration: 20 weeks