Pygame: Can you make an industry-standard game?
Written on May 10th, 2022
What is Pygame?
Pygame is a game framework available for use in Python3. You may notice that I called it a framework, not a game engine. Pygame is a set of modules that act as a wrapper around SDL (Simple DirectMedia Layer). This differs from a Game Engine which generally provides a GUI, pre-built game resources, etc. for the user.
As a game framework for Python, Pygame makes window creation, sprite management, controls mapping, and more much easier for the user. I will have a series of development blogs posted on this site to show how to take advantage of Pygame's strengths to develop a bullet-hell style platformer from planning to release.
Example of bullet hell game (Enter the Gungeon)
Let's talk Python
Python is known as either a godsend or slow language depending on the software developer asked. In the end, it is a tool that is amazing at some tasks and lackluster in others just like any other language. Python is generally not the first choice when creating a video game. However, with today's common computing power and C-based modules like Pygame available, Python can be plenty capable when it comes to creating a 2-dimensional game.
Example of Pygame created game (Shifting Edge)
Games with less assets on the screen work best for Python and Pygame. A bullet-hell genre game breaks this "light rule" since by definition it contains many assets (bullets and enemies) on screen at once. This leads to an interesting project / challenge since we will need to be careful to design a well-optimized entity manager and particle effects (which will be talked about more in the next section).
Python is also plagued with the GIL, or Global Interpreter Lock, which limits the number of cores useable by a Python application to 1. Therefore while multi-threading can still function (all on a single core), multi-processing is not possible in Python without having multiple applications running with the multiprocessing library, which is out of scope of this dev blog. Pygame gets around this problem by 'unlocking' the GIL - something that is only possible using the C language in modules.
So then why will we be using Python for this game? Well, Python is still a strong and mature language with many pros to it. Python has been around since 1991, and in that time a tremendous community has grown that has built many modules and documentation - making Python extremely powerful. Python's relative 'slowness' as a language also stems from pros of the language: being easy to write (no pointers / low level memory management), no compiling required (due to being an interpreted language), and ease of startup (from starting a project to having a running version).
Understanding of basic optimizing
We have discussed some high-level cons of using Python, so how will we get around it?
Let's start with the GIL. As mentioned above, this won't be an issue since Pygame actually gets around this. Pygame is also designed to be used without any threading by the user, so as far as thread/core management needed by us - there is none!
Next we have the relative slowness of Python. Our game will be doing some demanding stuff for an application: calculating, rendering, and modifying many objects each frame while aiming to maintain a 60 fps (frames per second) minimum on an average computer. Part of the solution, as mentioned above, is Pygame. But Pygame doesn't solve the rendering of text quickly, particle and entity management, etc. for us. For this, we will:
- build a custom entity manager
- build a custom font / text renderer
- build a custom particle system
- build any C modules as needed
This is by no means the only custom or optimized pieces we will create, but it will be the most important when it comes to performance.
A custom entity manager will allow us to choose what is being rendered / updated to the screen and how often that occurs. It will also allow us to use Python's strong list / dict comprehension to maintain integrity of the data while still doing all we need to do.
A custom font / text renderer is needed because (this may come as a surprise to many) text rendering is one of the tougher / time consuming processes for a game. Pygame has a text system built in to present text to the screen, but this requires the full string texture to be updated and re-rendered each update to the text. Instead, we will build a system that only creates the textures for rendering once for each symbol and will then blip this to the screen to create text. Since creating those textures is a heavy process, this will greatly increase the capabilities of our game.
Pygame doesn't have a particle system, so we need to create our own. Creation of any large number of entities brings the potential for poorly written code to slow down the game. Thankfully, we will be able to create a particle system that looks nice, functions according to the requirements we set, and is optimized enough to be used in our game.
Lastly, I suspect we can write 100% of our game in Python. However, if we run into some calculation or function that is unable to meet our speed goals, we can always create a C module. Even if this is not needed in the end, I will still do a blog post on how to create a C module for those that are interested.
What can we build to?
Python runs on nearly every OS and device. However, when it comes to making a game, we are limited to what we can distribute to due to limits of PyInstaller, device specifications, etc. Therefore, our goal with this game will be to have a distributable application available for Windows, MacOS, and Ubuntu Linux.
For any other OS or device someone would want to build to, we will have the source code available.
Leaders in Pygame development / Resources
Pygame has been around since the year 2000, so there are plenty of games and applications written with Pygame that you can look through and follow. However, when it comes to people pushing the limits of Pygame and creating well-written applications, the main two I like to follow would be:
For other resources / documentation, the following may be useful to you:
Top comments (1)
This was a nice explanation to pygame,
I will request you to check out my devlog as well.