Learning Goals 3 min
By the end of this lesson you will be able to:
- Build a complete hide-and-seek game from scratch using two sprites, an if/else-if chain, and a brand-new round reporter — distance to [ v] from the Sensing palette.
- Chain three if <> then else blocks into a "ladder" that classifies the player's distance into HOT (within 50 pixels), warm (within 150), or cold (anything else).
- Explain why the Target sprite uses go to x: ( ) y: ( ) with pick random ( ) to ( ) + hide at the start, and why all the "Hot/Cold" logic lives on the Seeker, not the Target.
Warm-Up — the playground game 7 min
Priya hides a kuih bahulu somewhere in the school garden. Daniel walks around looking for it. As he wanders, Priya calls out:
- "HOT!" — when Daniel is very close (within an arm's length).
- "warm" — when Daniel is in the same area (a few steps away).
- "cold" — when Daniel is far away.
The kuih never moves. Daniel never sees it. The only information he has is Priya's three words. From that, he can find anything.
Reveal the trick behind it
The game is a tiny computer in disguise. Priya is measuring distance between Daniel and the kuih, and using three thresholds to classify the answer. Today we are going to build exactly that game in Scratch — and the "Priya" role is just a chain of if-then-else blocks.
Today's lesson is a project lesson. We will use everything from cluster B — if-then, if-then-else, comparison operators, and the and/or logic from yesterday — to build a real, playable hide-and-seek game. By the end of class you'll have something you can show your friend over teh tarik.
New Concept — distance, thresholds, and ladders 15 min
This is a project lesson, so most of what's new is how to combine what you know. But two genuinely new things appear today: the distance reporter and the if/else-if ladder.
The distance reporter
Open the light-blue Sensing palette. Scroll down. You will see a round (oval) reporter that says distance to [ v]. It has a dropdown. Click the dropdown — you'll see mouse-pointer and the names of every other sprite in your project.
(distance to [Target v])
This block reports a number — for example, 23, or 147.8, or 320. The number is how far apart the two sprites are, measured in pixels. The Scratch Stage is 480 pixels wide and 360 pixels tall, so the biggest distance possible is roughly 600 pixels (corner to corner).
Important: distance is a round reporter (a number), not a hexagon. To use it as a yes/no question we have to wrap it in a comparison, like () < ().
Why thresholds turn numbers into words
We don't want the game to say a number — that would be too easy. We want it to say a word — HOT, warm, or cold. So we pick two threshold numbers — 50 and 150 — and split the number line into three regions:
- distance < 50 → HOT
- 50 ≤ distance < 150 → warm
- distance ≥ 150 → cold
The if/else-if ladder
To express the three regions in Scratch we chain if-then-else blocks. The "else" slot of the first if-then-else holds the next if-then-else. The shape looks like a ladder:
if <(distance to [Target v]) < (50)> then
say [HOT!] for (0.5) seconds
else
if <(distance to [Target v]) < (150)> then
say [warm] for (0.5) seconds
else
say [cold] for (0.5) seconds
end
end
Read the ladder top-to-bottom. The first question runs. If it's true, we say HOT and we're done (Scratch skips the else). If it's false, we drop into the else and hit the second if-then-else. That one asks "is the distance < 150?". If yes → warm. If no → cold (which is the final else, the catch-all).
This pattern — first true wins — is one of the most important shapes in all of programming. You will use it every time you classify a number into named buckets: ages into "child/teen/adult", scores into "F/D/C/B/A", weather into "hot/warm/cold".
Hiding the Target
The Target sprite needs to be invisible during play — that's the whole point of hide-and-seek. We use:
when flag clicked
go to x: (pick random (-200) to (200)) y: (pick random (-150) to (150))
hide
Notice we use a round Operators block — pick random ( ) to ( ) — nested inside the x and y slots of go to x: ( ) y: ( ). Round inside round. The Stage runs from x: −240 to 240 and y: −180 to 180, but we shrink the range a bit so the Target never sits flush against an edge.
Worked Example — building the game from empty Stage to playable 12 min
Open Scratch. We'll build the whole game in ten steps. Two sprites, two scripts, one if/else-if ladder.
Step 1 — Start fresh and rename
New project. Click the cat sprite and rename it to Seeker (right-click the sprite tile, choose "info", change the name field). The Seeker is what the player controls.
Step 2 — Add the Target sprite
Click the "Choose a Sprite" button (the cat-and-plus icon, bottom-right of the sprite pane). Pick any sprite — a Star or a Ball works well. Rename it to Target.
Step 3 — Target script: random spot, then hide
Click the Target sprite. In the code area:
when flag clicked
go to x: (pick random (-200) to (200)) y: (pick random (-150) to (150))
hide
Step 4 — Seeker setup: show and follow the mouse
Click the Seeker sprite. Start with a hat and make it follow the mouse pointer forever:
when flag clicked
show
forever
go to [mouse-pointer v]
end
Step 5 — Test movement
Click the flag. The Seeker should follow your mouse smoothly. The Target should be invisible (it ran its hide once and stayed gone). Good — basic structure works.
Step 6 — Add the HOT check
Inside the Seeker's forever, after the go-to: if <> then else. In the diamond drop () < () with distance to [Target v] on the left and 50 on the right. Inside the if-body: say [HOT!] for (0.5) seconds.
Step 7 — Add the warm check inside the else
Drag another if <> then else into the else slot of the first one. In its diamond: another () < () with distance to [Target v] on the left and 150 on the right. Inside the new if-body: say [warm] for (0.5) seconds.
Step 8 — Add the cold catch-all
In the inner else slot, drop say [cold] for (0.5) seconds. That's the catch-all: if we got here, the Seeker is not within 50 AND not within 150 — so cold is the answer.
Step 9 — Test the game
Click the flag. Move your mouse around. Watch the Seeker's speech bubble flip between HOT, warm, and cold. Find the invisible Target by walking the bubble up the temperature scale. When you reach HOT, you've found it.
Step 10 — The full assembled Seeker stack
when flag clicked
show
forever
go to [mouse-pointer v]
if <(distance to [Target v]) < (50)> then
say [HOT!] for (0.5) seconds
else
if <(distance to [Target v]) < (150)> then
say [warm] for (0.5) seconds
else
say [cold] for (0.5) seconds
end
end
end
What you just built: a real game — invisible target, mouse-controlled seeker, three-state feedback. It uses two sprites, a brand-new sensing reporter, an if/else-if ladder, and a comparison from L02-08. Show it to your sibling. Watch them play it. That is what a project lesson is for.
Try It Yourself — three Hot-or-Cold extensions 15 min
Goal: Add a fourth tier. Insert "boiling!" before HOT for distances under 20 pixels. Your ladder now has four steps: boiling (< 20), HOT (< 50), warm (< 150), cold (else). Be careful — the narrowest threshold goes first.
if <(distance to [Target v]) < (20)> then
say [boiling!] for (0.5) seconds
else
if <(distance to [Target v]) < (50)> then
say [HOT!] for (0.5) seconds
else
if <(distance to [Target v]) < (150)> then
say [warm] for (0.5) seconds
else
say [cold] for (0.5) seconds
end
end
end
Think: Why does the ladder always go narrowest-first? Because the first true rung wins. If you put "< 150" first, distances under 20 still match it and never reach "boiling".
Goal: Add a win condition. When the Seeker is within 20 pixels of the Target, broadcast a win — for now, just make the Target show itself with a victory pose. Add a second small script on the Target.
when flag clicked
forever
if <(distance to [Seeker v]) < (20)> then
show
say [Found me!] for (2) seconds
stop [all v]
end
end
Think: You could also put this on the Seeker. Either side works because distance is symmetric — distance to [Target v] on the Seeker gives the same number as distance to [Seeker v] on the Target. Pick whichever sprite makes the code easier to read.
Goal: Add a timer using cluster-B logic. Make the Seeker also check whether the player has taken too long. If the distance is still > 100 AND the built-in timer is more than 15 seconds, say Time's almost up!. Use the <> and <> hexagon you met yesterday.
when flag clicked
reset timer
show
forever
go to [mouse-pointer v]
if <<(distance to [Target v]) > (100)> and <(timer) > (15)>> then
say [Time's almost up!] for (0.5) seconds
else
if <(distance to [Target v]) < (50)> then
say [HOT!] for (0.5) seconds
else
if <(distance to [Target v]) < (150)> then
say [warm] for (0.5) seconds
else
say [cold] for (0.5) seconds
end
end
end
end
Think: The and hexagon from L02-10 lets you ask two questions at once — "are you still far?" AND "have you been searching too long?". Either side alone wouldn't be a useful warning. timer is a round reporter that gives seconds since the project started (or since reset).
Mini-Challenge — Aljay's "always cold" bug 5 min
"The game that never gets hot"
Aljay builds the Hot-or-Cold game, but no matter where he moves the mouse, the Seeker only ever says "cold". Never warm. Never HOT. Here's his Seeker script:
when flag clicked
show
forever
go to [mouse-pointer v]
if <(distance to [Target v]) > (50)> then
say [HOT!] for (0.5) seconds
else
if <(distance to [Target v]) > (150)> then
say [warm] for (0.5) seconds
else
say [cold] for (0.5) seconds
end
end
end
Read the ladder out loud. What did Aljay get wrong? And why does it always end up saying "cold"?
Reveal the fix
Aljay used () > () (greater than) instead of () < () (less than). His ladder reads "if distance is greater than 50, say HOT" — which is the opposite of what HOT means. HOT should fire when the Seeker is close (small distance), not far (big distance).
Even worse: his second rung says "if greater than 150, say warm". For any mouse position close to the Target, the distance is small (say, 10), so 10 is NOT > 50 — the first rung is false. Then 10 is NOT > 150 — the second rung is false. So the else fires and the cat says "cold" even when the mouse is sitting on top of the Target.
Fix: flip both operators from > to <.
when flag clicked
show
forever
go to [mouse-pointer v]
if <(distance to [Target v]) < (50)> then
say [HOT!] for (0.5) seconds
else
if <(distance to [Target v]) < (150)> then
say [warm] for (0.5) seconds
else
say [cold] for (0.5) seconds
end
end
end
Same blocks. One symbol flipped on each comparison. Lesson: comparison operators are easy to mis-read out loud. Always test your ladder by walking through one example value in your head — "distance is 30, what does my ladder do?".
Recap 3 min
You built a complete two-sprite game by combining everything from cluster B. The Target jumps to a random hidden spot at the start of every round. The Seeker follows the mouse and uses an if/else-if ladder to classify distance to [Target v] into HOT (< 50), warm (< 150), or cold. The ladder shape — first true wins — is one of the most-used patterns in programming. You also met your first instance of distance as a round reporter, which you'll use again in L02-15 when sprites start chasing each other automatically.
- Threshold
- A boundary number that splits a continuous value (like distance) into named regions. The Hot-or-Cold game uses thresholds at 50 and 150.
- If/else-if ladder
- A chain of if <> then else blocks where each else slot holds the next if-then-else. The first true rung wins. The final else is the catch-all.
- Distance reporter
- distance to [ v]. A round (number) reporter from Sensing. Reports the pixel distance from this sprite to whatever you pick in the dropdown.
- Pixel
- A tiny dot on the screen. Scratch's Stage is 480 pixels wide and 360 tall, so a "50-pixel distance" is roughly a tenth of the Stage's width.
- Catch-all
- The final else in a ladder. Runs when none of the previous rungs were true. Every ladder needs one so there is always a defined outcome.
- Project (lesson type)
- A lesson where the whole hour goes toward building one finished, playable thing. You learn very little new theory; you practise putting old theory together.
Homework 2 min
Polish your Hot-or-Cold. Take the game you built in class and add three small improvements at home.
- Open the project from class. Save a copy as
HW-L2-11-Hot-or-Cold.sb3so the original stays safe. - Pick a Backdrop that fits the theme — a garden, a beach, anything visually busy enough that the Seeker doesn't blend in.
- Add a score variable (just guess at Make a Variable if you haven't met it formally — the trick is in the orange Variables palette). Each time the Target is found, add 1 to the score, then re-hide the Target at a new random spot so the game can keep going.
- Add the timer-warning and hexagon from the Stretch task, so the game nudges players who take too long.
- Show it to one family member. Watch them play one round without explanation. Note one thing that confused them.
Bring back next class:
- The
.sb3file with all three improvements. - The one thing your family member found confusing — we'll talk about how to communicate game rules in L02-13.
- Your answer to: "Why is putting the Hot/Cold ladder on the Seeker easier than putting it on the Target? Could you build it either way?"
Heads up for next class: SCR-L02-12 opens cluster C with touching [Sprite v]? — a sibling of the edge sensor that asks "am I touching that other sprite right now?". Today we used distance; next time we'll use contact. Different question, same hexagonal shape.