Pygame: Can you make an industry-standard game?
Tags: pygame, python, gamedev, programming
Published: 2022-09-02 18:36:48

## Pygame: Can you make an industry-standard game?

<br>

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqozr5v1927llnydxrwh.png" loading="lazy" width=300px />

<i>Written on May 10th, 2022</i>

<br>

## What is Pygame?
[Pygame](https://www.pygame.org/docs/) 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](https://www.libsdl.org/) (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](https://en.wikipedia.org/wiki/Shoot_%27em_up) style platformer from planning to release.

<br>

<div class="display: flex; justify-content:space-between;" style="width:100%;">

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e238a31j60vqb106fvlr.jpg" loading="lazy" height=200px />

</div>
<i>Example of bullet hell game (Enter the Gungeon)</i>

<br>

## 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.

<br>
<div class="display: flex; justify-content:space-between;" style="width:100%;">

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgq59mmf8ldenh426s6h.jpg" loading="lazy" height=200px />

</div>
<i>Example of Pygame created game (Shifting Edge)</i>

<br>

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 <i>Global Interpreter Lock</i>, 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).

<br>

## 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.

<br>

## 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.

<br>

## 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:

* [DaFluffyPotato](https://www.youtube.com/c/DaFluffyPotato)
* [ClearCode](https://www.youtube.com/c/ClearCode)

For other resources / documentation, the following may be useful to you:

* [Python documentation](https://docs.python.org/3/)
* [Python list comprehension](https://www.w3schools.com/python/python_lists_comprehension.asp)
* [GIL explanation](https://wiki.python.org/moin/GlobalInterpreterLock)
* [Pygame documentation](https://www.pygame.org/docs/)
* [PyInstaller documentation](https://pyinstaller.org/en/stable/)
* [Game Library vs Framework vs Engine](https://gamefromscratch.com/gamedev-glossary-library-vs-framework-vs-engine/)
* [SDL](https://www.libsdl.org/)