- Learnt28 min · a switch, a referee, an if inside an if
- Basic25 min · a race with a proper start
- Challenge 1–327 min · a countdown, a rematch, a photo finish
- Extrabonus · a false start that costs you
Review — where we got to 5 min
Two lanes, two keys, one finish line. Two ideas come back today.
if <key [d v] pressed?> theninside a forever loop.<() = ()>, from the Quiz Show.
Quick-fire
Hold your key down BEFORE anybody presses the green flag. What happens, and why is that a problem?
Reveal the answer
You are already moving on the very first frame. The controller asks whether the key is down, and it is — it does not know or care when you pressed it.
Today’s Topic 3 min
- A
race onvariable used as a switch, not as a number - A referee sprite that counts down and turns the switch on
- An if inside an if: the race is on AND the key is down
- The winner switching the race back off
Learning outcome
By the end of this lesson you will be able to:
- Use a variable to turn other scripts on and off.
- Run a countdown that every player has to wait for.
- Freeze the race the moment somebody wins it.
Learnt — a switch, a referee, a freeze 28 min
1 · A variable that means yes or no
Every variable so far has been a number that counted something. This one only ever holds 0 or 1, and those stand for no and yes.
- Make a variable called
race on. - Leave it showing for now, so you can watch it flip.
- It starts at
0. That means the race has not started. - Something will set it to
1later. That means GO.
2 · The referee
Add one more sprite whose whole job is to start the race. It counts down and then turns the switch on.
- Add a sprite near the top of the Stage and name it
Referee. - Give it
when flag clicked. - First block:
set [race on v] to (0). - Then three say blocks, one second each:
3,2,1. - Then
set [race on v] to (1). - Then
say [GO!] for (1) seconds.
when flag clicked
set [race on v] to (0)
say [3] for (1) seconds
say [2] for (1) seconds
say [1] for (1) seconds
start sound [Win v]
set [race on v] to (1)
say [GO!] for (1) seconds
How to use this: Click the green flag to start it. Nothing else to press — watch it through, then use ↺ Reset to run it again.
3 · One more question inside the controller
The switch does nothing on its own. Every controller has to ask about it, and the answer is an if inside an if.
- Open your rocket’s controller.
- Drag a new
if <> thenonto the empty canvas. - Put
<(race on) = (1)>in its diamond. - Drag your WHOLE existing key-if into its mouth.
- Drag the pair back into the forever loop.
forever
if <(race on) = (1)> then
if <key [d v] pressed?> then
change x by (4)
end
end
end
Do the same on the second rocket. Both controllers must be gated, or one player can start early and the other cannot.
How to use this: Click the green flag, then click the stage once and hold D to fly Rocket 1.
4 · The winner turns it off
One more line in the winning script. Without it the loser keeps flying while the result is on screen, which looks like the game did not notice.
when flag clicked
wait until <touching [Finish v]?>
set [race on v] to (0)
start sound [Cheer v]
say [Blue wins!] for (3) seconds
stop [all v]
How to use this: Click the green flag, click the stage once, then hold D and →. Nothing moves until the countdown finishes.
Basic — a race with a proper start 25 min
Your race from last week. Three passes over it.
Step 1 — the switch and the referee
- Make the
race onvariable and leave it showing. - Add a referee sprite at the top of the Stage.
- Build its countdown script.
- Press the flag and watch the number flip. Do not touch the rockets yet.
Step 2 — gate the first rocket
- Build the outer if on the empty canvas.
- Drag the existing key-if inside it.
- Drag the whole thing back into the forever loop.
- Test by holding your key from before the flag. Nothing should move.
Step 3 — gate the second
- Do exactly the same on rocket 2.
- Test both players holding their keys early.
Step 4 — the freeze
- Add
set [race on v] to (0)to both winning scripts. - It goes above the sound and the message.
- Race properly and watch the loser stop dead when the winner crosses.
Step 5 — hide the number
- Once it all works, untick the
race onbox. - A switch is for your project, not for the player.
- Race five more times to check nothing broke.
Nothing moves at all, even after GO.
The test says = 0 instead of = 1, or the referee never sets it to 1. Show the variable again and watch it.
One player can still start early.
Only one controller was gated. Open both and check they match.
The loser keeps flying after somebody wins.
The winning script sets the switch off after the say block, so it does not run for three seconds. Move it above.
My second race starts on GO with no countdown.
set [race on v] to (0) is not the first block of the referee’s script.
A countdown worth waiting for
Three say blocks is a countdown. Make yours something players actually feel.
- A sound on each number, or one that builds.
- The GO is bigger, louder or brighter than the numbers.
- It is the same length every time, so players can learn it.
A rematch
Losing should be followed by racing again, not by reaching for the green flag. Add a way to run it back.
- A rematch starts without touching the green flag.
- Both rockets go back to their start lines.
- The countdown runs again.
- Nobody can move during it.
Teacher note — this needs the winning script to stop stopping everything
stop [all v] makes a rematch impossible: nothing is left running to notice a click. The move is to replace it with broadcast [round over v] and let the referee decide what happens next — which is exactly the shape T36 builds a whole match out of. Pupils who get here have found next-next lesson by themselves.
The photo finish
Last lesson you found the bug: cross together and both winning messages fire. Fix it. Only one player may win.
- Two rockets crossing in the same instant produce exactly one message.
- The same player does not always get it.
- The race still ends properly.
Teacher note — a second switch is the tidy answer
when flag clicked
wait until <touching [Finish v]?>
if <(race on) = (1)> then
set [race on v] to (0)
say [Blue wins!] for (3) seconds
stop [all v]
end
The winner turns the switch off, and the other script — arriving a moment later — checks it first and finds the race already over, so it says nothing. It is the same switch doing a second job, which is worth pointing out: a good variable often answers more than one question.
A true dead heat, in the very same frame, can still slip through. Say so. Real racing has the same problem, which is why it has cameras.
A false start costs you
Pressing early currently does nothing. Make it send the rocket backwards, so guessing the GO is a real risk.
How to use this: Click the green flag, then click the stage once and hold D to fly Rocket 1.
- Pressing during the countdown moves the rocket backwards.
- The player is told why.
- A rocket cannot be pushed off the left of the Stage.
- Pressing after GO still works normally.
Teacher notes — the else nobody uses
forever
if <key [d v] pressed?> then
if <(race on) = (1)> then
change x by (4)
else
start sound [Bonk v]
say [Too early!] for (0.6) seconds
change x by (-20)
end
end
end
Note that the two ifs have swapped places. The key question is on the outside now, because the punishment only makes sense when somebody has actually pressed something. Ask the class why the other order would be wrong: gated the old way, the else would fire every frame of the countdown whether anybody touched the keyboard or not.
Going further: next lesson there are things in the way.
Summary 4 min
One variable, holding nothing but 0 or 1, decided what every other script was allowed to do.
- A switch is a variable that only ever holds 0 or 1.
- The referee owns the switch. Nothing else turns it on.
- Every controller asks about it, with an if inside an if.
- The winner turns it off, which freezes everybody at once.
- Switch
- A variable holding 0 or 1, standing for no and yes.
- Referee
- A sprite whose job is to run the rules rather than to be played.
- Gate
- An if wrapped round something, so it only happens when a condition is met.
- False start
- Moving before the GO. A game has to decide what that costs.
Next lesson: asteroids in the way, and a star worth flying for.