One of the most common reasons that our students enroll in our coding classes for kids is because they are interested in game development. And why not? The gaming industry as a whole was valued at more than 150 billion dollars in 2020, according to a report from Newzoo, and that number is only going to increase over time. Without a doubt, the gaming industry has viable job opportunities for teens looking to get into coding.
But games can be coded in a ton of different programming languages, which can make it difficult to know which one to start with.
With its easy-to-read syntax, a variety of useful code libraries and its use in a number of popular games, Python is a great starting point for coding a game.
Is Python Good for Game Development?
So, let’s get down to the heart of the subject!
Is Python even any good for game development? For beginner game developers, Python is a great coding language to start with for a few reasons:
- Easy to understand syntax
- Ability to reuse code
- Easy to debug
While large game studios use languages that offer more speed and allow for better graphics, Python has a lot going for it that makes it an especially useful language for game development. Python is very commonly used by large game studios for game prototyping, which means the original version of some of your favorite games may very well have been written in Python before getting re-made in a faster language.
In addition, large games may supplement their codebase with Python for specific components of their game. Battlefield 2 uses Python for the implementation of scorekeeping and team balancing while Civilization 4 has its inner logic and AI written using Python.
Interestingly, the same properties that make Python a great starting point for the pros are what make it such a great language for beginners taking their first steps into game-making.
Easy to Understand Syntax
Possibly the best reason for developing games using Python is the relative ease of the language because it is written much like English.
Python is one of the simplest coding languages to write and to read, making it a great starting point for all sorts of coding activities. Having an easy to write language is super helpful for tasks where the speed of development matters.
For large game studios, this means being able to quickly make a prototype of a game to see if it’s worth developing further. It saves a ton of time compared to starting in a language like C++, which means that that time (and money) can be used where it matters.
For beginning coders, development speed can matter even more. Using a simpler language to make a game can be the difference between development taking days or taking weeks. Especially when learning, being able to see results quickly can be just the encouragement needed to keep going!
Ability to Reuse Code
Another major point in favor of writing a game in Python is its ability to reuse large chunks of code.
Python is an object-oriented coding language, which means that it can take a piece of prepared code and use it wherever it’s needed. This is incredibly useful and results in many fewer lines of code in the finished game. Fewer lines of code means less time re-writing the same code throughout the project and less time spent in development overall.
More importantly, it also gives you the ability to use code written by other developers through the use of Python’s extremely large ecosystem of code libraries.
Lots of different coding languages are object-oriented. However, the depth and breadth of Python’s libraries mean that for any problem you’re trying to solve, there’s a great chance that someone else has already written code that can handle it!
This is great not only for lowering the overall amount of work going into the game but also for learning the skills needed to deal with all sorts of problems.
Easy to Debug
A third major reason for using Python for game development is how it makes the debugging process easier. Debugging is the process of looking for and fixing errors in your code.
Since Python is already one of the easier coding languages to read and write, it makes sense that it would be simpler to debug than other languages, but there’s more to it than just that.
Python is what is called an interpreted language. That means that instead of being compiled before being run, the code is executed directly. There are pros and cons to that, but one of the major benefits is that an error in the code will stop the code’s execution and provide a single error message.
Being able to focus on a single error, solve it, and move on is a huge help in keeping debugging manageable.
Recommended: Python for Kids
Popular Games Made with Python
Learning why Python is useful for coding games is all well and good, but being able to see how those advantages have been used in the real world is even better.
While tons of studios use Python for prototyping, there are a number of great games that use Python as an integral part of their game. Here are some fan favorites.
EVE Online, for those not familiar, is a space-based MMORPG (massively multiplayer online role-playing game) with an emphasis on the “massive.” Players in the EVE universe can engage in trade-based gameplay like mining, manufacturing, and a player-driven market, or they can focus on exploration, combat, and even piracy!
This game depends heavily on a fork of Python called Stackless Python.
Disney’s ToonTown Online
Disney’s ToonTown Online was developed with Python and uses the Panda3D game engine for its graphics. It’s an MMORPG set in a cartoon-based world where you get to explore and fight. You can tell that it’s aimed at a younger demographic, but it is also a well-made game and a great example of the flexibility possible using Python.
Battlefield 2 is a great example of a game developed by a large gaming company that is dependent on Python to run. It was a major first-person shooter type game exploring a fictional world war. It’s an older game at this point, and no longer has operational multiplayer servers, but much of the game’s functioning depended on Python code.
Sid Meier’s Civilization IV
The Civilization games are the shining standard for what strategy games should be, and Civilization IV is no exception. These games are chock-full of interesting components, but one of the best parts is the ability to design and play your own maps with friends.
The map editor used to make that experience possible depends on Python.
Frets on Fire
Frets on Fire is an open-source musical video game similar to Guitar Hero. The basic idea is to time specific button presses to the action on the screen to perform music.
This game was written in Python and is an excellent example of what can be done with the language. The game itself is available under the GNU license as free software, and you can look directly at the code used to write it on Sourceforge.
What Are the Best Python Game Engines
Ready to take Python for a spin?
The first step in developing a Python-based game is deciding which game engine you want to use. Game engines provide the software framework to build and run video games and often include development tools as well. There are many different game engines to choose from, so it’s not a simple decision. Since each one does different things better or worse than others, choosing the right engine is a big part of making games with Python.
Here are some of the best options for some of the more common development situations.
PyGame (for 2D games)
PyGame is nearly a one-stop shop for all the resources needed to build a 2D game using Python. It is a low-level graphics library built around the SDL open-source 2D graphics library.
It isn’t the platform you need for making extremely complex, graphics heavy games, but it is an excellent choice for building out a simple Python game.
Panda3D (for 3D games)
Panda3D is the graphics engine used to make Disney’s ToonTown, which is great proof that it’s a solid choice for using Python to make a 3-dimensional game. The core of Panda3D is actually written in C++ in order to boost its speed, but the majority of the APIs can be called through a Python interface so you can feasibly create an entire 3D game using Python coding!
Recommended: Free Coding Programs for Kids
Guide to Creating Your Own Game Using Python
Now that you’ve gained some background knowledge in Python’s capability to create great games, it’s time to think about taking the leap and creating your first game. But how do you start making your dream game from nothing? While this task can seem daunting at first, if you tackle it like a programmer and break it down into multiple steps, you will be surprised at how easy it can be.
While specifics of each step may vary for each game and programmer, general guidelines to use are as follows:
- Brainstorm ideas until you have a type of game in mind.
- Break your big idea up into multiple pieces and tackle each piece one at a time
- Write pseudocode for each piece
- Add assets to your game such as images and music
- Write the logic for each piece until it’s working as intended
- Test and refactor as needed
- Deploy your game
To show you how these steps work, we’ve created a Traffic vs. Zombie game. https://mediap.tr.cwhq-apps.com/traffic_vs_zombie_py
1. Brainstorm ideas until you have a type of game in mind
You will need to look at this part as a designer, programmer, and player. How do you want your game to look? How do you want your game to behave? And most importantly, what is the purpose of the game.
While it may be fun to come up with a brand new idea, I find it easier to mix and match games you have played before into your own unique game. My game is called Traffic vs Zombie which is a mash-up with Frogger and Plants vs Zombies. With this idea, I can answer the three questions listed above. I want my game to have one zombie and three roads with cars. The cars will drive along the roads blocking the zombie from a finish line. The player controls the zombie and tries to cross the road to get to the finish line.
2. Break your game up into multiple pieces and tackle each piece one at a time
Many games have a lot of different functionalities that can be tackled separately. It’s a good idea to identify these components so they are more manageable when you start working. For example, in Traffic vs Zombie, I broke the overall design into these separate parts.
- Design the main backdrop of the game such as the sky, roads, cars, one zombie, and a finish line.
- Write logic to make the cars move continuously along the roads
- Allow the zombie to move using WASD keys.
- Add collision detection when a zombie touches a car and causes a game over.
- Let the player win the game when the zombie touches the finish line.
- Spice up the game with extra sprites and designs!
3. Write pseudocode for each piece
Pseudocode is the process of writing code down, but in your native language instead of a programming language. For example, instead of writing the python code for making vehicles move, you may write down what you want to happen. Pseudocode for adding a vehicle and making it move across the screen looks something like this:
Add vehicle image
Place vehicle image on the screen
Animate vehicle image from left to right, then right to left
Writing pseudocode will help organize your thoughts and the logic that the game will need, making the programming part much more manageable.
Then when you’re ready to write Python code, you can translate your pseudocode into a language that the computer can understand. The above pseudocode translates to the following in Python:
image = add_image(choice(vehicle_options), size=vehicle_size)
position_element(image, vehicle_x_position, vehicle_y_position)
animate_right(image, 2200, time=randint(10, 12), loop=True)
4. Add assets to your game such as images and music
If you’re a graphic designer and musician as well as a programmer, then go ahead and create everything from scratch. However, if you’re like most programmers, you’ll realize that it helps to re-use stuff created by other people. However, ensure you get permission before doing this in your own games!
Here are examples of assets we are using in our game:
If you’re looking for some royalty-free game assets and sounds, I highly recommend https://www.kenney.nl/assets. Kenney provides a plethora of free assets that you can use freely without payment, permission, or attribution. However, I always recommend at least attributing when possible.
5. Write the logic for each piece until it’s working as intended
By this point, you’re staring at an awesome background of beautiful assets, but nothing is happening yet. This is when you take out the pseudocode you wrote earlier and start programming.
You know what needs to be done, so now use the knowledge you’ve gained with Python and write the game logic.
6. Test and refactor as needed
If you’re like 99.9999% of other programmers out there, the first time you run your game it will not be perfect. That’s normal and it’s okay to be in a perpetual state of wanting to make improvements.
If you’re planning on sharing this game with friends, ask yourself questions such as, is this game too easy or is this game too difficult? How can you balance the strategy of the game to encourage people to play the game without rage quitting (I’m looking at you Dark Souls!) or stopping after 10 seconds because they’ve already beaten your game? A good idea to manage this is to add difficulty levels.
7. Deploy your game
Happy with the final product? Share your game with your family and friends! If the game is created on our CodeWizardsHQ editor, you can just copy and paste the link to anyone and they can play your game for free! If you’d like to check out Traffic vs Zombie you can do so by going to https://mediap.tr.cwhq-apps.com/traffic_vs_zombie_py.
Learn Programming with Python Classes for Kids
Whatever kind of game you’re wanting to build, it’s a lot easier to build it when you’re working with experienced teachers who have already done it themselves. CodeWizardsHQ offers fun and effective Python classes for kids and teens designed to get your Python skills where they need to be to become a real game developer.
CodeWizardHQ’s coding for middle school students and for coding for high school students both start out with the basics, but quickly dive into all you need to know in order to be a great Python developer. Since one of the larger benefits of coding a game in Python is being able to see and use code that has already been written, adding in the chance to learn from our teachers’ previous experience in Python is a great way to increase those benefits!