Create a Python game for the Raspberry Pi
Posted on 13 Sep 2012 at 09:00
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 take your first steps in programming. But to unlock more of the power and potential of the hardware, take a look at Python.
More Raspberry Pi...Check out our tutorial guide to writing your own Raspberry Pi game using Scratch and submit your game creation for a chance to win £250
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, which you can submit to our Raspberry Pi coding competition.
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.
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.
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.
As we’ve mentioned, however, Python works across many platforms, so you can write your code on a Windows PC and then move it across to the Raspberry Pi when you’re finished. To do this, you’ll need to install Python 2.7 on your PC. Even if you’re running 64-bit Windows, choose the regular Windows installer, not the x86-64 one, since the standard Pygame installation is 32-bit only.
Inspired to add 'lives' to game
This article inspired me to get programming on my Raspberry Pi after not touching code in years! Have made an iteration to the base features adding 'lives' to the turret. You can download this here if your are trusting enough! https://dl.dropbox.com/u/75684057/raspberry-pie.zi
Please feel free to use this if it helps you or leave feedback.
By lancepj on 18 Sep 2012
For more details about purchasing this feature and/or images for editorial usage, please contact Jasmine Samra on firstname.lastname@example.org
- Windows 9: release date, features, free update and cloud version
- iPhone 6 Plus "less likely to bend than HTC One"
- iPhone 6 Plus: Apple's had nine complaints over "bendgate"
- Does the iPhone 6 Plus bend?
- Adobe buys Aviary to open Creative Cloud to mobile
- iPhone 6 is toughest Apple handset yet
- Ellison steps down: but who's really running Oracle now?
- Amazon releases high-end Kindle Voyage Touch
- Sony warns of massive loss on smartphones
- Dropbox app doesn't work properly with iOS 8
- Michael Dell: Cloud infrastructure is the roads, bridges and highways of the 21st century
- How to check your identity hasn’t been sold to the hackers
- Tim Cook: this is how much TV has changed since the 70s
- Westminster wins the .London battle
- 20 years of PC Pro: from deep pan pizza to virtualisation
- Five reasons why the Apple Watch leaves me cold
- Apple Watch, iPhone 6 and 6 Plus: Tim Cook's Apple back with a bang?
- BT Home Hub 5: how to get maximum speed
- 20 years of PC Pro: one-star reviews (including "the worst tablet we've ever seen")
- 20 years of PC Pro: our best covers