- 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. Today we use two blocks you know.
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? Why is that a problem?
Reveal the answer
You are already moving at the very start. The controller asks if the key is down. It is down. The controller does not 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 switches the race back off.
Learning outcome
By the end of this lesson you will be able to:
- Use a variable to turn scripts on and off.
- Run a countdown that all players must wait for.
- Freeze the race as soon as somebody wins.
Learnt — a switch, a referee, a freeze 28 min
1 · A variable that means yes or no
So far, each variable counted something. This one only holds 0 or 1. 0 means no and 1 means yes.
- Make a variable called
race on. - Leave it showing for now. Then you can watch it change.
- 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. Its only job is to start the race. It counts down. Then it 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. Each rocket must ask about it. We use 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 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 need the gate. If not, 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
Add one more line to the winning script. Without it, the loser keeps flying while the result is on screen. It 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
Use your race from last lesson. You will change it in a few steps.
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 change. Do not touch the rockets yet.
Step 2 — gate the first rocket
- Build the outer if on the empty canvas.
- Drag the key-if inside it.
- Drag the whole thing back into the forever loop.
- Hold your key down, then press the flag. Nothing should move.
Step 3 — gate the second
- Do the same on rocket 2.
- Both players hold their keys early. Test it.
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. The loser stops dead when the winner crosses.
Step 5 — hide the number
- When it all works, untick the
race onbox. - The switch is for your project, not for the player.
- Race five more times. 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 rocket has the gate. 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 waits three seconds before it runs. 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 make a countdown. Make yours fun to watch.
- 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. Then players can learn it.
A rematch
After you lose, you want to race again. You should not need the green flag. Add a way to race again.
- A new race starts without 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 a bug. Cross together and both winning messages show. Fix it. Only one player may win.
- Two rockets cross at the same moment. You see just one message.
- The same player does not always win 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
Right now, pressing early does nothing. Make it send the rocket backwards. Then 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 go off the left of the Stage.
- After GO, the keys work as normal.
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 held only 0 or 1. It decided what every other script could do.
- A switch is a variable that only holds 0 or 1.
- The referee owns the switch. Nothing else turns it on.
- Each rocket asks about it, with an if inside an if.
- The winner turns it off. That freezes everybody at once.
- Switch
- A variable that holds 0 or 1. 0 means no and 1 means yes.
- Referee
- A sprite that runs the rules. Nobody plays it.
- Gate
- An if wrapped round some blocks. They only run when the if is true.
- False start
- Moving before the GO. The game decides what that costs.
Next lesson: asteroids in the way, and a star worth flying for.