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.
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.
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.
For more details about purchasing this feature and/or images for editorial usage, please contact Jasmine Samra on email@example.com
- Europol warns: public Wi-Fi isn't safe
- IDC: iPad intertia opens door for Windows tablets
- Rivals put on notice as Spotify snaps up The Echo Nest
- Windows 8.1 Update 1 leaks via Microsoft's website
- Mozilla questions why Dell charges £16 to install Firefox
- Hundreds of NHS sites vulnerable to hackers
- Samsung Chromebook 2 gets faux-leather look - and bloatware
- Windows 8.1 Update 1 hits RTM
- Hackers take Meetup.com offline over $300 ransom
- Office 365 goes social with "Oslo" news feed
- Move over Delia: IBM Watson is cooking tonight
- Eric Schmidt on the double-edged smartphone: friend and foe
- Getty joins the race to the bottom
- Hour of Code: five steps to learn how to code
- Sony Xperia Z2 Tablet review: first look
- Sony Xperia Z2 review: first look
- Samsung Galaxy Gear 2 review: first look
- Nokia XL review: first look
- Samsung Galaxy S5 review: first look
- Nokia X review: first look
- Windows Server 2012 R2: how the Datacenter edition could change SMBs
- Invoices and VAT: how to set up your documents correctly
- Nexus 5 vs Samsung Galaxy S4 Active: the best phone for avoiding screen burn
- How much is a social user worth?
- The key to choosing a secure password
- Thunderbolt Bridge: a fast Mac migration tool
- Should you advertise on Twitter?
- How to track a lost smartphone
- Self-publishing success: the best way to sell your book
- 1.6TB SSD: why would you need one?