Skip to navigation

Write a Raspberry Pi game in Python

Posted on 13 Feb 2013 at 10:05

Want to code your own action game for the Raspberry Pi? Kevin Partner shows how to create a simple shoot-em-up in Python

If you’re the proud owner of a Raspberry Pi, the visual Scratch language is a great way to start creating your first game. But to unlock more of the power and potential of the hardware, take a look at Python.

Python is the default educational programming language for the Raspberry Pi. It’s used around the world as an introductory language for many platforms, thanks to its uncluttered style and easy-to-understand syntax. It isn’t only for beginners, though: according to the TIOBE Programming Community index, Python is the eighth most popular programming language among professional engineers, ahead of Perl, JavaScript and Visual Basic .NET. It’s also free, so it’s easy to see why so many people choose Python as their first scripting language.

In this feature we’ll introduce the key concepts of Python, and show you how to get started with the language by walking you through a sample game written in Python. Then it’s over to you to create your own masterpiece.

Getting to know Python

There are two major versions of Python in use. Python 3 is the future, but Python 2 enjoys wider compatibility with existing resources, and it’s this version that’s currently bundled with Raspberry Pi. For this tutorial, we’ll use Python 2.7, the final stable release in the 2.x series: this means we can use the majority of online examples and pre-written code unchanged. When the time comes, moving up to version 3 will be straightforward.

When you look at Python code, the first thing you may notice is how neat it looks. Those used to PHP, JavaScript or other modern languages will also spot the absence of curly braces, as conventionally used to enclose sections of code. This is because Python uses indentation to organise code. This is good practice in most languages, because it makes code more readable: in Python it’s mandatory. If you don’t get your spacing right, your code won’t work. While this can be a shift for experienced programmers, it becomes natural quickly and leads to clear, intelligible code by default.

Python also makes a good first language because it represents a simple implementation of object-oriented programming – a concept any aspiring coder must understand – and is relaxed about how variables are created and managed. The net result is a particularly productive programming environment.

Python code is usually run through an interpreter rather than being compiled, but despite this, end results are pretty fast. Python applications can run on all major operating systems, so it’s possible to create a Python game on a Windows PC and have it run on a Mac or Linux box – or, indeed, a Raspberry Pi.

The Pygame module includes all sorts of helpful functions and methods for creating action games in Python

A final great strength of Python is the availability of a wide range of add-on modules that bring extra functionality. One such module is Pygame. As its name suggests, Pygame helps you create games using Python: for example, it greatly improves image handling through its Sprite class, makes it easy to control in-game audio, and even allows your game to work with joysticks. Games such as Call of Duty might be beyond its capabilities, but an Angry Birds clone certainly isn’t.

Native and cross-platform development

If you’re running the recommended Debian Squeeze Linux distribution on your Raspberry Pi, Python and Pygame come preinstalled, so you can start programming right away. You can write Python scripts in a text editor, but we suggest you take advantage of the free Geany integrated development environment (IDE) that’s also installed as part of the operating system. This software offers numerous helpful features for programmers, such as automatic syntax colouring, to make your code simpler to read and debug, and a symbol browser to help keep track of all the classes and variables in your code.

1 2
Subscribe to PC Pro magazine. We'll give you 3 issues for £1 plus a free gift - click here
Be the first to comment this article

You need to Login or Register to comment.


For more details about purchasing this feature and/or images for editorial usage, please contact Jasmine Samra on


Most Commented Features
Latest News StoriesSubscribe to our RSS Feeds
Latest Blog Posts Subscribe to our RSS Feeds
Latest ReviewsSubscribe to our RSS Feeds
Latest Real World Computing


Sponsored Links

Your email:

Your password:

remember me


Hitwise Top 10 Website 2010

PCPro-Computing in the Real World Printed from

Register to receive our regular email newsletter at

The newsletter contains links to our latest PC news, product reviews, features and how-to guides, plus special offers and competitions.