- Learnt27 min · round and join, the Play broadcast, clearing up, your own digits
- Basic25 min · make the game restartable
- Challenge 1–327 min · the box, the retry, your own digits
- Extrabonus · a title screen
Review — where we got to 5 min
Last lesson the board got busy and the game got fussy.
- One spawner stack per hole, so several moles are up at once.
wait until <not <mouse down?>>makes one press count once.- Accuracy is
((score) / (hit)) * (100), and<> and <>needs both halves true.
Quick-fire
You scored 41 from 47 swings. What does the accuracy sum give you?
Reveal the answer
87.234042553... — and that is a problem. Today you learn to turn it into something a person can read.
Today’s Topic 3 min
- Showing a result the player can read
- The order things happen in when a game ends
- A Retry button, and why the green flag has to step aside
- Clearing up your clones
- Numbers drawn in your own digits
Learning outcome
By the end of this lesson you will be able to:
- Round a number and join words onto it.
- Start a whole game from a broadcast so anything can restart it.
- Delete leftover clones so a second round is as fast as the first.
Learnt — finishing properly 27 min
A number a person can read
round () throws the decimals away. join () () sticks two things together. Together they turn 88.888888 into 89%.
set [accuracy v] to (join (round (((score) / (hit)) * (100))) [%])
One thing at a time
The end of a round is a little sequence, and each step calls the next:
- The clock reaches zero.
- Broadcast Over — everything stops making moles.
- The verdict appears: you win, or you lose.
- Broadcast Statistic — the accuracy box shows the number.
- Broadcast Retry — the button appears.
Do it all at once and the box lands on top of the verdict. Broadcasts are how you make things queue up.
Why the green flag has to step aside
You want a Retry button. But a button cannot press the green flag — nothing in Scratch can.
So the flag stops being the thing that starts the game. It shouts a word instead, and everything listens for the word:
when flag clicked
broadcast [Play v]
Every other when flag clicked in your game — hammer, holes, moles, referee, everything — becomes when I receive [Play v].
And now the Retry button is three blocks, because it can shout the same word:
when this sprite clicked
broadcast [Play v]
Clear up your clones
Here is a bug that only shows up on the third go. Every round makes moles; if the old ones are never deleted, they keep running underneath the new ones.
Round one is fine. Round three has three rounds’ worth of moles all popping up, and your computer starts to struggle.
when I receive [Over v]
delete this clone
Numbers in your own digits
Scratch’s monitor is a grey box that does not match your game. You can draw your own numbers instead.
Make one sprite with eleven costumes: 0, 1, 2 … 9 and %. Name each costume after what it shows — the costume for eight is called 8.
Then letter (1) of (accuracy) gives you the first character of “87%”, which is “8” — and that is the name of a costume.
switch costume to (letter (1) of (accuracy))
go to x: (-30) y: (0)
create clone of [myself v]
switch costume to (letter (2) of (accuracy))
go to x: (30) y: (0)
create clone of [myself v]
Basic — make it restartable 25 min
Open your Lesson 2 game. Nothing about the playing changes; everything about the starting does.
Step 1 — the accuracy variable
Make a variable called accuracy. Set it to nothing at the start of a round.
Step 2 — the box
Add a sprite for the result box, hidden at the start. Give it a when I receive [Statistic v] stack that works out the accuracy with round and join, goes to the front, and shows it.
Step 3 — the sequence
On the referee: after the verdict, broadcast [Statistic v]. Take out the stop [all v] — the round is not over until the button is up.
Step 4 — Play instead of the flag
Change every when flag clicked in your project to when I receive [Play v]. Then add one new stack, anywhere: when flag clicked then broadcast [Play v].
Test now. The game should behave exactly as it did before.
Step 5 — the Retry button
Add a button sprite, hidden at the start. It shows on when I receive [Retry v], and its when this sprite clicked stack broadcasts Play.
Step 6 — clear up
Give the mole and the hole a when I receive [Over v] stack with delete this clone.
Step 7 — play five rounds
Click Retry five times in a row. The fifth round must feel exactly like the first. If it is slower, a clone is not being deleted.
Clicking Retry does nothing.
Something is still stopping everything. Find your stop [all v] and take it out — it kills the button’s script along with the rest.
The second round starts with the old score.
The set [score v] to (0) is on a when flag clicked stack you forgot to change. It has to be on when I receive [Play v] like everything else.
Moles appear twice as fast after a retry.
The spawner stacks are still running from the last round. They are on when I receive [Play v], so a second Play starts a second copy of each. Add stop [other scripts in sprite v] at the top of the mole’s Play stack.
Show the accuracy
At the end of every round — win or lose — show the player how accurate they were.
- A whole number, not a long decimal.
- With a % after it.
- After the verdict, not on top of it.
Play again
- Every start block becomes
when I receive [Play v]. - A Retry button appears at the end and starts a fresh round when clicked.
- Every clone is deleted when the round ends.
Teacher note
The clone leak is the valuable part. Ask the class to skip the delete on purpose and retry six or seven times — the game visibly degrades, and they can see why. It is probably the first time they have met a bug that only appears with use rather than immediately.
Your own digits
The deck’s mission. Stop using Scratch’s grey monitor for the result.
- Paint one sprite with costumes 0–9 and %, each named after what it shows.
- Show the accuracy in those digits at the end of the round.
- It must look right at 9%, 87% and 100% — two, three and four characters.
- Show the score the same way.
Teacher reveal — counting the characters
There are only three cases, so it is an if / else inside an if / else:
if <(accuracy) < (10)> then
say [two characters: the digit and the %]
else
if <(accuracy) < (100)> then
say [three characters]
else
say [four characters]
end
end
A pupil who finds length of () in Operators has found the better answer — one number instead of three cases — and it deserves saying so.
Watch for the accuracy variable being text by this point. If they joined the % on before comparing, <() < ()> will behave oddly. Two variables — the number and the text — is the clean fix, and it is a real lesson about keeping the two apart.
A title screen
You have already done the hard part. Everything starts on Play — so nothing has to start when the flag is clicked.
Give your game a proper front page: a title, and a button. The flag shows the title screen and waits. The game begins only when the player says so.
Summary 5 min
round ()drops the decimals;join () ()sticks things together. Together they make a result readable.- The end of a game is a sequence. Each step broadcasts the next, so things happen one at a time.
- Move every start onto
when I receive [Play v]and the flag becomes just one of the things that can start the game. - Delete your clones when a round ends, or the next round runs on top of the last one.
letter (1) of (accuracy)picks one character, and a costume named after that character can be chosen with it.
- round
- Turns 88.888 into 89 — the nearest whole number.
- join
- Sticks two pieces of text together. The result is text, not a number.
- Sequence
- Things happening one after another, each one calling the next.
- Clean-up
- Deleting what a round left behind, so the next round starts fresh.
Hand in
Share your project and send the link to your teacher. Next lesson finishes Whack-a-mole.