Learning Goals 3 min
By the end of this lesson you will be able to:
- Paint a coloured goal square on the backdrop and use touching color [#00ff00] ? as a win trigger.
- Use a broadcast + switch backdrop + stop [all v] chain to end the game cleanly.
- Track a multi-stage win with a keys-found list — collect three coloured keys before the goal counts.
Warm-Up — the maze with no finish line 7 min
Last lesson the player glided perfectly through the maze. But there's still something missing — something every game in the world has but yours doesn't.
when flag clicked
set [move-speed v] to (3)
go to x: (-200) y: (-150)
forever
set [last-good-x v] to (x position)
set [last-good-y v] to (y position)
if <key [right arrow v] pressed?> then
change x by (move-speed)
end
if <touching color [#000000] ?> then
set x to (last-good-x)
set y to (last-good-y)
end
end
Predict: a friend opens your maze and plays for 30 seconds. What's their first question?
Reveal the answer
"Cool, so… what do I do? When have I won? Do I just walk around forever?" A maze with no goal is a screensaver. The player needs a place to reach (a goal), a signal that they made it (a win message), and an end (the game stops). Today we add all three.
Predict: if you just dropped a green goal square at the end of the maze, what's the shortest script you could add to detect "the player reached it"?
Reveal the answer
One if <> then inside the forever, with touching color [#00ff00] ? in the diamond. That's it. The same touching-colour pattern you already use for walls works for goals — just a different colour.
New Concept — the goal, the broadcast, the stop 15 min
A win condition has three parts. We'll build each one as its own block, then snap them together.
Part 1 — the goal pixel
On the Backdrop, pick the rectangle tool, choose a colour the maze doesn't already use (bright green #00ff00 is the standard "this is the goal" colour in retro games), and paint a small square somewhere reachable inside the maze.
Critical: when you go to write the touching-colour check, click the colour swatch in the block and use the eyedropper to sample the green directly off the Stage. Don't try to type the hex by hand. If your green is #00ff00 but the block holds #00fe00, the check never fires.
Part 2 — the detection
Inside the player's forever loop, add one more if-then:
if <touching color [#00ff00] ?> then
broadcast (win v)
end
Part 3 — the reaction
Add a new sprite-wide hat on the Stage (or any sprite, but the Stage is the natural home for backdrop switches):
when I receive (win v)
switch backdrop to [You Win v]
stop [all v]
Why a broadcast instead of putting switch backdrop directly in the player's if-then? Because more than one sprite needs to react to the win — the music sprite stops, the enemy sprites freeze, the backdrop changes. A broadcast is a radio call: any sprite that's listening reacts. It scales as your game grows.
Multi-stage wins with lists
"Walk to the green square" is the simplest win. A more interesting game asks: collect three keys, then reach the goal. Use a list to track progress.
Make a list called keys-found. On the player, add one if-then per key colour:
forever
if <touching color [#ffff00] ?> then
add [yellow] to [keys-found v]
broadcast (yellow-collected v)
end
if <touching color [#ff00ff] ?> then
add [pink] to [keys-found v]
broadcast (pink-collected v)
end
if <touching color [#00ff00] ?> then
if <(length of [keys-found v]) = (3)> then
broadcast (win v)
end
end
end
Each key sprite hides itself when its broadcast fires (when I receive [yellow-collected v] → hide) — that way the player can't re-touch a collected key and add "yellow" to the list twice.
Worked Example — the green-square finish 12 min
Open the L03-34 glider project. We'll add the simplest possible win — reach the green square, see "You Win", game over — in eight steps.
Step 1 — Paint the goal
Click the backdrop in the sprite panel. Pick the rectangle tool. Set fill to bright green (#00ff00). Paint a square about 30×30 in the upper-right of the maze, somewhere reachable but not too easy.
Step 2 — Make the "You Win" backdrop
Backdrops tab → choose a new backdrop (or paint a fresh one). Add a big "You Win!" text in the centre, maybe with some confetti emojis you draw. Rename it to You Win (lowercase "you win" works too — just match the case in the block dropdown).
Step 3 — Open the player sprite
Click the player sprite. Find the big forever loop from L03-34. We'll add to it.
Step 4 — Add the goal-detection if-then
Inside the forever, after the wall-bump rollback, drop one more if <> then. Diamond gets touching color [#00ff00] ? — use the eyedropper on your green goal square.
Step 5 — Broadcast "win"
Inside the new if-then: broadcast (win v). Click the dropdown → New message → name it win.
Step 6 — Switch to the Stage
Click the Stage in the sprite panel. New script.
Step 7 — Receive the broadcast
From Events: when I receive (win v). From Looks: switch backdrop to [You Win v].
Step 8 — Stop everything
From Control: stop [all v]. Snap it under the switch-backdrop. Done.
Click the flag. Drive the player to the green square. The backdrop should flip and the player should freeze mid-stride.
The full assembled stacks
when flag clicked
go to x: (-200) y: (-150)
forever
set [last-good-x v] to (x position)
set [last-good-y v] to (y position)
if <key [right arrow v] pressed?> then
change x by (move-speed)
end
if <key [up arrow v] pressed?> then
change y by (move-speed)
end
if <touching color [#000000] ?> then
set x to (last-good-x)
set y to (last-good-y)
end
if <touching color [#00ff00] ?> then
broadcast (win v)
end
end
when I receive (win v)
switch backdrop to [You Win v]
stop [all v]
What you just built: a real game. A start state, a player, a goal, a victory. From here, everything else (lives, score, multiple levels) is decoration. The skeleton is complete.
Try It Yourself — three goal drills 15 min
Goal: Add a delay before the win backdrop appears. The player touches green, you hear a "tada" sound for one second, then the backdrop switches and everything stops.
when I receive (win v)
play sound [Win v] until done
switch backdrop to [You Win v]
stop [all v]
Think: Why play sound until done and not just play sound? Because stop [all v] happens immediately on the next block — without "until done", you'd hear the first 30 milliseconds of the sound and then silence.
Goal: Add a time-taken variable. Start it at 0 when the flag is clicked, count up every second in a separate loop. When the player wins, broadcast "win" but also have the Stage say "You won in (time-taken) seconds!" before switching backdrop.
when flag clicked
set [time-taken v] to (0)
forever
wait (1) seconds
change [time-taken v] by (1)
end
Think: If the maze is for ages 9–13 and the average kid takes 45 seconds, that's a satisfying number. Save the timer's "best time so far" in a second variable for replay value.
Goal: Three keys must be collected before the goal counts. Paint a yellow key, a pink key, and a cyan key as three small sprites inside the maze. The player touches each one to "collect" it (broadcast, the key hides, add the colour to the keys-found list). The green goal only wins if the list is length 3.
when flag clicked
delete all of [keys-found v]
forever
if <touching color [#ffff00] ?> then
if <not <[keys-found v] contains [yellow] ?>> then
add [yellow] to [keys-found v]
broadcast (got-yellow v)
end
end
if <touching color [#00ff00] ?> then
if <(length of [keys-found v]) = (3)> then
broadcast (win v)
end
end
end
Think: You've just designed a tiny Zelda-style game. The keys-as-list pattern scales to any number of collectibles — try 10 keys with the goal needing all 10, or only requiring 5 of any colour.
Mini-Challenge — the flickering finish 5 min
"Aaron's flashing You-Win"
Aaron built the basic green-square finish. It almost works — but when the player touches the goal, the "You Win" backdrop flashes on and off rapidly, the player keeps moving, and the cat ends up halfway off the screen. He shows you this:
when flag clicked
forever
if <touching color [#00ff00] ?> then
broadcast (win v)
end
end
when I receive (win v)
switch backdrop to [You Win v]
The win broadcast fires every frame the player is touching green — that's many times a second. What's missing, and how do you fix the flicker?
Reveal one valid solution
Two bugs, one fix. Aaron forgot stop [all v] at the end of the Stage script. Without it, the forever loop on the player keeps re-broadcasting "win" every frame, and Scratch dutifully switches the backdrop again and again — which on slow systems looks like a flicker, and on fast systems wastes CPU.
when I receive (win v)
switch backdrop to [You Win v]
stop [all v]
stop [all v] halts every script in the project — the player's forever, the timer's forever, every enemy patrol. The win backdrop sits there, calm and final. Stopping is part of winning.
Recap 3 min
You turned a maze into a game by adding a goal, a broadcast, and a stop. The player detects the cheese pixel with the same touching color pattern used for walls, then fires a broadcast that the Stage (and any other sprite) listens for. The Stage switches to a "You Win" backdrop and calls stop [all v] to freeze every script in the project. The maze finally has a finish line.
- Win condition
- The rule that decides "the player has won". Always a boolean — touching a colour, list reaching a length, score above a number, etc.
- Broadcast / receive
- Scratch's message system. One sprite shouts; every sprite that's listening reacts. The cleanest way to fan one event out to many sprites.
- stop [all v]
- The Control block that halts every script in the project. Essential for any game with a definite end — without it, the win event fires forever and the game keeps running underneath.
- Multi-stage win
- A win condition that needs several smaller goals first. Implemented with a list or counter that the final check inspects before broadcasting "win".
- Goal pixel
- A specific colour painted on the backdrop (or a sprite) that the player must touch. Always sample with the eyedropper to guarantee an exact match.
Homework 2 min
The Three-Key Quest. Extend your maze into a real mini-game.
- Open
HW-L3-34-Glider.sb3. Save it asHW-L3-35-Quest.sb3. - Paint a small green square in the hardest-to-reach corner of the maze as the final goal.
- Create three key sprites (paint a small yellow key, pink key, cyan key) and drop each one somewhere in the maze.
- Create a list keys-found. Show it on the Stage so the player can see their progress.
- Player script: each coloured key adds to the list and broadcasts a "got-X" message. The green-goal check only broadcasts "win" if the list length is 3.
- Key scripts: each key hides on its matching broadcast.
- Stage: at when ⚑ clicked, delete all of [keys-found v] and switch to the maze backdrop. On when I receive (win v), switch to "You Win" and stop [all v].
Bring back next class:
- The
.sb3file. - One sentence answering: "What happens if you forget the delete all of [keys-found v] on flag-click? Try it."
- One sentence answering: "Why does the win logic live on the player and the backdrop-switch live on the Stage?"
Heads up for next class: SCR-L03-36 adds enemies — patrol sprites that walk the maze using the same wall-detection pattern, and a lives system so the player can die (and respawn) instead of just winning.