Learning Goals 3 min
By the end of this lesson you will be able to:
- Combine pick random with go to x: () y: () so a sprite teleports to one of several positions every loop tick.
- Use the when this sprite clicked hat together with a score variable to count successful taps.
- End a game cleanly using the timer reporter inside an if <> then + stop [all v].
Warm-Up — predict the tikus 7 min
A tikus is a mouse or rat in Malay. We're about to build a Malaysian version of the old "Whack-a-Mole" arcade game — but first, two predictions to get your brain warm.
Prediction 1. Aisyah writes this script on a tikus sprite that starts at x: 0, y: 0:
when flag clicked
forever
go to x: (pick random (-150) to (150)) y: (pick random (-100) to (100))
wait (1) seconds
end
What does the tikus look like on stage when Aisyah hits the flag?
Reveal the answer
The tikus jumps to a random spot, sits still for 1 second, jumps to another random spot, sits still for 1 second — forever. It looks teleporty, not whack-able. There's nothing to react to yet: the tikus is always visible, so there's no moment where you must hit fast.
Prediction 2. Daniel adds two blocks to make it whack-able:
when flag clicked
forever
go to x: (pick random (-150) to (150)) y: (pick random (-100) to (100))
show
wait (0.8) seconds
hide
wait (1) seconds
end
Now what changes for the player?
Reveal the answer
The tikus pops up at a random spot, dares you to whack it for less than a second, then vanishes and re-rolls. That's a game. All that's missing is a way to count whacks and a way to end after 30 seconds — and that's the whole lesson.
New Concept — three habits of a tiny game 15 min
Every reaction game you'll ever build follows the same three habits. Whack-a-Tikus is small enough that we can see all three habits in one screen of blocks.
Habit 1 — the sprite has a loop of its own
The tikus sprite isn't standing still waiting for instructions. It has its own forever loop that picks a position, shows, waits, hides. The player isn't in charge of when the tikus appears — the tikus is. That's what makes the game feel alive.
when flag clicked
hide
forever
go to x: (pick random (-150) to (150)) y: (pick random (-100) to (100))
show
wait (0.8) seconds
hide
wait (1) seconds
end
Habit 2 — clicking adds to a score
A second script on the same tikus sprite listens for clicks. When the tikus is clicked, change [score v] by (1) and hide immediately so the player can't whack the same tikus twice.
when this sprite clicked
change [score v] by (1)
hide
Two important details here:
- when this sprite clicked is a hat — it starts its own little script, separate from the forever loop. Scratch runs the two scripts side by side.
- The score variable is a For all sprites variable (the default). Create it once from the Variables palette and every sprite can read it.
Habit 3 — the game ends when a timer says so
Without an ending, the tikus pops up forever and nobody knows their "real" score. Scratch has a built-in timer reporter that counts seconds since the flag was clicked. Compare it to 30 and stop everything when it's bigger:
when flag clicked
reset timer
forever
if <(timer) > (30)> then
stop [all v]
end
end
Worked Example — building Whack-a-Tikus 15 min
Open Scratch. We'll build the whole game in ten steps. Each step is one or two blocks.
Step 1 — Start a new project and rename the sprite
New project. Delete the cat. From the sprite library pick any small animal — Mouse1 works perfectly. Rename the sprite to Tikus in the sprite info area.
Step 2 — Create the score variable
Variables palette → Make a Variable → name it score, leave "For all sprites" selected. Tick its checkbox so the score shows on stage.
Step 3 — Set up the start
On the tikus sprite, drag in this opening:
when flag clicked
set [score v] to (0)
reset timer
hide
Step 4 — Add the forever loop
Snap a forever under the four blocks from Step 3. The C opens, ready for the pop-up routine.
Step 5 — Pick a random hole
Inside the forever, drop go to x: () y: (). In the x slot drop pick random (-150) to (150). In the y slot drop another pick random (-100) to (100).
Step 6 — Show, wait, hide
Under the go-to, add show, then wait (0.8) seconds, then hide, then wait (1) seconds. That's one full pop-up cycle.
Step 7 — Check your first script
Click the flag. The tikus should be bouncing around the stage, appearing for 0.8s and disappearing for 1s. You can't whack it yet — but it's alive.
Step 8 — Add the click-to-score script
Still on the tikus sprite, drag a fresh when this sprite clicked hat into empty space (not under the forever). Add change [score v] by (1) and then hide.
when this sprite clicked
change [score v] by (1)
hide
Step 9 — Add the referee
Click the Stage in the sprite list (not the tikus). In the Stage scripting area drop this:
when flag clicked
forever
if <(timer) > (30)> then
say [Time's up!] for (2) seconds
stop [all v]
end
end
Quick fix: leave the say out of the Stage's script (the Stage has no costume to speak from). Put a tiny "Game Over" sprite at the centre with its own when I receive (game-over v) if you want a message — but that's a stretch.
Step 10 — The final referee script
when flag clicked
forever
if <(timer) > (30)> then
stop [all v]
end
end
The full assembled game
You now have three scripts across two scripting areas. Here they are together:
when flag clicked
set [score v] to (0)
reset timer
hide
forever
go to x: (pick random (-150) to (150)) y: (pick random (-100) to (100))
show
wait (0.8) seconds
hide
wait (1) seconds
end
when this sprite clicked
change [score v] by (1)
hide
when flag clicked
forever
if <(timer) > (30)> then
stop [all v]
end
end
Click the flag. Whack tikuses for 30 seconds. Check your score. That's a game.
Try It Yourself — three extensions 12 min
Don't start from scratch — keep working on your Whack-a-Tikus project. Each task adds one feature.
Goal: Add a play sound [pop v] the moment the player whacks the tikus. Pick any short sound from the Sounds tab.
when this sprite clicked
play sound [pop v] until done
change [score v] by (1)
hide
Think: Sound is feedback. Players whack faster when they hear a satisfying pop!, even though nothing about the score has changed.
Goal: Make the tikus appear faster and faster as the game goes on. After 20 seconds the tikus should only show for 0.5 seconds (not 0.8).
when flag clicked
set [score v] to (0)
reset timer
hide
forever
go to x: (pick random (-150) to (150)) y: (pick random (-100) to (100))
show
if <(timer) > (20)> then
wait (0.5) seconds
else
wait (0.8) seconds
end
hide
wait (1) seconds
end
Think: Difficulty curves keep players engaged. Easy at the start, hard at the end. You could even use (0.8) - ((timer) / (60)) for a smooth ramp instead of a sudden step.
Goal: Add a second tikus sprite so there can be up to two tikuses on stage at once. The second tikus should have its own copy of the same two scripts.
when flag clicked
hide
wait (0.5) seconds
forever
go to x: (pick random (-150) to (150)) y: (pick random (-100) to (100))
show
wait (0.8) seconds
hide
wait (1) seconds
end
Think: Right-click the first tikus in the sprite list → Duplicate. Both copies share the same score variable (because we made it "For all sprites"), so whacks from both add to the same score. Priya might add a third — but more than three gets chaotic.
Mini-Challenge — the cheater's score 5 min
"Daniel's suspiciously high score"
Daniel shows you his Whack-a-Tikus and brags about scoring 47 in 30 seconds. You suspect cheating. You look at his click-handler:
when this sprite clicked
change [score v] by (1)
Why is Daniel scoring so high, and what one block fixes it?
Reveal the answer
Daniel's tikus stays visible after a whack. So a player can click-click-click-click on the same tikus while it's still showing, racking up score with one well-timed mouse spam. The fix is the missing hide:
when this sprite clicked
change [score v] by (1)
hide
Now one click = one score, because after the hide, when this sprite clicked won't fire again until the forever loop shows the tikus at the next hole. The hide isn't just visual — it's the game rule.
Recap 3 min
You built a complete reaction game in three small scripts. The tikus sprite ran its own pop-up loop, listened for clicks on itself, and changed a shared score variable. The Stage refereed by watching the built-in timer and pulled the plug at 30 seconds. The same three habits — independent sprite loop, click handler, referee — power almost every Scratch arcade game.
- Pick random
- pick random (-150) to (150) is a round reporter from the Operators palette. It gives a new whole number every time it's read, which is exactly what you want for hole positions.
- When this sprite clicked
- An Events hat that fires only when the sprite's visible costume is clicked. Hidden sprites can't be clicked — that's why hide in the click-handler also acts as anti-cheat.
- Timer
- A built-in Sensing reporter. It counts seconds since Scratch opened, but reset timer sets it back to 0 — usually right after the flag.
- Referee script
- An informal name for a script that doesn't draw anything or react to clicks — it just watches a condition (like the clock) and stops the game when something is true. Often lives on the Stage.
- Stop [all v]
- A Control block that halts every running script in every sprite. The hard cut-off for "game over". Less brutal options: stop [this script v] or stop [other scripts in sprite v].
Homework 2 min
Polish Whack-a-Tikus into a presentable game. Take your in-class build and add these:
- A title screen. Create a "Title" sprite that shows the words Whack-a-Tikus. When clicked, it broadcasts
start, hides itself, and the tikus only begins its forever loop on when I receive (start v). - A countdown on the Stage. Use a second variable called
time-left, set to 30 at flag, and change [time-left v] by (-1) + wait (1) seconds inside a repeat-30 loop. End the game when time-left hits 0 instead of using the timer reporter. - A final score callout. When the game ends, a "GameOver" sprite shows itself and say (join [Final score: ] (score)) for (5) seconds using the join operator.
Save as HW-L2-43-Whack-a-Tikus.sb3. Test by playing twice in a row — both runs should start clean (score 0, full 30 seconds).
Bring back next class:
- The
.sb3file. - Your high score after 5 honest plays.
- Your answer to: "Why did we put the referee on the Stage instead of on the tikus sprite?"
Heads up for next class: SCR-L02-44 builds another small game — a five-question Quiz Show — using ask [] and wait and the answer reporter. Different genre, same three habits.