If you are learning to program in Python, writing your own game is a great way to immerse yourself in the language. Writing a game pushes you to understand the core parts of the language such as data structures and program flow, but also moves you quickly into using existing libraries. This tutorial walks you through the creation of a simple game, Balloon Ninja, using the Pygame framework.
I have watched a number of new programmers try to follow along with a variety of tutorials, and this has influenced the way I currently write tutorials. In particular, I have observed that students tend to get confused by seeing code “snippets”. If you are an experienced programmer and you see a code snippet showing just the changes to a previously-mentioned file, you can probably make quick sense of how the fragment fits into the larger file. But new programmers don’t keep the context of a larger file in their heads very well. So every time a file is modified, I list the whole file, with the modified sections highlighted. It makes for longer pages, but screen space is cheap. The clarity for new programmers seems worthwhile.
The tutorial is written on an Ubuntu system, but if you have Python installed on another OS you can probably follow right along.
- Creating a screen
- Create an empty screen, and give it a background color.
- Releasing balloons
- Release a single balloon, and then release a series of balloons.
- Popping balloons
- Respond to user input
- Adding a scoreboard
- Track game statistics, and show some stats on a game scoreboard.
- Adding game logic
- Add logic to make the game more interesting and challenging.
- Start screen and game over
- The game only starts when you click Play, not when you run the program. The game ends when you miss too many balloons.
- A better scoring system
- Points increase throughout the game, and the score is reported a little more cleanly.
- So far, you can slash away at anything on the screen. Now there are kittens, which you must avoid hitting.
- Wrapping up
- Posting to GitHub, and additional features that could be implemented.
Trying Balloon Ninja
The “Wrapping Up” post includes instructions for downloading the game from GitHub, and running the game from your local installation. Feel free to run the game and poke around the code a bit before digging into the tutorial.
Next: Creating a screen