Link Search Menu Expand Document

PyGame

What is PyGame?

Pygame is a free and open-source cross-platform python module for game development. It is a community project, initially started to replace pySDL in the 2000s. The python game developers utilize these library modules to write multi-media applications containing computer graphics and sound effects.

PyGame in Python

As initially discussed, it is a wrapper for the Simple Direct Media Layer library (SDL lib), which provides the developers with the access to play with their system’s underlying hardware components. The components include video, sound, joystick, keyboard, and mouse. The most significant benefit of using this module is that it allows writing programs and games for multiple platforms that support this module.

Installation and Initialization

The users can install this library using the following pip command:

pip install pygame

Moreover, the users can verify the installation by loading one of the sample games included in these modules.

Following is the command to load the game:

python3 -m pygames.examples.aliens

The above command will open its window if the user has been able to install the pygame successfully. After installation, the users need to simply import this module and initialize it in their python program to use its methods.

Import pygame
pygame.init()

Important Concepts in PyGame

Both PyGame and the SDL library have to work abstractly to support numerous hardware options. Therefore, a thorough understanding of the abstraction concepts proves helpful in working with different hardware.

Modules and Initialization

Several Python constructs in the pygame library consist of various working modules. These modules help in providing access to specific hardware on the user’s system with complete abstraction. Moreover, these modules provide essential uniform methods for smooth working with the specific hardware.

For example, the display and joystick allow video access and complete control of the joystick with abstraction, respectively. The initialization step of PyGame, which is pygame.init(), calls the init() function of all other included modules. This process provides an abstraction to work with Linux, Windows, and Mac.

Shapes and Displays

Pygame modules contain various classes to encapsulate the software concepts such as shapes, and surfaces. The developers can use these classes to draw numerous shapes on the user’s screen. For instance, they can use surface class to draw on a defined rectangular area.

As there should be a single display for the users to view the game, the developers create the display first, which returns a surface to draw on. Then, they can call methods such as pygame.draw.circle() to draw on the display. Additionally, the users can pass various values to the function to control the appearance and position of the display objects on the screen.

Disk Images

Like drawing on the user’s screen, the developers can also display images on the disk. PyGame includes an image module that allows the developers to load and save images in various formats. The developers need to load these images into the surface object in the same way as they load shapes and then manipulate their appearances. Additionally, the rectangles represent the images and windows because rectangles are significantly prevalent in game development. That is why there is also a unique rect class to manage its functionalities.

Sample Code for Basic PyGame Game

Below is the sample code for a simple game developed using pygame:

# importing the pygame module
import pygame as pg
#initializing the imported module
pg.init()
# setting up the surface for drawing
#the users can give customized dimensions for the display screen
surface = pg.display.set_mode([500, 500])
# initializing the variable as true for opened display screen
running = True
while running:
    # filling the background with white color
    surface.fill((255, 255, 255))
    # drawing a circle in the center of the surface
    pg.draw.circle(surface, (0, 0, 255), (250, 250), 75)
    # flipping the display for the user
    pg.display.flip()
    #checking if the screen is still opened or user has pressed the cross button
     for event in pg.event.get():
        if event.type == pygame.QUIT:
            running = False                         #to stop the program’s execution
# Quit the game
pg.quit()

Explanation

The above code will open a small square window of 500 x 500 dimensions and draw a solid blue circle in the center of the display screen. The users can give RGB values to the functions such as surface.fill() to fill the surface with that particular color background.  The game will keep drawing the circle until the user presses the cross button on the screen, which will trigger the quit event and break the while loop. Finally, the program will call the quit method and closes the execution.

Other useful articles:


Back to top

© , Learn Python 101 — All Rights Reserved - Terms of Use - Privacy Policy