- Learnt28 min · a menu, three starts, records, your robot
- Basic24 min · build the whole game
- Challenge 1–327 min · three starts, lasting records, the garage
- Extrabonus · the tournament
Review — where we got to 5 min
(speed)and(turn)are sliders. Too much speed flies off; too much turn wobbles.- The pink dot finds the marker pads, because it is at the front.
(timer)counts seconds.reset timerstarts it again.
Quick-fire
Your best lap is in a variable. You press the green flag. What happens to your record if the flag script says set [best v] to (0)?
Reveal the answer
It is gone. A record must never be reset by the flag. That is today’s big idea.
Today’s Topic 3 min
- A menu screen that only listens while it is showing
- Three tracks as three backdrops, each with its own start
- A finish line that ends the lap
- Three records the green flag must not reset
- Your own robot — and a trap to avoid
Learning outcome
By the end of this lesson you will be able to:
- Choose a backdrop from a menu with keys.
- Keep a best score that survives the green flag.
- Finish a project and share it.
Learnt — one robot, three tracks 28 min
1 · The plan
The game has two screens.
- The Menu backdrop. The robot waits in the dashed box.
- You press 1, 2 or 3. That track’s backdrop appears, and the robot races one lap.
- It crosses the finish. It says its time. Back to the Menu.
2 · A menu that only listens on the menu
If 2 worked all the time, pressing it in the middle of a lap would start a new one. So every menu key first checks which backdrop is showing.
when flag clicked
switch backdrop to [Menu v]
go to x: (0) y: (-96)
point in direction (90)
when [1] key pressed
if <(backdrop [name v]) = [Menu]> then
set [track v] to (1)
broadcast [setup v]
end
(backdrop [name v]) is in Looks. It reports the name of the backdrop on show.
3 · Each track has its own start
The backdrops are in order: Menu is 1, the Loop is 2, the Wave is 3, the Twister is 4. So track + 1 is the backdrop number.
when I receive [setup v]
switch backdrop to ((track) + (1))
if <(track) = (1)> then
go to x: (-17) y: (118)
point in direction (90)
end
if <(track) = (2)> then
go to x: (-46) y: (97)
point in direction (132)
end
if <(track) = (3)> then
go to x: (8) y: (116)
point in direction (115)
end
broadcast [race v]
4 · One lap, then finish
Last lesson the follower ran for ever. Now it runs until the white of the finish strip.
when I receive [race v]
reset timer
repeat until <touching color [#FFFFFF]?>
if <color [#FF00D4] is touching [#23262D]?> then
turn ccw (turn) degrees
else
if <color [#00E0FF] is touching [#23262D]?> then
turn cw (turn) degrees
else
move (speed) steps
end
end
end
set [lap time v] to (timer)
broadcast [finished v]
when I receive [finished v]
say (join [Lap: ] (lap time)) for (2) seconds
switch backdrop to [Menu v]
go to x: (0) y: (-96)
point in direction (90)
How to use this: Click the green flag, then click the stage once. Press 1, 2 or 3 on the menu to choose a track. The robot drives itself.
5 · Records the flag must not reset
A record is the best time ever. It must live through the green flag. So we never set it under the flag.
- Three variables:
best 1,best 2,best 3. - 0 means “no record yet”.
- A lap becomes the record if there is no record yet, or it is faster.
if <(track) = (1)> then
if <<(best 1) = (0)> or <(lap time) < (best 1)>> then
set [best 1 v] to (lap time)
end
end
6 · Your robot
Make the robot yours. Paint two more costumes with different body colours. Keep the sensor dots exactly the same.
when [c] key pressed
if <(backdrop [name v]) = [Menu]> then
next costume
end
Why not use set [color v] effect to (50)? Try it. The colour effect repaints the whole costume, dots and all.
when flag clicked
clear graphic effects
go to x: (-17) y: (118)
point in direction (90)
forever
if <color [#FF00D4] is touching [#23262D]?> then
turn ccw (5) degrees
else
if <color [#00E0FF] is touching [#23262D]?> then
turn cw (5) degrees
else
move (3) steps
end
end
end
when flag clicked
wait (4) seconds
set [color v] effect to (50)
How to use this: Click the green flag and watch. Nothing to press — after four seconds the robot changes colour.
Basic — build the whole game 24 min
How to use this: Click the green flag, then click the stage once. On the menu, press C to change your robot and 1, 2 or 3 to race a track.
Step 1 — the backdrops
- Paint a Menu backdrop. Write the three track names and “Press 1, 2 or 3”.
- Put your three tracks after it, in order. Give each a finish strip.
- Name the first backdrop exactly
Menu.
Step 2 — the menu keys
- Make the variable
track. - Build the flag script and the three key scripts.
- Press 1, 2 and 3 on the menu. Check each shows the right backdrop.
Step 3 — setup and race
- Build the setup script. Find each start by dragging the robot onto the line.
- Move your follower into the race script, inside repeat until.
- Build the finished script. Race all three tracks.
Step 4 — records
- Make
best 1,best 2andbest 3. Show them on the Stage. - Add the three record checks to the top of the finished script.
- Race, press the flag, race again. The records must stay.
Step 5 — your robot
Paint two more costumes. Add the C key script.
Pressing 2 shows the wrong track.
The backdrops are out of order. The Menu must be first, then tracks 1, 2 and 3.
The lap ends the moment it starts.
The start is on the finish strip. Move the start a robot’s length further along the line.
My records are 0 after every green flag.
There is a set block for them under the flag. Delete it.
Three tracks, three starts
Every track in your game can be raced from the menu.
- 1, 2 and 3 each open a different track.
- On every track, the robot starts on the line, facing along it.
- Keys do nothing while a lap is running.
How to use this: Click the green flag, then click the stage once. Press 1, 2 or 3 on the menu to choose a track.
Records that last
Each track keeps its own best lap.
- The first lap on a track becomes its record.
- A slower lap never replaces a record. A faster one always does.
- The green flag does not reset any record.
How to use this: Click the green flag, then click the stage once. Press 1, 2 or 3 on the menu to race a track, and C to change your robot.
Teacher note — the whole finished script
when I receive [finished v]
if <(track) = (1)> then
if <<(best 1) = (0)> or <(lap time) < (best 1)>> then
set [best 1 v] to (lap time)
end
end
if <(track) = (2)> then
if <<(best 2) = (0)> or <(lap time) < (best 2)>> then
set [best 2 v] to (lap time)
end
end
if <(track) = (3)> then
if <<(best 3) = (0)> or <(lap time) < (best 3)>> then
set [best 3 v] to (lap time)
end
end
say (join [Lap: ] (lap time)) for (2) seconds
switch backdrop to [Menu v]
go to x: (0) y: (-96)
point in direction (90)
The slow-lap test is the one that matters. A pupil who only ever gets faster never finds out that <(lap time) > (best 1)> — the wrong way round — also “works” on the first lap.
The garage
Let the player choose a robot.
- At least three robots, each a different costume.
- On the menu, C shows the next robot and says its name.
- Every robot follows the line equally well.
How to use this: Click the green flag, then click the stage once and press C to change the robot.
Teacher reveal
when flag clicked
go to x: (0) y: (-96)
point in direction (90)
switch costume to [blue v]
say (costume [name v])
when [c] key pressed
next costume
say (costume [name v])
In the full game the C script keeps its menu check. The trap is a costume whose dots moved: a robot painted by duplicating the first costume keeps them in place, and one painted from scratch almost never does.
The tournament
Press T on the menu to race all three tracks in a row.
- After each lap, the next track starts by itself.
- The three lap times are added up in
total. - At the end, the robot says the total and goes back to the menu.
How to use this: Click the green flag, then click the stage once and press T on the menu. Watch all three tracks, about 30 seconds.
Teacher reveal — on the Stage
when [t] key pressed
if <(backdrop [name v]) = [Menu]> then
set [tournament v] to (1)
set [total v] to (0)
set [track v] to (1)
broadcast [setup v]
end
when I receive [finished v]
if <(tournament) = (1)> then
change [total v] by (lap time)
wait (2) seconds
if <(track) < (3)> then
change [track v] by (1)
broadcast [setup v]
else
set [tournament v] to (0)
broadcast [tournament over v]
end
end
And on the robot, the finished script only goes back to the menu outside a tournament:
when flag clicked
set [tournament v] to (0)
switch backdrop to [Menu v]
go to x: (0) y: (-96)
point in direction (90)
when I receive [finished v]
say (join [Lap: ] (lap time)) for (2) seconds
if <(tournament) = (0)> then
switch backdrop to [Menu v]
go to x: (0) y: (-96)
point in direction (90)
end
when I receive [tournament over v]
say (join [All three in ] (total)) for (3) seconds
switch backdrop to [Menu v]
go to x: (0) y: (-96)
point in direction (90)
Setup and race are unchanged. That is the payoff of broadcasting setup instead of writing it into each key script: the tournament reuses it three times.
Summary — the arc, finished 5 min
Four lessons ago, the robot needed you to drive it. Now it:
- reads the floor with
<touching color [#23262D]?>— pads, holes and finish lines; - steers itself with two dots and
<color [#FF00D4] is touching [#23262D]?>; - has two numbers you tune with sliders, and a lap timer;
- races three tracks from a menu, and remembers a record for each.
- Menu
- A screen where the player chooses, before the game starts.
- Record
- The best result ever. The green flag must not reset it.
- Backdrop number
- A backdrop’s place in the list. The first one is 1.
- Graphic effect
- A change to how a whole costume looks — including its sensor dots.
Ship it
- Race each track once, so every record has a time. Then save.
- In the project page’s instructions, write: press 1, 2 or 3 to race, C to change robot.
- In the notes, say which track is hardest, and your best settings for it.
- Share it, and send the link to your teacher.