A Self-help Guide to Learn Pgzero and Develop Python Games

If you have read my previous post PyGame is too complicated? Try PgZero, you might have an impression that Pgzero is much simpler to be used than PyGame. On the other hand, you might think it could only develop some simple games. Actually, using proper tools, we could use Pgzero to develop any games which you could implement with Scratch, that is why Pyzero call itself a migration from Scratch programming to code-based programming.

Compared to PyGame, PgZero is easier to understand. In this post, I would like to provide the steps of learning PgZero by yourself.

PgZero website

PgZero is based on PyGame and wraps and combines many functionalities of PyGame, so that the whole game structure becomes easier to use for beginners. Pgzero has its own official website which contains enough information for you to learn it thoroughly.

This is the website link: https://pygame-zero.readthedocs.io/en/stable/index.html. The home page is shown like below image.

On the left side of the webpage, you could see a navigation bar. Among them, “Installing Pygame Zero” topic introduces how to install Pgzero module on your computer. Since Pgzero is based on Pygame library, when installing Pgzero, a default Pygame library is installed automatically under your current active Python package.

“Running Pygame Zero in IDLE and other IDEs” introduces how to run your first Pgzero program. You could use any Python IDE tools, such as PyCharm, Atom, Python’s default IDLE tool, etc.

When running pgzero, we need to “import pgzrun” first (NOT “import pgzero”), and then the last line in your program should be “pgzrun.go()”, as shown in the below code snippet.

Read online tutorials

Once the above Python code snippet runs correctly, you might click “Introduction to Pygame Zero” topic on the Navigation bar to get familiar with Pgzero through hands-on practices. It is a short and sweet introduction, but explains some fundamental concepts of Pgzero, such as:

  • In order to keep the whole program run correctly, you need to keep all the image files within “images” subfolder, and keep all the sound files within “sounds” subfolder. No further sub folder under “images” and “sounds” could be created.
  • All the sprites in the game should be created by using Actor class. You could specify image and position for those actor objects.
  • “Update” and “draw” functions are built-in Pgzero functions. They have special meanings.
  • “WIDTH” and “HEIGHT” constants refer to the width and height of the pyzero game window.
  • “screen” is a built-in object responsible for bliting images and text into display surface of the pgzero game window.

Read built-in objects documentation

Once you are familiar with the above tutorial, it is time for your to dig deeper. Let’s go through the built-in object documentation and event hooks. It is written in a plain language, so easier to understand by beginner.

You could access it on the navigation bar by clicking “Event Hooks” and “Built-in Objects” topics. By reading through the reference documentation, you would understand why Pgzero is designed in this way and how to use the built-in object’s properties and methods properly.

For example, under the topic “Event Hooks”, the document explains the game loop. PgZero will automatically pick up and call event hooks that you define. This approach saves you from having to implement the event loop machinery yourself, which is different from PyGame because in PyGame, we need to define and manage the main loop by ourselves.

Dig deeper by coding projects

If you go through all the above steps, what you need is to learn by writing real projects. Click “Tutorials and Resources” in the Navigation bar, you will see all the available books, online tutorials and magzines.

Among them, what I like most is the “Game Development with Pygame Zero” online tutorial. First of all, it is for free. Second, it offers step-by-step instruction to go through several real projects, as shown below. You could even download its image and sound packs for use in your own projects.

Please note that this online tutorial is located in another website. You could also click here to access it.

This website contains several projects, such as Gem Catcher, Ninja Runner and Tanks. They are arranged in an assending order in terms of difficulty. You could choose to challenge yourself by coding the project “Intermediate Game Development with Pygame Zero”, which offers another project called Vertical Shooter.

Personally, I would strongly recommend you to go through the intermediate level project “Vertical Shooter”. Not only does it offer more useful code, but also introduce two important tools which are especially helpful in developing PgZero games. Let me introduce them here.

Tool #1: Vectr

This tool is used to create and edit vector images. As we know, in Scratch programming, the Scratch project provides costume editor where you could draw your own images. However, for Pgzero or Pygame, we need to draw our images using third party tools. Vectr is an excellent choice. You could follow the illlstration on this page to draw your first vector image.

It is easy to edit an image in Vectr and then export it as PNG file. Take a look at some images I created for my scrolling platformer game. The black background represents the transparent part of the image, so you could export them to PNG format.

Use Pgzhelper.py

This is a wonderful add-on module the author of the website creates for Pgzero. I would say if it could be added to Pgzero module officially, that would enhance the functionality of Actor class dramatically. Aftering downloading and adding this file to your project folder, you could find all the similar functions to the Scratch programming. You could download this file from this page.

After downloading pgzhelper.py, put it in the project folder, as shown below.

Finally, just demonstrate the scrolling platformer game written with Pgzero. Enjoy the coding and have fun!

Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.