- 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 sticks a sprite to the pointer.mouse down?is the click.go to [front v] layerchooses what is on top.pick random (0) to (1)is a coin toss. 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. One sprite can only be in one place. Today we fix that.
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 many copies of one sprite in different places at the same time.
- Count a button press once, even if it is held down.
- 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 slow. Only one mole is ever up. That is because there is only one mole sprite. A sprite can only be in one place.
You already know the fix from Topic 1.10. When you need many of something, clone it.
Nine holes, nine clocks
Give the mole nine stacks, one per hole. Each stack waits its own random time. Then it 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 add one stack. Every clone runs it when it is made:
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. You will score far more than nine. So the rules change. You now need more than 60 points. You also need to be accurate.
Accuracy means this: of all the times you swung, how many hit a mole? Swinging wildly all the time should not win.
Counting the swings
Make a second variable, hit. Add one every time the player presses the mouse.
There is a trap. A forever checks mouse down? many times a second. So if you hold the button for one second, it counts hundreds of hits.
wait until <not <mouse down?>> fixes it. The script counts one. Then it stops and waits for you to let go of the button.
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. Say you got 65 out of 70 questions right:
65 ÷ 70 = 0.928, and × 100 gives 92.8%.
Here, score is how many you got right. Hit is how many times you tried:
((score) / (hit)) * (100)
Two rules at once
The player has to pass both rules. They need more than 60 points and better than 90% accuracy. One is not enough.
<> and <> takes two questions. It is only true when both are true.
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 will change the mole and add one counter.
Step 1 — the mole becomes a clone
Move everything the mole did into a when I start as a clone stack. End it with delete this clone, not hide.
Step 2 — one spawner per hole
A spawner is a stack that keeps making clones. Build the nine maker stacks. Do one and test it. Then duplicate the stack eight times and change the x and y numbers.
Step 3 — smashing a clone
The hit stack also goes on when I start as a clone. It ends with 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 it by holding the button. It should add just one.
Step 5 — the new winning rule
On the referee, swap the old test for the <> and <> one. It needs more than 60, and better than 90%.
Step 6 — show the numbers
Tick hit so it shows on the Stage. Move the three monitors so the player can see score, hit and time easily.
My hit count jumps by hundreds.
The wait until <not <mouse down?>> is missing. Or it is above the change [hit v] by (1). It should be below it.
Two moles appear in the same hole.
Two of your nine stacks have the same x and y. Check them against the grid. Each pair should be there only once.
I always lose, even with a good score.
Check the accuracy sum. It must be (score) / (hit), this way round. Hit divided by score gives a number smaller than 1, and you can never win.
A busy board
Turn your one mole into clones, with one spawner per hole.
- Several moles can be up at the same time.
- Never two in the same hole.
- Each one still comes up, waits and goes down by itself.
Count the swings
- A hit variable on the Stage. It counts 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
This is the main mission. It has two parts.
- At the end, show the accuracy. Show it if the player won or lost.
- Add a retry button. Click it to start a new round. You do not need 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
Now and then a golden mole comes up. That is about one mole in twenty. It looks different. It is worth five points. It is only up for half as long.
Add a bad mole too. You should not hit it. If you smash it, you lose three points.
Summary 5 min
- One sprite is in one place. When you need many at once, clone it. Use one spawner per place.
go to x: () y: ()thencreate clone of [myself v]happen at the same moment. So a clone is always made where you put it.wait until <not <mouse down?>>turns a held button into one press.- Accuracy is
((score) / (hit)) * (100). It is the same sum as a test score. <> 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.