- Learnt27 min · moles as clones, counting swings, working out accuracy
- Basic25 min · rebuild the game with clones and a hit counter
- Challenge 1–327 min · several at once, count the swings, show it and retry
- Extrabonus · a golden mole
Review — where we got to 5 min
Last lesson the mouse took over.
go to [mouse-pointer v]in a forever glues a sprite to the pointer;mouse down?is the click.go to [front v] layerdecides what covers what.pick random (0) to (1)is a coin toss, and a hidden referee sprite keeps the clock.
Quick-fire
Your mole is up at (132, 86). Can another mole also be up at (0, 0)?
Reveal the answer
No. There is only one mole sprite, and one sprite has one position. That is the limit today removes.
Today’s Topic 3 min
- Moles as clones — one per hole, all running at once
- Counting how often the player swings
- Working out accuracy with a sum
- Two conditions at once with
<> and <>
Learning outcome
By the end of this lesson you will be able to:
- Have several copies of one sprite active in different places at the same time.
- Count a button press once, however long it is held.
- Turn two numbers into a percentage and test it.
Learnt — many moles, and measuring 27 min
The problem with one mole
Your game works, but it is quiet. Only one mole is ever up, because there is only one mole sprite — and a sprite has one position.
You already know the fix. Topic 1.10: when you need many of something, clone it.
Nine holes, nine clocks
Give the mole nine stacks — one per hole. Each waits its own random time, moves to its hole, and makes a clone there.
when flag clicked
forever
wait (pick random (1) to (4.5)) seconds
go to x: (-132) y: (86)
create clone of [myself v]
end
Then one stack that every clone runs when it is born:
when I start as a clone
go to [front v] layer
switch costume to [half out v]
show
wait (0.3) seconds
switch costume to [fully out v]
wait (0.7) seconds
delete this clone
Now the game is too easy
Try it and you will score far more than nine. So the rules change: you now need more than 60 points, and you need to be accurate.
Accuracy means: of all the times you swung, how many actually hit something? Flailing wildly should not win.
Counting the swings
Make a second variable, hit. Every time the player presses the mouse, add one.
There is a trap. A forever checking mouse down? runs dozens of times a second, so holding the button for one second would count as hundreds of hits.
wait until <not <mouse down?>> fixes it: after counting one, the script stops and waits for the button to come back up.
when flag clicked
set [hit v] to (0)
point in direction (90 v)
forever
if <mouse down?> then
point in direction (150 v)
change [hit v] by (1)
wait until <not <mouse down?>>
point in direction (90 v)
end
end
Working out the accuracy
It is the same sum as a test score. Out of 70 questions you got 65 right:
65 ÷ 70 = 0.928, and × 100 gives 92.8%.
Here, score is what you got right and hit is how many you attempted:
((score) / (hit)) * (100)
Two rules at once
The player has to clear both bars — more than 60 points and better than 90% accurate. One is not enough.
<> and <> takes two questions and is only true when both are.
if <<(score) > (60)> and <(((score) / (hit)) * (100)) > (90)>> then
say [You win!] for (2) seconds
else
say [You lose!] for (2) seconds
end
Basic — rebuild the game 25 min
Open your Lesson 1 game. You are changing the mole and adding one counter.
Step 1 — the mole becomes a clone
Move everything the mole did into a when I start as a clone stack, and end it with delete this clone instead of hide.
Step 2 — one spawner per hole
Build the nine maker stacks. Do one, test it, then duplicate the stack eight times and change the coordinates.
Step 3 — smashing a clone
The hit stack also goes on when I start as a clone, and ends in delete this clone.
Step 4 — count the swings
Make the hit variable. Add the counting and the wait until <> to the hammer’s swing stack. Test by holding the button — it should add exactly one.
Step 5 — the new winning rule
On the referee, replace the old test with the <> and <> version: more than 60, and better than 90%.
Step 6 — show the numbers
Tick hit so it appears, and lay the three monitors out so the player can see score, hit and time at a glance.
My hit count jumps by hundreds.
The wait until <not <mouse down?>> is missing, or it is above the change [hit v] by (1) instead of below it.
Two moles appear in the same hole.
Two of your nine stacks have the same coordinates. Check them against the grid — each pair should appear exactly once.
I always lose, even with a good score.
Check the accuracy sum. (score) / (hit) the right way round — hit divided by score gives a number smaller than 1 and you can never win.
A busy board
Turn your single mole into clones, one spawner per hole.
- Several moles can be up at the same time.
- Never two in the same hole.
- Each still comes up, waits and goes down on its own.
Count the swings
- A hit variable, shown on the Stage, counting every swing.
- Holding the button down counts as one, not hundreds.
- Winning needs more than 60 points and better than 90% accuracy.
Teacher note
Ask the class to deliberately lose by flailing. Seeing hit hit 200 while score sits at 60 makes the percentage mean something — and it is the first time in the course a number has measured how well rather than how much.
Show it, and let them try again
The deck’s own mission, in two parts.
- At the end, show the accuracy — whether the player won or lost.
- Add a retry button. Clicking it starts a fresh round without touching the green flag.
Teacher reveal — saying a number, and the retry
Saying a sentence with a number in it needs join () () from Operators:
say (join [Accuracy: ] (join (round (((score) / (hit)) * (100))) [%]))
round () is worth adding — 92.85714% is not a thing to show a player.
The retry button is a sprite with when this sprite clicked on it. The honest trap: stop [all v] at the end of the round kills the button’s script too. Either broadcast a restart instead of stopping everything, or have the button broadcast and let every sprite reset itself on that cue — which is Topic 1.4’s scene change wearing a different hat.
A golden mole
Every so often — about one mole in twenty — a golden one appears. It looks different, it is worth five points, and it is only up for half as long.
And one that costs you: a mole you should not hit. Smash it and you lose three points.
Summary 5 min
- One sprite is in one place. When you need several at once, clone it — one spawner per position.
go to x: () y: ()thencreate clone of [myself v]happen in one instant, so a clone is always born where you put it.wait until <not <mouse down?>>turns a held button into a single press.- Accuracy is
((score) / (hit)) * (100)— the same sum as a percentage in a test. <> and <>is true only when both halves are.
- Accuracy
- How many of your attempts worked, as a percentage.
- not
- An Operator that turns true into false and false into true.
- and
- An Operator that is true only when both of its questions are true.
- Spawner
- A stack whose job is to keep making clones.
Hand in
Share your project and send the link to your teacher. Keep it — the next two lessons finish this game off.