Learning Goals 3 min
By the end of this lesson you will be able to:
- Use change [lives v] by (-1) on every missed kuih so the player has a number to protect.
- Send a broadcast (broadcast (game over v)) the moment (lives) = (0), so every sprite and the Stage react together.
- Receive the broadcast on the Stage with when I receive (game over v), swap to a Game Over backdrop, and halt the project with stop [all v].
Warm-Up — the game without stakes 7 min
Open HW-L2-38-Catch.sb3 from last lesson. Kuih fall. The basket moves. Catches raise the Score; misses just vanish. Aljay plays it for twenty seconds and shrugs: "okay… what do I lose? when does it end?"
What's missing?
Two things. A lives counter that goes down when you miss (so missing feels bad). And an ending — a moment where the game stops and tells you it's over. Without these, a "game" is just a screensaver. The Score we already have from arc F's HUD.
Today we add both. The lives variable you already met in L02-36 (the Score & Lives Counter) — we reuse the same pattern. The new part is the broadcast that ties everything together.
when flag clicked
set [score v] to (0)
set [lives v] to (3)
Predict: if a change [lives v] by (-1) runs after the repeat-until exits (the "miss" case), how many misses end the game?
Reveal
Three. Lives starts at 3. Each miss drops it by one — 3, 2, 1, then 0. The moment it reads 0, the game-over watcher fires. The Score keeps its own count of catches, completely separate from Lives.
New Concept — broadcasts as a game-wide announcement 15 min
You met variables in arc F and broadcasts in L02-20. Today they meet in a real game loop.
Lose a life on a miss — tiny, in the right place
Inside the kuih's clone script you already have the catch (if-touching-Basket) and the miss (after the repeat-until). The miss now drains a life:
when I start as a clone
go to x: (pick random (-220) to (220)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-5)
if <touching (Basket v) ?> then
change [score v] by (1)
go to x: (pick random (-220) to (220)) y: (170)
end
end
change [lives v] by (-1)
delete this clone
Who watches the lives counter?
Now the awkward question: who notices when lives hits zero? The kuih is busy falling. The basket is busy listening to arrow keys. The cleanest answer: a dedicated watcher script on the Stage, running its own forever loop.
when flag clicked
forever
if <(lives) = (0)> then
broadcast (game over v)
stop [this script v]
end
end
The stop [this script v] matters — without it, the watcher keeps broadcasting game over many times per second, which is wasteful and can cause stutters when receivers re-fire.
Why broadcasts and not just a stop here
We could put stop [all v] right inside the watcher. The game would freeze. But multiple things need to happen at game over: the Stage swaps to a Game Over backdrop, the basket could play a sad sound, the kuih spawner stops, and eventually everything stops. A broadcast lets every sprite listen on the same channel and react in parallel.
when I receive (game over v)
switch backdrop to [Game Over v]
stop [all v]
The backdrop
Click the Stage in the sprite pane. Go to the Backdrops tab. Add a new backdrop — paint one with big "GAME OVER" text, or pick one and edit it. Name it exactly Game Over so it matches the dropdown.
Worked Example — adding the losing path 12 min
Open HW-L2-38-Catch.sb3. We add lives and the game-over flow in eight steps.
Step 1 — Make the two variables
Variables palette → Make a Variable. Name the first score (For all sprites) if it doesn't already exist. Make a second called lives (For all sprites). Tick both checkboxes. Drag the monitors to the top corners.
Step 2 — The reset script on the Stage
Click the Stage. Drag in when ⚑ clicked. Snap on set [score v] to (0) and set [lives v] to (3). Add switch backdrop to [backdrop1 v] so re-clicking the flag resets the Game Over backdrop.
Step 3 — Lives on miss
Click the kuih sprite. After the repeat until ends (so the kuih fell past the bottom), before the outer delete this clone, snap on change [lives v] by (-1).
Step 4 — Test catches and misses
Click the flag. Score should climb on catches. Lives should drop on misses. Both monitors update in real time. Nothing happens at lives = 0 yet — that is the next step.
Step 5 — The lives-watcher (on the Stage)
Click the Stage. Drag in another when ⚑ clicked (separate from the reset). Add forever. Inside, add if <> then with () = () in the diamond — left side (lives), right side 0. Inside the if, snap on broadcast (game over v) then stop [this script v].
Step 6 — The Game Over backdrop
Stage → Backdrops tab → paint a new backdrop. Big text: "GAME OVER". Name it exactly Game Over.
Step 7 — Receive the broadcast on the Stage
Back on the Stage's Code tab. Drag in when I receive (game over v). Snap on switch backdrop to [Game Over v] then stop [all v].
Step 8 — Lose on purpose
Click the flag. Miss three kuih on purpose. Lives drops 3 → 2 → 1 → 0. The screen flips to GAME OVER and everything stops. Click the flag again — the backdrop and both numbers reset for a fresh game.
The full Stage script set
when flag clicked
set [score v] to (0)
set [lives v] to (3)
switch backdrop to [backdrop1 v]
when flag clicked
forever
if <(lives) = (0)> then
broadcast (game over v)
stop [this script v]
end
end
when I receive (game over v)
switch backdrop to [Game Over v]
stop [all v]
What you just built: a complete, playable game with a beginning (flag, reset), a middle (catch kuih, watch lives), and an end (game over screen). This is the whole shape of every arcade game — even Pac-Man does exactly this. The last two arc lessons add polish: speed ramp-up (L02-40) and the capstone build (L02-41).
Try It Yourself — three game-over twists 15 min
Goal: Show the final score on the Game Over screen. Add a say on the Stage's game-over handler. (Stages can say too — the text shows top-left.)
when I receive (game over v)
switch backdrop to [Game Over v]
say (join [Final score: ] (score)) for (3) seconds
stop [all v]
Think: The join block builds sentences out of variables. You will use it anywhere you want a message that includes a number.
Goal: Stop the kuih spawner at game over. Right now kuih keep raining after the player loses. Add a receiver on the kuih sprite.
when I receive (game over v)
stop [other scripts in sprite v]
delete this clone
Think: Because the Stage's stop [all v] runs at the same time, this feels redundant — but if you later let the player click "play again" without a full stop, this safety net matters.
Goal: Add a "you won" path. If the player reaches (score) = (20) before lives hits zero, broadcast you win and switch to a "You Win!" backdrop. Build the second watcher.
when flag clicked
forever
if <(score) = (20)> then
broadcast (you win v)
stop [this script v]
end
end
when I receive (you win v)
switch backdrop to [You Win v]
stop [all v]
Think: The two watcher scripts are identical in shape — same forever, same if-check, same broadcast-then-stop. When you see the same shape twice, you are recognising a game pattern. The capstone in L02-41 leans on this hard.
Mini-Challenge — Aisyah's score-stuck-at-zero bug 5 min
"Aisyah's score never goes up"
Aisyah finished the worked example. She clicks the flag. Kuih fall. She catches one — score stays at zero. She catches ten more — still zero. Misses work fine (lives drops correctly). Here is her kuih clone script:
when I start as a clone
go to x: (pick random (-220) to (220)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-5)
if <touching (Basket v) ?> then
go to x: (pick random (-220) to (220)) y: (170)
change [score v] by (1)
end
end
change [lives v] by (-1)
delete this clone
Stare at the order inside the if-then. What runs first, and what does it do to the touching check?
Reveal the fix
The snap-back runs first. That is fine on its own — but watch what happens at the bottom of the same loop turn: the kuih is now at the top, far from the basket. That part is OK. The real trouble is subtler: because the snap-back and the score-bump are both inside the if, and the kuih can briefly still overlap the basket after a tiny move, the score sometimes bumps and sometimes doesn't. The reliable fix is to score first, then move away — so the point always counts before the kuih leaves the basket:
when I start as a clone
go to x: (pick random (-220) to (220)) y: (170)
show
repeat until <(y position) < (-170)>
change y by (-5)
if <touching (Basket v) ?> then
change [score v] by (1)
go to x: (pick random (-220) to (220)) y: (170)
end
end
change [lives v] by (-1)
delete this clone
The rule: do the bookkeeping (score, sound, life) before you move the sprite away or delete it. Side-effects come first; "leaving" comes last.
Recap 3 min
You finished the core Catch the Kuih game. Catches bump the score; misses drain lives. A watcher on the Stage runs a forever loop asking "is lives zero yet?" and the instant it is, broadcast (game over v) fires a project-wide announcement. Every sprite and the Stage can listen and react — swap backdrop, stop spawning, play a sad sound, halt everything. This separation — one announces, many react — runs almost every Scratch game's "moments": start, win, lose.
- score / lives
- Two For-all-sprites variables for progress and remaining chances. Reset on flag click, changed in the kuih's catch and miss exits.
- Broadcast
- A project-wide message sent with broadcast (name v). Any sprite or the Stage listening with when I receive (name v) reacts in parallel.
- Watcher script
- A small forever loop whose only job is to check a condition (like (lives) = (0)) and fire a broadcast when true. Usually on the Stage.
- Backdrop swap
- switch backdrop to [name v] changes the Stage's background. Used on game over and game start to show the scene.
- stop [all v]
- The big red button. Halts every script in every sprite at once. Use it last, after any visible cleanup.
Homework 2 min
Catch the Kuih v1.0. Tonight your game becomes playable end-to-end. Start, play, lose, see Game Over, click the flag, start over.
- Open
HW-L2-38-Catch.sb3. Save a copy asHW-L2-39-Game-Over.sb3. - Make the
scoreandlivesvariables. Build the Stage reset script (Step 2). - Add the catch-bumps-score (already there) and miss-drains-lives blocks to the kuih clone (Step 3).
- Build the lives-watcher on the Stage (Step 5).
- Paint a Game Over backdrop named exactly
Game Over(Step 6). - Build the Stage's when I receive (game over v) handler (Step 7).
- Play. Lose on purpose (miss three kuih). The screen should swap to Game Over and freeze.
- Click the flag. Verify the backdrop resets, score resets to 0, lives resets to 3, and the game starts fresh.
Save as HW-L2-39-Game-Over.sb3.
Bring back next class:
- The
.sb3file. - Your highest score from at least three play-throughs.
- Your answer to: "What feels too easy? What feels too hard? Name one number you'd change to make it more fun." (We act on this in L02-40.)
Heads up for next class: SCR-L02-40 · Levelling Up Speed makes the game get faster the longer you survive. We use a fall-speed variable that climbs as the score grows, feeding back into how fast the kuih drop. Bring this project.