- Learnt28 min · momentum, steering as a number, checkpoints as a sprite
- Basic24 min · upgrade your race
- Challenge 1–327 min · momentum, the grass, five posts
- Extrabonus · three tracks
Review — where we got to 5 min
- Two cars, two sets of keys, both working at once.
- A car turns, then moves forward —
turn cw (15) degreesand thenmove (10) steps, in that order. - Checkpoints in order stop the finish line being cheated.
Quick-fire
Your car moves 4 steps when you hold up. It stops dead as soon as you let go. What does that feel like?
Reveal the answer
Like a chess piece, not a car. Real cars build up speed. They roll on after you stop pushing. That is today.
Today’s Topic 3 min
- Acceleration: speed that builds up
- Friction: speed that slowly drops away
- Steering as a number, and what a negative turn means
- Checkpoints as a sprite, placed by your own block
Learning outcome
By the end of this lesson you will be able to:
- Make a sprite that keeps moving after you let go of the key.
- Use one variable to turn either way. It can be above or below zero.
- Place five checkpoints with one My Block and count laps from them.
Learnt — giving the car weight 28 min
What is missing
A real car does not go from still to fast straight away. It accelerates. That means it gets faster while you keep your foot down. When you lift your foot, it does not stop. It rolls, and slowly loses speed.
Your car does neither. Let’s fix both with one variable.
Step 1 — the key stops moving the car
This is the strange bit. It is the big idea: the up arrow no longer moves the car. It makes a number bigger.
if <key [up arrow v] pressed?> then
change [speed v] by (0.8)
end
if <key [down arrow v] pressed?> then
change [speed v] by (-0.8)
end
Step 2 — one block does all the moving
move (speed) steps
Try it. Hold up for a second, then let go. The car keeps going for ever. Nothing makes speed smaller again.
Step 3 — friction
Take 10% off the speed every time round the loop:
set [speed v] to ((speed) * (0.9))
Steering, the same way
Do it again with a second variable. Here is the clever part. There is only one turn block:
if <key [left arrow v] pressed?> then
change [steering v] by (-1.2)
end
if <key [right arrow v] pressed?> then
change [steering v] by (1.2)
end
turn cw (steering) degrees
set [steering v] to ((steering) * (0.85))
turn cw (-5) degrees is a turn to the left.Checkpoints, properly
Last lesson the checkpoints were colours painted on the track. That works. But every track you draw must use just the right colours.
Here is a better way. Make them a sprite, with five costumes numbered 1 to 5. Post 5 is the finish line.
Placing five clones is five jobs that are nearly the same. So make a block:
define Post (x) (y) (number)
go to x: (x) y: (y)
switch costume to (number)
create clone of [myself v]
when flag clicked
hide
Post (145) (62) [1] :: custom
Post (145) (-62) [2] :: custom
Post (-145) (-62) [3] :: custom
Post (-145) (62) [4] :: custom
Post (0) (100) [5] :: custom
One rule for all five
Every post runs the same check:
- Am I touching the car?
- Is my number one more than the last checkpoint passed?
- If yes, I am the new last checkpoint.
- If I am number 5, that is a lap. Count it and go back to 0.
when I start as a clone
show
forever
if <touching [Red car v]?> then
if <(costume [name v]) = ((P1 checkpoint) + (1))> then
set [P1 checkpoint v] to (costume [name v])
if <(P1 checkpoint) = (5)> then
change [P1 lap v] by (1)
set [P1 checkpoint v] to (0)
end
end
end
end
Why costume name and not costume number?
Either works, if they match. Our checkpoint sprite uses ten costumes named “0” to “9”. So costume name “3” is the right label. Costume number would be 4. It is one out, all the way along.
You can paint your own five costumes and name them 1 to 5. Then the number and the name are the same, and you can use either. Mixing them up is the most common reason this stops working.
Basic — upgrade your race 24 min
Open your Lesson 5 race.
Step 1 — two variables per car
Make P1-Speed and P1-Steering. Set both to 0 when you click the flag.
Step 2 — rewrite the controls
The four key checks now only change numbers. Then, still inside the loop, add turn cw (P1-Steering) degrees, move (P1-Speed) steps, then the two ×-blocks.
Step 3 — feel it
Drive it. Try 0.9, then 0.98, then 0.7 for the friction. A bigger number is more slippery. Pick what you like.
Step 4 — the checkpoint sprite
Make a new sprite with five costumes. Add the Post My Block and the clone script above.
Step 5 — player 2
Duplicate the car and change the four keys. Make P2-Speed, P2-Steering, P2 checkpoint and P2 lap. Add a second check to each post for the blue car. One sprite watches both.
The car spins on the spot and never stops turning.
Your steering has no friction. Add set [steering v] to ((steering) * (0.85)) at the bottom of the loop.
The car shoots off the screen instantly.
Change speed by 0.8, not 8. Check the ×0.9 is in the loop. Without it, nothing ever slows down.
The lap never counts.
Most of the time it is costume number mixed up with costume name. Show costume [name v] on the Stage. Drive over each post to see what it really says.
Both cars share the same lap count.
The post only checks one car. Or both checks change the same variable. Each car needs its own checkpoint and lap variables.
Give it weight
- Holding up makes the car faster and faster, up to top speed.
- Letting go makes it roll on and slow down by itself.
- Turning has the same feel.
Grass, not a wall
This is the main mission. Going off the track must not send you back to the start.
- On the grass, the car is very slow.
- Back on the road, it speeds up as normal.
- Nothing jumps to a new place, and nothing resets.
Teacher note
Being sent back to the start is the harsher rule and the easier code, so some will have built it that way. Ask which is more fun to play. It is a design question, not a programming one, and it is the first time this course has asked one.
Five posts, two cars
- One checkpoint sprite, five clones, placed by a My Block.
- Skipping a post does nothing at all.
- The same five posts count both cars, each on its own.
Teacher reveal — hiding the posts
The deck paints the checkpoints the same colour as the road, so players cannot see them. Ours are visible on purpose — a pupil debugging an invisible checkpoint is debugging blind.
Get it working visible, then hide it. Either set [ghost v] effect to (100) or paint them the road colour; both still count, because touching [edge v]? does not care what a sprite looks like.
Three tracks
Next lesson the game gets a menu. A menu needs things to choose from. Draw three tracks.
- An easy one: wide, gentle corners.
- A hard one: narrow and twisty.
- One of your own design. A figure of eight is a good one.
Summary 5 min
- Acceleration: the key changes a number. One
move (speed) stepsdoes the moving. - Friction: multiply by 0.9 every loop. The car rolls to a stop by itself.
- One turn block does both ways. A negative turn goes the other way.
- Checkpoints as a sprite work on any track. A My Block places all five in five lines.
- A post only counts if its number is one more than the last one. That makes a lap honest.
- Acceleration
- Speed going up while you keep pushing.
- Friction
- Speed slowly dropping when you stop pushing.
- Negative
- Below zero. In motion blocks it means the opposite direction.
- Clone
- A copy of a sprite, made while the game runs. All five posts are one sprite.
Hand in
Share your race and send the link to your teacher. Bring three tracks next week.