Build your first game using Scratch on the Raspberry Pi

17th Jul 2013 | 11:00

Build your first game using Scratch on the Raspberry Pi

Ideal for first-time programmers

You can use a wide range of programming languages with the Raspberry Pi, but here we're going to use Scratch. It's a great beginner's language, as it introduces many of the concepts of programming while at the same time being easy to use. It's especially good for creating graphical programs such as games.

If you've never programmed before, or if you're new to graphical programming, we'll ease you gently in, and hopefully have a little fun along the way. Without further ado, let's get started.

You'll find Scratch on the desktop in Raspbian, so there's no need to install anything - just click on the icon to get started. It's best to use Raspbian rather than one of the other distros for the Pi for this, since not many of the others support Scratch.

If you want to try this on other (non-Raspberry Pi) computers, Scratch is available for most systems, including Windows, Mac OS X and most flavours of Linux. Head to for more information if you want to install Scratch on another computer.

The main window is split up into sections. Broadly speaking, the bits you can use to make your programs are on the left, you make your programs in the middle, and the programs run on the right. Each program is made up of a number of sprites (pictures) that contain a number of scripts. It's these scripts that control what happens when the program is running.

Scratch main window

In the top-left hand corner, you'll see eight words (Motion, Looks, Sound, Pen, Control, Sensing, Operations and Variables). Each of these is a category that contains pieces that you can drag and drop into the scripts area to build programs. Good luck!

Variables and messages

Sooner or later, you're going to want to get your program to remember something. It might be a number, a piece of text, or anything. You can do this using variables.

These are little pieces of the computer's memory that your program can place pieces of data in. In step 3, we created a pair of these to store some numbers in, although we could also have put text in them. Note that in some programming languages, you have to create different types of variables if you want to store different types of data, but you don't need to worry about that in Scratch.

Once you have created a variable, you can then use it in a few ways. Firstly, you have to set them to be a particular value, then you can use them to evaluate conditions (which we'll do in steps 6 and 11), or you can output them.


If you create a number of scripts, you may need to communicate between them. Sometimes you can do this with variables, but it is often better to use messages. These can be used to trigger scripts in the same way as key presses can. When one script broadcasts a message, it will then trigger all the scripts that start with When I Receive…

Like variables, messages have names, so they have to be created first, and for a script to trigger it has to be linked to the same message as the broadcast.

Step by step

1. Create the mouse

step 1

Change the image from a cat to a mouse by going to Costumes > Import > Animals > Mouse 1. Then reduce the sprite size by clicking on the Shrink sprite icon (circled) and then the mouse. We set it to about the size of our thumbnail.

2. Set keys

step 2

Click on Scripts, and change When Right Arrow Key Pressed to When r Key Pressed. We'll use this to start a new game (r is for reset). Then drag Move 10 Steps off the bottom of the script. If you drop it back in the left side, it will be deleted.

3. Create and name variable

step 3

Click on Variables in the top-left (see above for more details on what they are). Click on Make A Variable and enter the variable name as score. Repeat the process to create a variable called over.

4. Reset the score

step 4

Under the script When r Key Presses, add the lines show (from Looks), Go To X:100, Y:100 (from Motion, don't forget to change the 0s to 100s), Set Score To 0 and Set Over to 0 (both from Variables).

5. Add Broadcast

step 5

Add the block Broadcast… to the bottom of the When r Key Pressed script. Once it's there, click on the drop-down menu and select New... and give the message the name 'start'. We'll use this to let the other sprite know that the game has started.

6. Create a loop

step 6

We can create loops that cycle through the same code many times. Continue the script with Repeat Until… (from Control), and then drag and drop … = … (from Operators), then drag Over (from Variables) into the left-hand side of the = and enter 1 on the right.

7. Add to your loop

step 7

Inside the Repeat Until Over = 1 block, add Change score By 1 (from Variables), Move 7 Steps (from Motion) and If On Edge, Bounce (also from Motion). These three pieces of code will be constantly repeated until the variable over gets set to 1.

8. Hide the mouse

step 8

Once the game has finished (and the cat has got the mouse), the Repeat Until loop will end and the program will continue underneath it. Drag Hide (from Looks) under the loop, so the mouse disappears when this happens.

9. Resize your cat

step 9

Select Choose New Sprite From File > Cat 4, and shrink the sprite down to an appropriate size, like we did with the mouse. Each sprite has its own set of scripts. You can swap between them by clicking on the appropriate icon in the bottom-right.

10. Move the cat

step 10

In the scripts for the new sprite, start a new script with When I Receive start (from Control), and Go To X:-100 Y:-100. This will move the cat to the opposite corner of the screen from the mouse. (0,0) is the middle.

11. Give the cat a loop

step 11

As with the mouse, the cat also needs a loop to keep things going. Add Repeat Until (from Control), and then in the blank space add Touching Sprite 1 (from Sensing). This will keep running until the cat (sprite 2) catches the mouse (sprite 1).

12. Set the difficulty

step 12

Inside the Repeat Until block, add Point Towards Sprite 1 (from Motion) and Move 4 Steps (also from Motion). The amount the cat and mouse move in each loop affects the difficulty of the game. We found 4 and 7, respectively, to work well.

13. Finish the loop

step 13

The loop will finish when the cat has caught the mouse - the game is over, so we need to stop the script on Sprite 1. We do this by adding Set over To 1 (from Variables) underneath the Repeat Until block. This will cause Sprite 1's main loop to finish.

14. Tell the player the game is over

step 14

We now want to let the player know that the game is over. We will do this in two ways: with audio, and on screen. Add Play Drum 1 for 1 Beats (from Sound), then Say Mmmm Tasty For 1 Secs (from Looks).

15. Display a score

step 15

Finally, we can let the player know their score. We increased the variable score by one every loop, so this will have continued to go up. Add Say You Scores … For 1 Secs, then drag another Say … for 1 Secs block and then drag score (from Variables) into the blank field.

16. Play your game!

step 16

Press r and play! You can use the up and down arrows to move the mouse around. You can make it easier or harder by changing the size of the sprites and the amount they move each loop. Good luck and happy gaming!

tutorial Scratch Raspberry Pi programming Brit Week computingcarousel-en-au computingcarousel-en-gb computingcarousel-en-us computingrecommended-en-au computingrecommended-en-gb computingrecommended-en-us
Share this Article

Most Popular

Edition: UK
TopView classic version