Create a Python game for the Raspberry Pi

Once you’ve installed Python (accepting the installation defaults), download Pygame for Windows – again, accept default settings. If you already have a preferred IDE, it will almost certainly support Python “out of the box”; if not, get Geany for Windows. To configure it for testing and running Python scripts, launch Geany, then go to the Build menu and click Set Build Commands. Find the Execute field and replace the current contents with C:Python27python %f (assuming you installed Python to the default directory).

Designing a game

Our first Python project will be a simple shooting game called Raspberry Pie (ahem). In this game, three different types of fruit will fall from the top of the game area, but only the raspberries should reach the bottom and go into the pie. The player controls a movable turret, and wins points by shooting cherries and strawberries: points are deducted if a raspberry is destroyed accidentally, or if one of the other fruits ends up in the pie.

Let’s start by collecting together the assets. The Open Clip Art Library is a great place to find free clip-art; we can get our fruit images from here. We’ll create our own images for the turret and the bullets. We could collect together sounds for our game, too, but for now we’ll keep things simple and silent.

Now let’s think about how we’re going to structure our program. The basic structure of an arcade game is the same whether you’re planning to use Scratch, Python or any other tool. We begin by setting up the execution loop environment, and create the objects that will be used in the game. We then initialise everything – for example, setting the score to zero. Then there’s a main loop that handles the gameplay. Once the game ends, the score is displayed.

Objects

Object-oriented programming (OOP) underpins most modern programming languages, and it isn’t too difficult to understand. Think about the visible objects in our game: the only aspects that vary from one tumbling fruit to another are their position and genus (that is, whether they’re raspberries, strawberries or cherries). We can therefore represent them all very simply by creating a single fruit class with properties that indicate genus and position. We can then create fruits as independent instances of the class (these instances are the “objects” of OOP), each of which contains information about what sort of fruit it is and its location.

Create a Python game for the Raspberry Pi

As well as properties, classes can have methods – built-in actions that we can invoke whenever we want. In the case of fruit, the major thing each object does is fall. So we might build a method into our class that says “take my current vertical position and move it down by 3 pixels”.

Since all the code relating to the fruit sits within the fruit class, bugs are easy to find and, once fixed, apply to all instances across the entire program. You can even base one class on another, giving you a hierarchy of subclasses. For now, however, let’s stick to a simple implementation. Remember that, while classes are written as part of the code, objects are created only when the program runs, and don’t exist beyond the duration of the game.

Creating the code

On the following pages we’ll walk you through the code for Raspberry Pie, so you can see how the program works and type along if you wish. If anything seems unclear, your first stop should be the Python documentation for more information. We’ll also be drawing heavily on the methods and properties provided by Pygame – see documentation here.

You can also download the code file here if you want to play the game yourself right away. In addition, you can use this code as a starting point to learn more by modifying and updating it yourself. Can you add a high-score table? Or make different types of fruit move in different ways? Once you’ve mastered the basics of writing games in Python, you can create your own original game, and send it in for our competition.

Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.

Todays Highlights
How to See Google Search History
how to download photos from google photos