Game development is easy with Pygame Zero

Programming can be a daunting prospect for anyone immersing themselves in the world of game development.

Python is often considered a great coding gateway because this language prioritizes readability. If you know Python, you may have already noticed how close the language is to English.

Pygame Zero is a great starting point for new Python programmers who are more interested in creating than spending time writing boilerplate functions.

What is Pygame Zero?

Pygame logo
Image credit: TheCorruptor/Pygame

Pygame Zero was created to provide an easier game coding experience for new Python programmers.

Daniel Pope (“Lord Mauve” on GitHub) set out to create a barrier-free entry for Python beginners. Pygame zero removes the need to recreate the most basic common code so you can focus on creating amazing results.

Using Pygame Zero, you can experiment with image sprites and your game design, without worrying too much about low-level details.

Install Pygame Zero

Newer Linux distributions and macOS versions come with Python included. Depending on how often you run your distro’s update tool, you may end up with an outdated version of Python.

First, confirm the version of your Python installation:


If Python 3 is installed, this command will display the exact version on the command line:

check python3 version in linux terminal

Next, install PIP (the Python package installer) using one of these commands or another PIP installation method:

apt install python3-pip  
dnf install python3-pip

Once PIP is installed, proceed to install Pygame Zero:

pip3 install pgzero

The process will look like this:

install pygame zero in terminal

With Pygame Zero installed, you can start making games. For example, check out this tribute to the NES classic, Excite Bike, using Pygame modules.

Are you ready to create your first game?

Build with Pygame Zero

There are a bunch of great tutorials, repositories, and sample games ready for you to learn or develop.

The Pygame GitHub repository has some great examples of retro games to get you started. Grab a classic like Pong, Tetris, Mines, or Tron to revel in some quick nostalgia and learn how to work with Pygame Zero.

To get started with an introduction to Pygame Zero, download this minimal project from GitHub. The Python code is well documented and the file explains the dependencies you will need.

Take a look at and you’ll see some common functions that will help you get familiar with the Pygame Zero feature.

Overview of functions

You can load a sprite by passing the base part of its filename to the Actor class. For example, to load the images/alien.png case:

alien = Actor('alien')

The draw() function tells the computer what to draw on the screen. With your sprites, sounds, and code in the correct directory structure, you can avoid bothering with extra syntax. For example, you can display the alien sprite as follows:

def draw():
Code output showing alien sprite on screen

You can use the update() function to move sprites:

def update():
alien.left += 2

if alien.left > WIDTH:
alien.right = 0

Pygame will call the update() function on each image. This code tells the computer to move the alien sprite to the right until it exits the edge of the screen. At this point, it resets the position to the far left. The process repeats until you close the window or press CTRL+C in the terminal to end the program.

You can add a fun element using the on_mouse_down() function. Use this function to check if there is a mouse click at the same position as the alien sprite. Print out an appropriate answer and change the alien sprite to show the alien_hurt.png image.

You now have a simple, yet functional game that hits the mark. Run the code with the command pgzrun Don’t forget to click on your alien sprite to get an answer:

Run the command, code with comments and Pygame in action

You can consult the built-in Pygame Zero object documentation to understand the basic concepts of Pygame Zero.

Creative Inspiration

At first, there can be so many possibilities that it’s hard to know where to start. Here are some ideas to get those creative juices flowing!

treasure hunt

Check out this quick demo of a two-player side-scrolling game with stick figures.

2D platformer with sophisticated features

This in-depth tutorial takes about two hours. However, it’s a great educational guide when you’re ready to go beyond the basics of game programming.

Challenge yourself with game development

To develop your Python skills, try programming basic games with Pygame Zero.

There are many great resources in the Pygame documentation, YouTube tutorials, and easy-to-use guides to help you on your Pygame coding journey.

After spending time honing your Python and Pygame skills, consider contributing to the open source project itself. You can find details in the Pygame Zero GitHub repository.

Source link

Comments are closed.