This article was written by Sean McManus and first appeared in The MagPi 86. Get a free Raspberry Pi with a 12-month subscription to the print edition of The MagPi magazine.
Scratch 3 is now on Raspberry Pi, with new blocks for the Sense HAT. In this game, you tilt your Raspberry Pi to turn left and right, and use the HAT’s up and down joystick controls as an accelerator. Going off-road or colliding with the computer’s racer damages your car. Can you complete three laps before your car is too beaten up to drive? If so, how fast is your qualifying time? This project will give you a tour through what’s new in Scratch 3, including computer speech, new sound effects, and the extensions. Rev up!
while blah
do a thing
if something
do another thing
What you'll need
Speakers to hear the engines roar
Download the full code from magpi.cc/github86
![The vertical bar chart in the middle of the LED matrix is the speedometer. The tiny joystick is your accelerator. Tilt to steer!](http://images.ctfassets.net/2lpsze4g694w/iqPZWrUCJSu0uvtfyHIdY/c27994ab15d9a833be89f2af5b10c6a8/Scratch-3-game-LED-matrix.jpg?w=800)
Add extensions to Scratch
One of the best features of Scratch 3 is the extensions, which enable you to add new capabilities to Scratch. For example, you can add blocks for simple electronics, controlling the GPIO, video sensing, and using the Makey Makey input device. To simplify Scratch for first-timers, the Pen and Music blocks that used to be in the Blocks Palette have now been put into extensions too.
For this project, you need to add the extensions for Raspberry Pi Sense HAT, Pen, and Text to Speech. The menu to add extensions is in the bottom left.
Draw the road
Hover over the icon in the bottom right to open the backdrop menu. Click the option to paint a backdrop and draw a green box that fills the Stage. Add Listing 1 to Sprite1 (the cat). You’ll need to use the menu in the first block to set up a new message called ‘draw road’. Click the script to test it. You should see the road is drawn with two differently coloured checkpoints at the top and bottom.
![Throttle and Tilt game Listing 1](http://images.ctfassets.net/2lpsze4g694w/12ELBU69KgofE1hMUzUBPw/d009cdece74343a605136b7619cc018c/listing1.png?w=800)
Draw the cars
Hover over the icon at the bottom of the Sprite List to add a sprite and choose Paint. The vector editor is now the default. Draw an overhead view of a race car, driving right. Centre your car on the cross-hairs target on the canvas, to make sure it turns correctly. Right-click your car in the Sprite List and duplicate it. With vector images, you can edit the shapes and colours afterwards, so you can easily make the second car look different. Click each car in the Sprite List and use the boxes above them to rename them to ‘player car’ and ‘rival car’.
![The vector art editor is now the default. It makes it easy to modify the race cars to make two distinct designs](http://images.ctfassets.net/2lpsze4g694w/2cNttKNcevyvTizAugM9tF/cd87e3d66982e05391e6c9942514080e/Scratch-3-sense-HAT-game-car-painting.png?w=800)
Start the rival car moving
The rival car is just an obstacle to avoid. Add the scripts in Listing 2 to it. You’ll need to create a new broadcast message for ‘start race’. Click the green flag to position the car, then click the longer script. You should see it looping around the road, without veering off. If needed, adjust the size of the car in Listing 2 (and Listing 3 later) to fit both cars comfortably on the road. You might need to tweak the positions in the glide blocks in Listing 2 too.
![Throttle and Tilt game Listing 2](http://images.ctfassets.net/2lpsze4g694w/5vaCTl4HHUnPPFjxpjkoyV/ed5bdd51ccd4f190bbef4b22073150e8/listing2.png?w=800)
Create the variables
In Scratch 2, variables were created in the Data section of the Blocks Palette. Now, it’s called Variables again, as it was in Scratch 1.4. Every project now starts with a variable created called my variable to help newcomers experiment. You need to make these variables: half lap count, next checkpoint, qualifying time, roadworthiness, row to light, row to turn off, and speed. They can be ‘for all sprites’. Untick your variables in the Blocks Palette, except for roadworthiness, which we want to show on the Stage. Drag its box to the bottom middle of the Stage.
Set up the player’s car
Click the player’s car in the Sprite List and add Listing 3 to it. Sense HAT and Text to Speech are both new in Scratch 3. The display block enables you to set a pixel pattern to show on the LED matrix, using the light colours you’ve set up with set background and set colour blocks. At the start, the game shows a chequered flag on the Sense HAT and the computer says, “Ready, Get Set, Go!”
![Throttle and Tilt game Listing 3](http://images.ctfassets.net/2lpsze4g694w/7laQS37CV90ABfDOiEA8Pt/63182d7bef426778957de2ee536a5a51/listing3.png?w=800)
Add player movement controls
You’ll steer by holding your Raspberry Pi up, with the Sense HAT facing you, and tilting it left and right. You detect this axis of movement using the pitch block in the Sense HAT extension. Tilting left (up to 90 degrees) gives values of 0 to 90. Tilting right (up to 90 degrees) gives values of 360 to 270. Listing 4 goes on your player’s car. It turns your car if your Raspberry Pi is tilted left or right by more than 10 degrees, and uses the Sense HAT joystick to trigger the blocks to change your speed. To make the pink blocks, click My Blocks and use the button to make your own blocks, with the names you see in the listing. For the touching color blocks, click the colour box in the block and use the new, improved pipette to copy the checkpoint colours from the Stage.
![Throttle and Tilt game Listing 4](http://images.ctfassets.net/2lpsze4g694w/2QrX0VkGga3d3JR8P81UR5/60dff78ead5a013d28c621f82d0cc202/listing4.png?w=800)
Add sounds
There are fun new sounds to explore. For this game, we need the Cheer, Coin, Car Horn, Engine, and Skid effects. Click the Sounds tab on your player’s car, and use the button in the bottom left to add them. You can now search for them by name.
Add speed controls
Find the pink hat blocks for define increase speed and define decrease speed. They were created for you in the Code Area when you made those blocks in Step 7. Use them to build Listing 5. You’ll need to create new blocks (using My Blocks again) for ‘speed lights on’ and ‘speed lights off’.
![Throttle and Tilt game Listing 5](http://images.ctfassets.net/2lpsze4g694w/A5MMGO6o0S2EwST2CgyKG/875fc1386207f0ed7ef3a70d00a0d66d/listing5.png?w=800)
The set pixel Sense HAT block enables you to light an individual LED using its x and y position (both numbered from 0 to 7, from the top left). We’re using it to turn the LED matrix into a speedometer, with a bar graph that grows with your speed, which can range from 0 to 8. Scratch 3 adds new sound effects blocks. We’re making the engine sounds higher when you speed up. Don’t confuse the sound pitch (how low or high it is) with the Sense HAT pitch (the angle of the device). Your colours in the ‘speed lights off’ script should match the Sense HAT background colour in Listing 3. Click the colour box in the block to check the numbers.
Add the lap counter
To confirm the car has done a lap of the track, we’re using checkpoints. The car has to visit the red one, then the yellow one, and repeat until it’s gone around three times. The next checkpoint variable remembers which checkpoint the car needs to visit next. The half lap count variable stores how many checkpoints have been successfully crossed. Add Listing 6 to your existing define lap counter block in the Code Area. Copy the red and yellow colours in the touching color blocks from the Stage.
![Throttle and Tilt game Listing 6](http://images.ctfassets.net/2lpsze4g694w/1FMJnhdLspRKk3eDSNbZhT/21666436cbdd23088b7853b9162c9373/listing6.png?w=800)
Detect car damage
Your roadworthiness variable is like a health score. It goes down when you hit the grass or the other car. The two scripts in Listing 7 continuously check whether you’ve hit something. If you have, they reduce your score and then pause briefly to stop your health sapping too fast. Use the pipette to copy the exact grass colour and the main body colour of your rival car (pink in our case) into your scripts. Listing 7 goes on the player’s car.
![Throttle and Tilt game Listing 7](http://images.ctfassets.net/2lpsze4g694w/4zBFTHFKfEyrVTZcvWNr0s/d146b3f1380512ffdc94add61755df56/listing7.png?w=800)
Add game-over sequences
There are two ways for the game to end. Either your roadworthiness gets to zero, or you successfully complete three laps. The display text block scrolls an appropriate message across the Sense HAT’s LEDs. Attach Listing 8 to the end of Listing 4, the script with movement controls on the player’s car. Now you’re ready to play!
![Throttle and Tilt game Listing 8](http://images.ctfassets.net/2lpsze4g694w/2dLm7X0eBVgyNS6iUiQy5x/44db9f97e26739e26bf59782990a0b05/listing8.png?w=800)
Top tip: Random gliding
We’re not using it here, but there’s a new block to glide to a random position, which you might find handy in your games.
Sean McManus is the author of Scratch Programming in Easy Steps, Cool Scratch Projects in Easy Steps, and Mission Python. Get free chapters at Sean’s website.
See also: