Skip to content

Creating an Engaging Python Game Effortlessly

CodeMDD.io

PyGame: A Primer on Game Programming in Python

By Jon Fincher

When I started learning computer programming late in the last millennium, it was driven by my desire to write computer games. I tried to figure out how to write games in every language and on every platform I learned, including Python. That’s how I discovered pygame and learned how to use it to write games and other graphical programs. At the time, I really wanted a primer on pygame.

Background and Setup

Before we dive into the details of pygame, let’s first understand what it is and how to set it up.

pygame is a Python wrapper for the Simple DirectMedia Layer (SDL) library. SDL provides cross-platform access to your system’s underlying multimedia hardware components, such as sound, video, mouse, keyboard, and joystick. pygame started life as a replacement for the stalled PySDL project. The cross-platform nature of both SDL and pygame means you can write games and rich multimedia Python programs for every platform that supports them!

To install pygame on your platform, use the appropriate pip command:

Terminal window
$ pip install pygame

You can verify the installation by loading one of the examples that comes with the library:

Terminal window
$ python3 -m pygame.examples.aliens

If a game window appears, then pygame is installed properly! If you run into problems during the installation process, check the official pygame documentation for troubleshooting tips.

Basic PyGame Program

Once you have pygame installed, you can start writing your first pygame program. Let’s start with a basic program that draws items on the screen.

import pygame
# Initialize pygame
pygame.init()
# Set up the screen
screen = pygame.display.set_mode((800, 600))
# Game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen
screen.fill((0, 0, 0))
# Draw items on the screen
pygame.draw.circle(screen, (255, 255, 255), (400, 300), 50)
# Update the screen
pygame.display.flip()
# Clean up pygame
pygame.quit()

In this program, we import the pygame module and initialize it using the pygame.init() function. We then set up the screen by creating a Surface object using the pygame.display.set_mode() function. The Surface object represents the window on which we can draw.

Next, we enter the game loop, which runs as long as the running variable is True. Inside the game loop, we handle events using the pygame.event.get() function. This function returns a list of all the events that have occurred since the last time it was called. We check if any of these events are of type pygame.QUIT, which is triggered when the user closes the window. If we detect a quit event, we set running to False and exit the game loop.

We then clear the screen using the screen.fill() function, which fills the entire screen with a specified color. In this case, we fill it with black (0, 0, 0).

Next, we draw an item on the screen using the pygame.draw.circle() function. This function takes the Surface object (screen), the color of the circle (white), and the position and radius of the circle. In this case, we draw a white circle with a radius of 50 at position (400, 300).

Finally, we update the screen using the pygame.display.flip() function, which updates the entire display surface to the screen. This is necessary for the changes we made to the screen to become visible.

Once the game loop is exited, we clean up pygame by calling the pygame.quit() function.

PyGame Concepts

Now that you have a basic understanding of how to create a pygame program, let’s dive deeper into some of the key concepts and features of pygame.

Initialization and Modules

To use pygame in your program, you need to initialize it by calling the pygame.init() function. This initializes all the pygame modules and prepares them for use. After initialization, you can import and use various pygame modules to achieve different functionalities in your game.

Displays and Surfaces

In pygame, the game window is represented by a Surface object. A Surface is a rectangular area in which you can draw, display images, and handle events. You can create a Surface object using the pygame.display.set_mode() function, which takes the width and height of the window as parameters.

Images and Rects

In pygame, images are represented by Surface objects. You can create a Surface object from an image file using the pygame.image.load() function. A Rect object represents the position and size of an image or Surface on the screen. It is useful for handling collision detection and positioning objects.

Conclusion

In this article, we covered the basics of pygame and how to set it up on your platform. We also walked through a basic pygame program that draws items on the screen. By understanding the core concepts of pygame and exploring its features and functionalities, you will be well-equipped to start creating your own games and graphical programs in Python. Happy coding!