- Learnt28 min · a crash, and three lives
- Basic24 min · the game-over screen
- Challenge 1–327 min · 60 touches, the hurt flag, the stale round
- Extrabonus · rounds done right
Review — last lesson 5 min
- A
statevariable holds a word naming the situation:title,play,over. - Every script asks the state before it acts. The one that forgets is the bug.
broadcastannounces a change; the player’s own key replaces the green flag.
Quick-fire
Your game reaches the over state after a fixed number of ticks, however well or badly the player flew. What is missing?
Reveal the answer
A reason to lose. The round has to end because of something the player did — and that means a cost for being hit.
Today’s Topic 3 min
touching (Bolt v)?ending the roundlives, and why one bolt can take all three in a fifth of a second- A
hurtflag, and a cool-down script of its own - Where a reset belongs — under the flag, or at the start of a round
Learning outcome
By the end of this lesson you will be able to:
- End a round because of how the player played.
- Stop a single collision being counted sixty times.
- Say which of your resets belongs in which script, and why.
Learnt — crashing, and lives 28 min
1 · One touch ends it
The simplest way to lose: touching a bolt sets the state to over. Because every script asks the state, nothing needs telling twice — the bolt hides itself, the flying loop falls out, and the whole Stage stops together.
repeat until <not <(state) = [play]>>
next costume
if <touching (Bolt v)?> then
set [state v] to [over]
end
end
How to use this: Click the green flag and fly into the bolt with the arrow keys.
2 · Three lives instead of one
One hit is harsh. Make lives — shared, because there is one player — and spend one per hit:
if <touching (Bolt v)?> then
change [lives v] by (-1)
set [ghost v] effect to (50)
go to x: (-150) y: (0)
wait (0.35) seconds
clear graphic effects
if <(lives) = (0)> then
set [state v] to [over]
end
end
How to use this: Click the green flag and fly. The bolt hunts you, so all three lives will go.
3 · Why a hit needs a moment of safety
Scratch checks that if about thirty times a second. A parrot overlapping a bolt is still overlapping it on the next frame, and the next. So one collision is not one hit — it is one hit per frame for as long as you are touching.
That is why the parrot is moved away and made see-through for a moment. Challenge 1 takes those blocks out so you can measure exactly how bad it is; Challenge 2 puts back something better.
Basic — the game-over screen 24 min
Open last lesson’s Storm Flight. You are giving it a way to end, and a way to begin again.
Step 1 — lives
- Make
lives, for all sprites, and show it. - Make
roundstoo — it is how you will prove the play-again works.
Step 2 — reset where the round starts
- In the
when I receive [start v]script, setstatetoplayandlivesto 3. - Add
change [rounds v] by (1). - Leave the green flag doing nothing but going to the title. This matters. See Challenge 3.
Step 3 — the hazard
- Add the
Lightningsprite and call itBolt. - Give it the shape from last lesson: a forever loop that asks the state, shows itself in
playand hides otherwise. - Inside
play:point towards (Parrot v),move (9) steps,point in direction (90).
Step 4 — losing a life
- In the flying loop, test for touching the bolt.
- Lose a life, go see-through, fly home, wait a third of a second, clear the effect.
- If lives reached 0, set the state to
over.
Step 5 — the screen
- After the loop, bring the parrot to the middle and make it bigger.
- Say
GAME OVER, thenPress space to play again. - Make sure your space-key script sends
startwhen the state isover.
How to use this: Click the green flag, press space to play, lose all three lives, then press space again.
All three lives go the instant I touch a bolt.
The wait and the move-away are missing. This is Challenge 1, and it is worth building deliberately before you fix it.
The round never ends, however many times I am hit.
The if <(lives) = (0)> then is outside the touching check, or lives skipped past 0 into negatives so it never equalled 0 exactly. Use <(lives) < (1)> if you are unsure.
The bolt keeps chasing after the game is over.
Its loop is not asking the state. Same bug as last lesson’s Challenge 1.
The bolt turns upside down as it chases.
point towards rotates the sprite. Add point in direction (90) straight after the move, or set the rotation style to “do not rotate”.
The second round starts with no lives.
Your reset is under the green flag. That is Challenge 3 — and it is the single most common bug in this whole arc.
One bolt, sixty hits
Measure the bug rather than being told about it.
- Park the parrot inside a stationary bolt and leave it there.
- Run the loop exactly 60 times —
repeat (60)with a tiny wait, so it is about two seconds. - Count
touchesas well aslives, and take the guard out completely.
How to use this: Click the green flag and just watch. The parrot does not move and neither does the bolt.
Teacher note
Ask for a prediction before running it. Most pupils guess 1, or 3. The answer being exactly the loop count is the thing that makes “per frame, not per event” land.
The hurt flag
Fix it properly. Everything stays exactly as it was in Challenge 1 — same sixty frames, same stationary bolt, same parrot sitting in it — and you add one variable.
- A
hurtvariable, 0 or 1. - The hit only counts when
hurtis 0. - A separate script clears it after 0.4 seconds.
How to use this: Click the green flag and watch the hurt box flick between 1 and 0 as the touches creep up.
Why does the fixed version still end on minus lives?
Because the cool-down fixes the rate of damage, not the floor. Nine hits on three lives is still −6. Stopping at zero is a separate guard — the if <(lives) > (0)> then you met in Lesson 3-20 — and a finished game needs both. Ask pupils to add it and watch the number land on 0.
Teacher reveal — why the cool-down must be its own script
when I receive [cool down v]
set [ghost v] effect to (55)
wait (0.4) seconds
clear graphic effects
set [hurt v] to (0)
Put that wait inside the main loop instead and the loop stops for 0.4 seconds — the parrot freezes mid-game and the fix is worse than the bug. A broadcast starts a second script that waits alongside the first. That is the real lesson here, and it is the same reasoning as the pause script next lesson.
The stale round
Move your resets under the green flag, play two rounds, and watch the second one start already broken.
set [lives v] to (3)under the flag, not in the round.- Two rounds back to back, with two hits each.
- Write down what round two started with.
How to use this: Click the green flag and read the speech bubbles. Two rounds run one after the other.
Teacher reveal — the rule
A reset belongs in the script that owns the thing being reset.
- Under the flag: things that should outlast a round — the high score, the number of rounds played, the difficulty setting.
- At the start of a round: things the round owns — lives, score, position, effects, leftover clones.
Ask them to list every variable in their project under one of those two headings. It takes four minutes and prevents most of the bugs in the next three arcs.
Rounds done right
Three rounds in a row, each one starting clean, and a best score that survives all three.
- The flag sets up only what outlasts a round.
- The round resets only what it owns.
- A
bestthat never goes down.
How to use this: Click the green flag and let three rounds play out. Watch best against stars.
Teacher note
This is the pattern the rest of Level 3 assumes. If a pupil’s next game has a score that resets oddly, come back to this project rather than debugging the new one — the shape is easier to see here.
Summary 5 min
touching (Bolt v)?plusset [state v] to [over]is a game you can lose.- A collision is tested every frame, so one touch is many hits unless you stop it.
- The ghost effect does not make you safe. A
hurtflag does — 60 touches became 9. - The cool-down goes in a script of its own, or the wait freezes the game.
- A reset belongs in the script that owns the thing: the flag for what outlasts a round, the round for what it owns.
- Lives
- How many hits are left before the round ends.
- Cool-down
- A short time after an event during which it cannot happen again.
- Flag variable
- A variable holding 0 or 1, used as a yes-or-no answer.
- Stale state
- A new round starting on the last round’s leftovers.
Hand in
Save your game. Next lesson is the one that tests whether your state machine really works: pause.