- 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 harder.
- One spawner stack per hole, so many moles are up at once.
wait until <not <mouse down?>>makes one press count once.- Accuracy is
((score) / (hit)) * (100).<> 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... That is hard to read. Today you learn to make it easy to 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 stops starting the game
- 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 old clones, so a second round is as fast as the first.
Learnt — finishing properly 27 min
A number a person can read
round () gets rid of the decimals. 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 sequence. That means steps in order. 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.
If it all happens at once, the box covers the win or lose message. Broadcasts make things happen in turn.
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 no longer starts the game. It sends a message instead. Everything listens for the message:
when flag clicked
broadcast [Play v]
Every other when flag clicked in your game changes. The hammer, holes, moles and referee all use when I receive [Play v].
Now the Retry button is three blocks. It sends the same message:
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 you do not delete the old ones, they keep on running.
Round one is fine. In round three, moles from three rounds all pop up. Your computer gets very slow.
when I receive [Over v]
delete this clone
Numbers in your own digits
Scratch’s monitor is a grey box. It does not match your game. You can draw your own numbers.
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%”. That is “8”. It is also 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. The playing stays the same. The way it starts changes.
Step 1 — the accuracy variable
Make a variable called accuracy. Make it empty at the start of a round.
Step 2 — the box
Add a sprite for the result box. Hide it at the start. Give it a when I receive [Statistic v] stack. The stack works out the accuracy with round and join. Then it goes to the front and shows it.
Step 3 — the sequence
On the referee, after win or lose, add 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 work just like before.
Step 5 — the Retry button
Add a button sprite. Hide it at the start. It shows on when I receive [Retry v]. 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. Put delete this clone in it.
Step 7 — play five rounds
Click Retry five times in a row. The fifth round must feel just 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 stops the button’s script too.
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. 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
Show the player their accuracy at the end of each round. Do it if they win or lose.
- 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. Click it to start a new round.
- 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
This is the main mission. Stop using Scratch’s grey monitor for the result.
- Paint one sprite with costumes 0–9 and %. Name each one 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%. That is 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 front page with a title and a button. The flag shows the title screen and waits. The game starts only when the player clicks.
Summary 5 min
round ()drops the decimals.join () ()sticks things together. Together they make a result easy to read.- 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]. Then the flag is just one thing that can start the game. - Delete your clones when a round ends. If not, the next round runs on top of the last one.
letter (1) of (accuracy)picks one character. Use it to pick the costume with that name.
- round
- Turns 88.888 into 89. That is the nearest whole number.
- join
- Sticks two pieces of text together. The result is text, not a number.
- Sequence
- Things that happen one after another. Each one calls the next.
- Clean-up
- Deleting what a round left behind. Then the next round starts fresh.
Hand in
Share your project and send the link to your teacher. Next lesson finishes Whack-a-mole.