Simple clear advice in plain English

Learn programming with Python

How to program a simple snake game for the Raspberry Pi using a coding language called Python

Python programming code
Python is easy for beginners to use to create a simple game

**Please note that this feature contains code that your web browser may try to run.

The promise of the Raspberry Pi sub-£30 microcomputer is that it will usher in a new age of computer education not seen since the home-computing boom of the 1980s. If you followed our introduction to the Raspberry Pi last issue, you will now have a working computer connected to a keyboard, mouse, network and monitor or TV – but where do you go from there?

In this project, we will explain how to program a game for the Pi. Do not worry if you have absolutely no programming experience – we’ll be walking through each step in a simple, plain-English style.

Read more: Raspberry Pi beginner's guide
                  Make a games controller for the Raspberry Pi
                  Make a simple LED game

Getting started with Python
The Raspberry Pi Foundation, the organisation responsible for the Raspberry Pi, has chosen a computer programming language called Python to be its primary focus. It is this language, named after Monty Python’s Flying Circus, that we’ll use to write a version of the unending food-munching, tail-lengthening Snake game, popularised by Nokia phones.

Python is a powerful programming language, but it is also designed to be as easy as possible to learn. It is also where the Raspberry Pi gets the second part of its name – Python shortened to Pi.

The Raspberry Pi Foundation recommends using Raspbian for an operating system. This comes with several software packages already installed – including Python and Pygame, a Python add-on that adds the relevant tools for writing games.

Idle
To write the required code, we will use a tool called Idle – named after Monty Python member Eric Idle – that is included in the Raspbian operating system.

To load Idle, double-click the icon on the Raspbian desktop. If two Idle icons are visible, don’t worry. The first, labelled ‘Idle’, is the one to double-click; the second, ‘Idle 3’, is a special tool for programming in the latest version of Python – it includes features not needed for now, so ignore it.

Idle has two main parts: an interactive portion, which operates a bit like a super-powerful calculator and is called the ‘shell’, and an editing portion. Having double-clicked the Idle icon, the shell appears. Test this by typing in a simple sum, such as 1+2, and pressing the Enter key – the answer 3 should appear.

The shell is useful for trying out simple commands, but not for more detailed work. Click the File menu and choose New Window to open an editing window; this is like a text editor or word processor. Multiple lines can be typed in this window, and pressing Enter inserts a new line beneath the text cursor (rather than immediately executing the command, as with the shell).

Take a moment to click the File menu in the editor window and then choose Save As. In the box that appears, type raspberrysnake.py as the file name and click Save. As you work, periodically return to the File menu and click Save; this ensures that you will not lose too much work if the Pi experiences a power cut.

Start writing code
Programming a game is like writing a story – it needs a beginning, a middle and an end, and everything needs to occur in a specific order so that the reader does not become confused. Here, the reader is the Raspberry Pi and the story is a series of instructions.

To start the game’s story, certain extras are required to extend the capabilities of the Python programming language. These packages, known as modules, include the Pygame add-on described earlier as well as tools for producing random numbers, measuring time and controlling the Python system.

Reader Comments

   

Add your comment

Please keep comments constructive and free from abuse of any kind and swearing. If you wish to link to a product or service online, please do so in such a way that makes it clear that it is not spam. If you are connected to any such product you should make that clear.

We may use your comments in the magazine. We may edit your comments for clarity or to remove unacceptable material. We will attribute your comments but not share your email address.

We request your email address and record your Internet Address (IP address) in order to block spam from our site. We will never share this information without your permission.

All comments are reviewed by the Computeractive Team before being published. Please bear with the slight delay this causes, you don't need to post more than once.

Click here to read our Privacy Policy

Click here to read our site Terms & Conditions

Related articles

Raspberry Pi's OS is similar to Windows

Raspberry Pi: exploring Raspbian

Use programming languages Python and Scratch, and find pre-installed applications including a web browser called Midori

sk-pang-raspberry-starter-kit

Review: Starter Kit A for Raspberry Pi

Get started with the GPIO port on the Raspberry Pi with this kit that includes a case, breadboard and components

Raspberry Pi

Raspberry Pi to be manufactured in Sony's UK factory in Pencoed

Bulk of manufacturing shifted to South Wales as Raspberry Pi announces it is proud to be 'made in the UK'

Content Recommendation

Related articles

No matching document

Question & Answer

Q.Why is Windows Backup skipping files?

> Read the answer

Q.Why do my scanned documents display gibberish?

> Read the answer

Q.How can I convert MTS files to edit in Windows Movie...

> Read the answer

Best deals on the web

img

Apple iMac 2.7GHz 8GB 1TB 5400rpm 21.5''

£1024.95- Buy it now

img

HP Pavilion p6-2480ea (D2L08EA)

£269.95- Buy it now

img

HP Pavilion p6-2310ea (C3T79EA)

£299.99- Buy it now

Updating your subscription status Loading

Most popular articles

No matching document

Poll

Do you have Windows 8?

Jargon Buster

Computing terms explained in plain English

VGA

Video Graphics Array. Standard socket for connecting a monitor to a computer.

Great shopping deals from Computeractive

Information currently unavailable