Balloon Ninja: A Simple Pygame Tutorial

Introduction

Balloon Ninja, before kittens have been introduced.

Balloon Ninja, before kittens have been introduced.

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.

Tutorial Contents

  • 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.
  • Kittens!
    • 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

Advertisements

About ehmatthes

Teacher, hacker, new dad, outdoor guy
This entry was posted in programming and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s