Python in Action: Game Development (Yes, It’s Possible!)
Try these libraries and other Python resources to start writing games of your own today.
Hey Grokking Python readers!
Coding is a creative endeavor. It’s about innovating, telling stories, and developing novel solutions to complex problems. The desire to create something new is a driving motivation for countless programmers, and game development can be a great way to exercise that creativity. Video gaming also happens to be a multibillion-dollar industry that employs hundreds of thousands of people. It’s no surprise, therefore, that so many people learn to code because they want to make video games.
To start exploring game development, you’ll need to know what tools to use. Python isn’t necessarily the most popular language in this field. There’s a strong case to be made for using languages with low-level components like C++ for game development instead. Even so, Python plays an important role in the development process at many gaming companies, and there are some high-profile games written primarily in Python, including Eve Online, Civilization IV, and Frets on Fire. There are also tons of Python-based tools that you can use to start writing games of your own. So, if you’re a Pythonista thinking about getting into game development, don’t worry—it’s very possible, and we’ll discuss how to get started in this edition.
A Note About Performance
One of the most common criticisms of Python is that it is a slow language. This is debatable. Writing Python code is generally a quick process, and there are ways of accelerating its runtime to make it more competitive with faster languages. But there’s some truth to it. Python’s relatively slow runtime in its standard implementation is due to the fact that it’s a high-level language. Python has built-in functionalities that reduce the amount of code that programmers need to write. This makes writing the code easier, but it also means that executing the code takes longer than it does for a language with low-level components like C++.
This is a factor that game developers need to consider when choosing a programming language. Optimizing performance is important.
How You Can Use Python for Game Development
Python might not be every game developer’s language of choice, but it still has a lot to offer in the field. Let’s look at three reasons to consider using Python for game development.
There are tons of Python libraries that support—or are designed for—game development. In addition to this ready-made ecosystem of tools, Python is also easy to learn, and it has debugging capabilities that help users detect errors in their code. This makes Python a great place to start for people who are interested in designing games but don’t have an abundance of coding experience.
Many major gaming studios use Python for prototyping, meaning that they’ll write an early version of a game in Python before ultimately rewriting it in a faster language. This is a way of maximizing the strengths of the various languages employed. The developers take advantage of Python’s ease-of-use by using it to workshop the game in the early stages of its development, but then they translate the code into a faster language for the final version to ensure the highest level of performance possible.
Python’s popularity as a prototyping language doesn’t mean that it’s only used for that purpose. As noted earlier, there are some popular games that run largely on Python code. So, it’s very possible to build a successful, high-performing game in Python.
Python Libraries for Game Development
There are quite a few Python libraries for game development. Here are a few to explore:
Pygame is one of the most popular Python game development libraries. It uses C and Assembly for many of its core functions, making it faster than standard Python. It’s also highly portable and can be run on multiple operating systems.
Pyglet can be used for developing games and other applications on a range of operating systems. Pyglet is written entirely in Python, simplifying the development process for coders who aren’t familiar with other languages.
PyOpenGL is a Python wrapper for the OpenGL graphics library.
Pykira is a Python-based game development framework.
Panda3D specializes in developing 3D games, simulations, and graphics.
Kivy and tkinter are also worth exploring even though they’re not gaming-specific packages. Kivy is an open-source library that can be used to make applications with elaborate user interfaces (including games). Tkinter is a Python wrapper for the Tcl/Tk GUI toolkit. It makes Tcl/Tk’s powerful application development capabilities accessible through Python.
Getting Started
There are plenty of Python tools that can help you get started designing your own games. Many of the libraries listed earlier have user guides designed to help familiarize you with the library’s tools. Here are a few of those resources:
There are also tons of tutorials available online that teach you how to make simple games. You can use these tutorials to learn more about Python’s game development capabilities, or just to hone your Python skills in a fun, project-based setting.
Is there a Python game development resource you’ve found to be especially useful? Let us know in the comments or by replying to this email.
As always, happy learning!