You might be new to Python or have been using its advanced features for some time. So to answer the question, yes you can create lots of games in Python and it has support for multiple games engines. Python native game engines include Pygame, Arcade, Kivy, ursina, adventurelib, Ren’Py, Wasabi 2D, cocos2d, Panda 3D, Ursina, PursuedPyBear, I think you get the idea that there are plenty.
Additionally other games engines also support Python but not natively, for example Godot [https://github.com/touilleMan/godot-python] has a library which allows you to develop in Python unofficially. We could suggest that if you want to develop specifically using Python then you are best to pick one of the games engines listed above.
As there are so many good Python games engines out there then we could give any a go but we are going to focus on Pygame. Pygame is a set of Python modules with a sole purpose of creating media rich games and multimedia experiences. It has been built on top of the SDL (simple directmedia layer) development library and has add several additional features.
You have low level access to audio, mouse, keyboard and graphics acceleration via OpenGL and Direct3D. You can use multi-core CPU functionality; it is highly portable, and you can get up and running quickly. This means the Pygame engine is great for beginners but also packs in enough advance features to appeal to the more seasoned developer.
Pygame if free and open source under the LGPL license so there is no cost involved in its use and you do not need to attribute back to the engine.
Pygame like most game engines uses a game loop and classes to represent items to handle actions with. For example, you will likely have a player class to implement your player which will want to handle drawing your player graphic to the screen, keeping track of health, knowing what animations to play when moving, handling input for the character movement. You would then potentially have another class such as enemy to handle enemy movement, health, attack systems etc.
Most of your game functionality will live within the game loop. Simply put, a game loop is a simple loop (for example a while loop) which constantly keeps going for the lifetime of your game. Every time it loops round it then calls out to other objects to handle their actions. For example, during your game loop you will let your enemy move along the screen and need to redraw its location. Pygame has many modules which you can pick and chose from. The ones you are most likely to use are:
- Context, which gives you information about the system.
- Display, which is used to draw your game to the screen.
- Event, which can be used to interact with events and queues.
- Image, which is useful for saving and loading games.
- Key, can be used for keyboard interaction.
- Math, for your game’s math calculations.
- Mixer, for playing music and sounds.
- Mouse, well to work with the mouse.
- Sprite, this contains basic game object classes.
- Time, which is useful for monitoring time such as calculating delta.
- Transform, which can be used to transform objects drawn to the screen.
There are also several specialist modules for some additional features which we won’t go into detail here, for a full list then you can take a look at the full documentation found here.
If you are setup with Python on your PC then it is very simple to start working with Pygame. It is a standard pip install to grab the package and start. If you haven’t used Python before and don’t have everything setup and installed on your PC then head over to our post… Once you have gone through the setup then head back to continue your journey.
We will be using Visual Studio Code to install Pygame, but you can do it in a console of your choice. So to install first open Visual Studio Code and then we need to fire up a terminal. From the menu along the top select Terminal > New Terminal. This will then create a new terminal at the bottom of the screen. Once it is ready then enter the following code and press enter:
python -m pip install pygame
You’ll now see it download pygame and install.
That’s it, Pygame is now installed and ready to go.
There are a lot of tutorials out there using Pygame and a large community. So, if you have any issues, you aren’t far away from a friendly answer. One of the best beginner tutorials we have found is on realpython.com. It is an extensive tutorial which takes you through building a full flying game with enemies. It covers several key areas such as sprites, events, collisions, speed/timing, sound, and input.
Let’s look at what Pygame doesn’t provide and some of its possible limitations. Firstly, Pygame requires Python to be installed on the end users PC. That said, you can use something like PyInstaller which allows you to bundle everything into one application.
It can also be a bit of a challenge with any Python applications to obfuscate your code. The process of obfuscation allows you to hide away your code to keep it safe. If you have created an awesome game, it is unlikely that you want to give away all of your code to the rest of the world. There are a few options to obfuscate Python using packages such as pyarmor [https://pypi.org/project/pyarmor/] which do a reasonable job.
Unlike some of the other games engines we have reviewed there is no IDE for Pygame as it is just built up as libraries on top of Python. With Python being quick to run then you can quickly make changes and reload your game (with Python you don’t need to compile) so it might not be such an issue.
Overall Pygame is a simple yet powerful games engine and is a great starter for anyone who knows Python who wants to look into games development.