A racing game in Scratch looks simple: a car, a track, a finish line. The version most children build first has a car that turns on the spot, drives at one speed, and counts nine laps every time it crosses the line. This tutorial builds the version that feels like driving. The car speeds up and coasts, slows down on the grass, steers like a real car, times every lap to a hundredth of a second, and only counts a lap that went all the way round.
For parents: this project is a real lesson in variables, multiplication as a rate, and state, which is the idea that a program has to remember what has already happened. Every step below ends with what should happen when your child clicks the green flag, so they can check their own work. It suits children of about 9 and up who have already built one Scratch game. If yours has not, start with our guide on how to make a game on Scratch.
Key Takeaways
- Use a speed variable and multiply it by 0.9 every frame. With 0.6 added per frame while the up arrow is held, top speed settles at exactly 5.4 steps a frame.
- Steer by speed, with
turn right ((speed) * (0.8)) degrees. The car cannot spin while parked, and its turning circle is the same size at every speed. touching coloris true on every frame the car overlaps the line, so a plain lap counter adds 9 or 10 laps per crossing. A checkpoint variable fixes it.- Two checkpoints in order stop shortcuts and stop players scoring laps by wiggling back and forth over the line.
- Time laps with the built-in
timer, not a variable that goes up by 1 afterwait (1) seconds. The wait version only counts whole seconds and runs slow.
What you are building, and the numbers behind it
The game is top-down: you look at the track from above, like a map, and the car rotates as it turns. One sprite is the car. The track is painted on the backdrop, and the game reads the track by its colours. That is the whole trick of a Scratch racing game: touching color ()? lets one sprite react to grass, walls, the finish line and checkpoints without any of them being sprites.
Three facts about Scratch shape every number in this tutorial. The stage is 480 pixels wide and 360 tall. Scratch 3 runs scripts at 30 frames a second in the editor and on the website, so a forever loop runs 30 times a second. And direction is measured clockwise from straight up: 0 is up, 90 is right, 180 is down and -90 is left.
Step 1: paint the track on the backdrop
Click the Stage, open the Backdrops tab and paint a new backdrop. Use flat, solid colours only, because the car will recognise each part of the track by its colour:
- Green for grass. Fill the whole backdrop with it first.
- Mid grey for the road. Draw a thick loop: a rounded rectangle outline with the line width turned right up works well, or paint it with a wide brush. Make the road at least 70 pixels wide.
- Red for barriers. Add a few solid red blocks on the outside of the tightest corners.
- White for the finish line, a stripe about 10 pixels thick straight across the road.
- Yellow for checkpoint 1, a stripe across the road about a third of the way round.
- Light blue for checkpoint 2, about two thirds of the way round.
Decide which way the race goes, clockwise or anticlockwise, and put the checkpoints in that order after the finish line. Keep every corner gentle. Step 3 explains why: this car's tightest turn has a radius of about 72 pixels, so a hairpin tighter than that sends it onto the grass however well you drive.
Why the colours need to be clearly different
touching color is less exact than it looks. In the Scratch renderer's source code, the colour test keeps only the top five bits of red and green and the top four bits of blue. That means Scratch can tell apart only about 16,000 colours, not 16 million, and any shade close enough counts as a match. A very light grey such as #F8F8F0 counts as pure white, for example. Pick colours that are far apart, and never paint anything else on the backdrop in a shade near your finish-line white or checkpoint yellow.
When you choose the colour inside a touching color block later, click its swatch, pick the eyedropper and click the middle of the stripe on the stage. The edges of shapes are smoothed (anti-aliased) on the stage, and an edge pixel is a blend that will not match.
Step 2: draw a car that faces right
Scratch's library cars, such as Convertible and Convertible 2, are drawn from the side, so they look wrong on a track seen from above. Paint your own. Click Paint under Choose a Sprite, draw a rectangle about twice as long as it is wide, and add a darker windscreen near the right-hand end. Drag the whole car so its middle sits on the small crosshair at the centre of the paint canvas, which is the point the car will rotate around.
The car must face right in the costume editor. A costume as drawn is what the sprite looks like at direction 90, so a car drawn facing up will drive sideways. Set its size in the sprite info so it is about 40 pixels long on the stage, and name the sprite Car.
Then build the start script on the car:
when green flag clickedset rotation style [all around]go to x: (0) y: (-130)(change this to a spot just behind your finish line)point in direction (90)set [speed] to (0)
Make the variable first: Variables, Make a Variable, name it speed, choose For this sprite only. The rotation style block matters if anyone has ever clicked the left-right setting on this sprite. In the Scratch 3 source, left-right style always draws the sprite at direction 90 and only flips it, so a top-down car would slide round corners pointing sideways.
Click the green flag: the car should jump to the start, facing along the road.
Step 3: speed, friction and steering
This is the script that makes it feel like a car. Add a second script to the Car sprite. It starts on a broadcast so the countdown in step 6 can hold the car still; for now, add broadcast (go) at the end of the green flag script so it runs straight away.
when I receive [go]foreverif <key (up arrow) pressed?> thenchange [speed] by (0.6)if <key (down arrow) pressed?> thenchange [speed] by (-0.3)set [speed] to ((speed) * (0.9))if <key (left arrow) pressed?> thenturn left ((speed) * (0.8)) degreesif <key (right arrow) pressed?> thenturn right ((speed) * (0.8)) degreesmove (speed) steps
Use key () pressed? from Sensing inside the loop, not the when () key pressed hat. The hat waits for the keyboard's own repeat delay, so the car would lurch, pause, then move.
Why multiplying by 0.9 gives you a top speed
set [speed] to ((speed) * (0.9)) is friction: every frame the car loses a tenth of its speed. While the up arrow is held it also gains 0.6 a frame. At first the gain wins and the car speeds up. As the speed grows, the tenth it loses grows too, until gain and loss are equal. That balance point is the top speed, and you can work it out: top speed = 0.6 × 0.9 ÷ (1 − 0.9) = 5.4 steps a frame, or 162 pixels a second.
| What happens | Frames | Time at 30 frames a second |
|---|---|---|
| From standing to 5.0 steps a frame, up arrow held | 25 | 0.8 seconds |
| Coasting from 5.4 to below 0.1 after letting go | 38 | 1.3 seconds |
| Full speed to about 1 step a frame on grass (step 4) | 11 | 0.4 seconds |
Change the two numbers to change the car. A bigger gain, say 0.8, gives a faster car with a top speed of 7.2. A friction of 0.95 instead of 0.9 gives a slippery, icy car that takes much longer to stop. Reverse uses the same maths with a gain of -0.3, so the top reverse speed is -2.7, half the forward speed.
Why steering uses speed, not a fixed number
A fixed turn, such as turn right (5) degrees while the arrow key is held, lets a parked car spin in place, which no car can do. Multiplying the turn by speed fixes three things at once. A stopped car does not turn at all. In reverse, speed is negative, so the car steers the opposite way, just as a real car does when backing up. And because the car turns 0.8 degrees for every step it moves, it always takes 450 steps to drive a full circle, whatever its speed. That makes the turning circle the same size at every speed, a radius of about 72 pixels, which is the number to design your corners around.
Click the green flag and drive: the car should pick up speed smoothly, coast to a stop when you let go, and refuse to turn while parked.
Copying a friction script takes a minute. Understanding why 0.6 and 0.9 settle at 5.4 is a first taste of algebra, and it is the point where a teacher makes the biggest difference. A free trial class with a Codeyoung teacher shows you where your child is and what they are ready for next, before you commit to anything.
Book a Free Trial →Step 4: grass that slows you down, walls that push back
A popular page-one tutorial sends the car back to its starting position when it touches the grass. That punishes a small mistake far too hard. Slowing the car down is fairer, and it is one block. Add this straight after the friction line, before the steering:
if <touching color [green]?> thenset [speed] to ((speed) * (0.7))
On grass the car now loses 0.9 × 0.7 = 0.63 of its speed every frame, and the same maths as before gives a grass top speed of about 1 step a frame, less than a fifth of the road speed. Cutting a corner across the grass is now slower than driving round it, which is the first line of defence against shortcuts.
Barriers that the car cannot drive through
Walls need more care, because the car can get stuck in them. The obvious fix is move ((0) - (speed)) steps when touching the wall, which undoes the move but not the turn. If the driver steers into the wall, the car's nose rotates into the red, the move back does not rotate it out, and the car is stuck touching the wall every frame.
The reliable fix is to remember where the car was before it moved. Make three more variables for this sprite only: last x, last y and last direction. Put these three blocks at the very top of the forever loop:
set [last x] to (x position)set [last y] to (y position)set [last direction] to (direction)
And this at the very bottom, after move (speed) steps:
if <touching color [red]?> thengo to x: (last x) y: (last y)point in direction (last direction)set [speed] to ((speed) * (-0.5))
The car goes back to exactly where it was at the start of the frame, position and angle both, so it can never end a frame inside the wall. Multiplying speed by -0.5 bounces it backwards at half speed. The same save-and-restore pattern works for the walls of a maze, if your child has built one with our guide to making a maze in Scratch.
Click the green flag and drive into a barrier at full speed: the car should bounce off and never stick, however you steer.
Step 5: count laps with a checkpoint, so each lap counts once
This is the step that page one gets wrong. A widely read car-racing tutorial describes the rule as one lap whenever the car touches the white lap line. Built literally, that is:
if <touching color [white]?> thenchange [laps] by (1)
It does not count one lap. touching color is checked every frame, and it is true on every frame that any pixel of the car overlaps the line. A 40-pixel car crossing a 10-pixel line at the top speed of 5.4 steps a frame overlaps it for 50 pixels of travel, which is 9 or 10 frames. So one pass adds 9 or 10 laps. Slow down over the line and it adds more. Park on it and the counter climbs by 30 every second.
An obvious patch is wait until <not <touching color [white]?>> after the lap is added. That fixes the double count but leaves a bigger hole: a player can drive forward over the line, reverse back over it, drive forward again, and score a lap every few seconds without leaving the start. Cutting across the infield to the line works too.
The checkpoint fix
Make five more variables, all for this sprite only: laps, checkpoint, lap start, last lap and best lap. Then add a third script to the car:
when I receive [go]set [laps] to (0)set [checkpoint] to (0)set [best lap] to (0)set [lap start] to (timer)foreverif <<touching color [yellow]?> and <(checkpoint) = (0)>> thenset [checkpoint] to (1)if <<touching color [light blue]?> and <(checkpoint) = (1)>> thenset [checkpoint] to (2)if <<touching color [white]?> and <(checkpoint) = (2)>> thenset [checkpoint] to (0)change [laps] by (1)set [last lap] to ((round ((((timer) - (lap start)) * (100)))) / (100))set [lap start] to (timer)if <<(best lap) = (0)> or <(last lap) < (best lap)>> thenset [best lap] to (last lap)
The checkpoint variable is the game's memory of how far round the car has been. It can only go 0, then 1 at the yellow stripe, then 2 at the light blue one, in that order. The white line only counts when it is 2, and counting a lap sets it straight back to 0. That one reset is what stops the 9-lap bug: on the next frame the car is still on the line, but checkpoint is 0, so nothing happens.
It also beats the cheats. Wiggling over the line scores nothing, because checkpoint is 0. Cutting across the grass misses a checkpoint. Driving the whole track backwards meets the light blue stripe first, while checkpoint is still 0, so it is ignored; a player going the wrong way has to drive two full laps to score one. Notice also that the car starts just behind the line with checkpoint at 0, so crossing the line at the start of the race does not count as a lap, which is what you want.
With one checkpoint instead of two, a player could drive backwards to it and then forwards to the line, half a lap each way. That is no shorter than racing, so it is not a real shortcut, but two checkpoints make the rule simple to explain: go all the way round, the right way.
Click the green flag and drive one lap: laps should go from 0 to 1 exactly once when you cross the line. Then try wiggling over the line and cutting across the grass. Laps should not change.
If your child wants a scoreboard that looks better than a variable monitor, our guide to building a score counter in Scratch covers showing a number on the stage and explains when a variable should be for all sprites or for this sprite only.
Step 6: a countdown and a lap timer that does not drift
The lap script above already times laps with Scratch's built-in timer, from the Sensing category. It reports the seconds since the project started, to thousandths of a second. Scratch's source resets it every time the green flag is clicked, and reset timer sets it back to 0 whenever you like.
Some tutorials build their own clock instead: a variable that goes up by 1 inside forever after wait (1) seconds. Avoid it here for two reasons. It only counts whole seconds, so two laps of 23.4 and 23.9 seconds would both read 23. And it can only run slow, never fast: in the Scratch 3 source, a wait finishes on the first frame after the second is up, and the loop then has to come round again before the next wait starts. Every pass takes a little more than a second, and the error builds up over a race. Our step-by-step guide on how to make a timer in Scratch compares the two approaches in detail.
Now add a countdown so nobody can start early. Change the end of the car's green flag script to:
say [3] for (1) secondssay [2] for (1) secondssay [1] for (1) secondssay [Go] for (0.5) secondsreset timerbroadcast (go)
The reset timer matters. The timer started at the green flag, so without it the first lap would include the three-second countdown. The driving and lap scripts both start on when I receive [go], so the car cannot move until the countdown ends. Broadcasts are how one script starts others at exactly the right moment; our guide to Scratch broadcast blocks explains them properly.
Click the green flag: the car should count down, refuse to move until Go, and show a last lap time such as 21.37 after the first lap.
Step 7: finish the race after three laps
Inside the lap block, after the best-lap check, add:
if <(laps) = (3)> thenset [speed] to (0)broadcast (finished)say (join [Finished in ] ((round ((timer) * (100))) / (100)))stop [other scripts in sprite]
stop [other scripts in sprite] stops the driving loop, so the car halts on the line instead of coasting on. Because the timer was reset at Go, timer at this moment is the total race time. Any other sprite, such as a big Finished banner or a sound, can react with when I receive [finished].
Here is the complete driving script with steps 3 and 4 in place:
when I receive [go]foreverset [last x] to (x position)set [last y] to (y position)set [last direction] to (direction)if <key (up arrow) pressed?> thenchange [speed] by (0.6)if <key (down arrow) pressed?> thenchange [speed] by (-0.3)set [speed] to ((speed) * (0.9))if <touching color [green]?> thenset [speed] to ((speed) * (0.7))if <key (left arrow) pressed?> thenturn left ((speed) * (0.8)) degreesif <key (right arrow) pressed?> thenturn right ((speed) * (0.8)) degreesmove (speed) stepsif <touching color [red]?> thengo to x: (last x) y: (last y),point in direction (last direction),set [speed] to ((speed) * (-0.5))
Testing checklist: six bugs and their fixes
| Symptom | Cause | Fix |
|---|---|---|
| Car drives sideways | Costume drawn facing up | Redraw it facing right; direction 90 shows the costume as drawn |
| Car points the same way round corners | Rotation style is left-right or don't rotate | set rotation style [all around] |
| 9 or 10 laps per crossing | Plain if touching color lap counter | Checkpoint variable, reset to 0 when a lap counts |
| Grass or wall check never fires | Colour picked from a blended edge pixel | Eyedropper the middle of the shape |
| Car sticks inside a barrier | Move undone but turn not undone | Save and restore x, y and direction |
| First lap time 3 seconds too long | Countdown counted in the timer | reset timer just before broadcast (go) |
Where to take the racing game next
- Two players. Right-click the car, choose duplicate, and change the keys to W, A, S and D. Because every variable was made for this sprite only, each car gets its own speed, laps and checkpoint. Paint the second car a colour that is not green, red, white, yellow or light blue:
touching colorchecks other sprites as well as the backdrop, so a white car would count as a finish line for the other player. - A best lap that survives a reload. Best lap resets every race. Keeping a record between sessions needs a cloud variable, and our guide to saving a high score in Scratch explains the rules, including which accounts can use them.
- Drifting. Move the car's rotation centre forward, between the front wheels, in the costume editor. The car then pivots near its nose and the back swings out on corners, a trick suggested on the Scratch forums that costs no extra code.
- A computer opponent. The angle maths for chasing a target is the same as in our Scratch Pong tutorial, where a computer paddle moves toward the ball at a capped speed so it can lose.
Conclusion
A good racing game in Scratch is a speed variable multiplied by 0.9 every frame, steering tied to that speed, and a track painted in flat colours the car can sense. The part that separates it from a broken one is the lap counter. touching color stays true for every frame the car is on the line, so the game has to remember where the car has been, and a checkpoint variable that only moves 0, 1, 2 in order does exactly that. It is also the clearest introduction to program state a child will meet in Scratch.
If your child enjoyed building this, Codeyoung's Scratch coding classes take projects like this further with a teacher alongside, so a car stuck in a wall becomes a lesson in debugging instead of a reason to give up.
Codeyoung runs 1:1 live online classes for children aged 6 to 17, with a teacher who adapts the pace to your child rather than a fixed syllabus. The first class is free, so you can see how they respond before deciding.
Book a Free TrialSources checked 28 November 2026: the Scratch Foundation's scratch-vm source for move steps and turn, touching color and the timer (scratch3_sensing.js), the project clock and its reset on the green flag (clock.js and runtime.js), wait (scratch3_control.js) and rotation styles (rendered-target.js); the colour comparison and touching candidates in scratch-render's RenderWebGL.js; the 30 frames a second compatibility setting in scratch-gui's vm-manager-hoc.jsx; the Convertible sprites in scratch-gui's sprite library; the Scratch Wiki page Touching Color ()?; and the Scratch forum thread on drifting cars. Speeds, frame counts and the turning circle are calculated from those formulas at 30 frames a second, not timed in a live editor; a slow computer that drops frames will run the game more slowly.
