- Learnt28 min · gates from lists, lap times, a best lap
- Basic25 min · a race against the clock
- Challenge 1–327 min · a lap board, three laps and stop, a fifth gate
- Extrabonus · split times
Review — Kart Racer II 5 min
- A step is
speed × sin(d)across andspeed × cos(d)up. - The kart moved by its
heading, which followed itsdirectionby a fifth of the gap each tick. The gap was the slip. - The wrap — + 540, mod 360, − 180 — found the short way round.
- The Pen drew skids while the slip was over 15°.
Quick-fire
Level 2’s Car Race had coloured posts round the track, to stop people cheating a lap. How did it make sure they were driven in order?
Reveal the answer
A variable remembered which post was next, and only that colour counted. Today the posts become numbers in two lists — so the track can change without a single new block.
Today’s Topic 3 min
- Checkpoint gates stored in two lists:
gate xandgate y - One Gate sprite that only stands on the next gate
- A lap time:
timer − lap start - A best lap that survives the green flag
Learning outcome
By the end of this lesson you will be able to:
- Make a track’s checkpoints out of data, not drawings.
- Time something with the timer, without resetting it.
- Keep a record that a new game does not wipe.
Learnt — a lap you cannot cheat 28 min
1 · The cheat
The easy way to count laps is “every time the kart touches the white line, add 1”. Watch what that does to a kart that just rocks over the line.
How to use this: Click the green flag to start it. Nothing else to press — the kart drives over the finish line and back, three times.
2 · Gates in two lists
A lap is four gates, taken in order. Their places go in two lists, one item per gate. Item 1 of gate x goes with item 1 of gate y.
| Gate | gate x | gate y | Where |
|---|---|---|---|
| 1 | 185 | −10 | right-hand side |
| 2 | 10 | −60 | the bend at the bottom |
| 3 | −185 | −40 | left-hand side |
| 4 | 0 | 125 | the finish line |
3 · One Gate sprite
You do not need four sprites. One Gate stands on the next gate, waits for the kart, then jumps to the one after. A gate that is not there cannot be touched, so the order cannot be broken.
when flag clicked
set [next gate v] to (1)
set [laps v] to (0)
forever
go to x: (item (next gate) of [gate x v]) y: (item (next gate) of [gate y v])
wait until <touching [Kart v]?>
change [next gate v] by (1)
if <(next gate) > (length of [gate x v])> then
set [next gate v] to (1)
change [laps v] by (1)
end
end
How to use this: Click the green flag to start it. Nothing else to press — a script drives one lap and the gate jumps ahead of it.
4 · Lap times and a best lap
The timer counts seconds from when the project starts. We never reset it. Instead, we write down the time a lap started, and take it away at the end.
set [lap time v] to (([round v] of (((timer) - (lap start)) * (100))) / (100))
add (lap time) to [lap times v]
if <<(best) = (0)> or <(lap time) < (best)>> then
set [best v] to (lap time)
end
set [lap start v] to (timer)
change [laps v] by (1). The round keeps two decimal places.Look at what the green flag does not do: it never sets best. So the record stays when you play again. 0 means “no best lap yet”.
How to use this: Click the green flag to start it. Nothing else to press — a script drives three laps. Then press the flag again and watch best.
Basic — a race against the clock 25 min
Open your kart from Kart Racer II. The kart itself stays the same.
Step 1 — the lists and variables
- Make two lists, For all sprites:
gate xandgate y. Type the four numbers from the table into each. - Make a third list,
lap times, and the variablesnext gate,laps,lap start,lap timeandbest.
Step 2 — the Gate
- Save our gate, gate.svg, and use Upload Sprite. Call it
Gateand set its size to 110. - Build the script from Learnt section 3.
- Put the lap-time blocks from section 4 inside its
if, afterchange [laps v] by (1). - Under its green flag, add
go to [back v] layer,delete all of [lap times v]andset [lap start v] to (timer).
How to use this: Click the green flag, then click the stage once. Hold ↑ to go, ← and → to steer. Drive through each gate.
The gate jumps ahead before I reach it.
The kart is touching the next gate’s place already. That happens when two gates are too close together. Move one of them in the lists.
best stays at 0.
The or is an and. “Best is 0 and this lap is faster” is never true after the first lap.
The first lap time is huge.
lap start is not set under the green flag, so the first lap counts from when you opened the project.
A lap board
Tell the driver where they are in the race.
- Crossing the line after lap 1: the kart says “Lap 2 of 3”.
- After lap 2: “Final lap!”.
- After lap 3: “Finished!”, and it stays.
How to use this: Click the green flag to start it. Nothing else to press — a script drives three laps.
Teacher reveal
In the Gate, after change [laps v] by (1): broadcast (new lap v). Then on the Kart:
when I receive [new lap v]
if <(laps) = (1)> then
say [Lap 2 of 3] for (1.5) seconds
end
if <(laps) = (2)> then
say [Final lap!] for (1.5) seconds
end
if <(laps) = (3)> then
say [Finished!]
end
Three laps and stop
A race has an end.
- After lap 3, the keys stop working.
- The kart rolls to a stop on its own friction.
- It says the whole race time, to one decimal place.
How to use this: Click the green flag to start it. Nothing else to press — a script drives three laps, then lets go.
Teacher reveal
when flag clicked
set [race start v] to (timer)
repeat until <(laps) = (3)>
drive (gas) (steer) :: custom
end
repeat (40)
drive (0) (0) :: custom
end
say (join [Finished! 3 laps in ] (join (([round v] of (((timer) - (race start)) * (10))) / (10)) [ s.]))
The forever becomes a repeat until, with the key blocks still above drive (gas) (steer) :: custom inside it. Driving with gas 0 is the coast: friction does the stopping.
A fifth gate
Prove the track really is data.
- Add a gate in the bottom-right corner, at (120, −125).
- It must come between gate 1 and the old gate 2.
- Do not change a single block.
How to use this: Click the green flag to start it. Nothing else to press — a script drives one lap through five gates.
Teacher reveal
Insert 120 at position 2 of gate x, and −125 at position 2 of gate y. The Gate already asks (length of [gate x v]), so it works out on its own that a lap is now five.
insert (120) at (2) of [gate x v]
insert (-125) at (2) of [gate y v]
Split times
Real race timing does not wait for the end of the lap.
- On lap 1, write down the time at every gate.
- On lap 2, at each gate, show how far ahead (−) or behind (+) lap 1 you are.
- The Gate says it, for one second, as you go through.
How to use this: Click the green flag to start it. Nothing else to press — a script drives two laps; watch the gate in lap 2.
Teacher reveal
In the Gate, after wait until <touching [Kart v]?>:
set [split v] to ((timer) - (lap start))
if <(laps) = (0)> then
add (split) to [best splits v]
else
set [diff v] to (([round v] of (((split) - (item (next gate) of [best splits v])) * (10))) / (10))
if <(diff) > (0)> then
say (join [+] (diff)) for (1) seconds
else
say (diff) for (1) seconds
end
end
Summary 5 min
- The gates are two lists. A new gate is two new numbers.
- One Gate sprite shows only the next gate, so they can only be taken in order.
- A lap time is
timer − lap start. The timer is never reset. - A best lap that the green flag does not set survives every new game.
- Checkpoint
- A place a racer must pass, so a lap cannot be cut short.
- Lap time
- How long one lap took: the time now, take away the time it started.
- Split
- The time at one checkpoint, part-way round a lap.
Hand in
Save your race. Next lesson you stop racing the clock: a rival kart lines up beside you, and it drives itself.