- Learnt28 min · the cheat and its fix, live digits, My Blocks
- Basic24 min · make the hammer fair
- Challenge 1–327 min · fair play, your digits, your own block
- Extrabonus · a full game menu
Review — where we got to 5 min
Last lesson your game learnt to finish and start again.
round ()andjoin () ()turn a long sum into “87%”.- Everything starts on
when I receive [Play v], so a Retry button can start it too. delete this cloneon Over, or the next round runs on top of the last.
Quick-fire
Your accuracy comes out as 260%. What has gone wrong?
Reveal the answer
You scored more points than you took swings. That is impossible in a fair game — so the game is not fair. Finding out why is the first half of today.
Today’s Topic 3 min
- A cheat that is hiding in your game right now
- Two costumes, and hitting by colour
- Digits that keep up with a changing number
- My Blocks — making a block of your own
Learning outcome
By the end of this lesson you will be able to:
- Find a cheat in your own game and close it.
- Show a changing variable in your own digits.
- Turn repeated code into one custom block with inputs.
Learnt — fair, and tidy 28 min
Try to cheat your own game
Open last lesson’s game. Press the mouse button down and keep it down, then drag the hammer slowly across every mole on the board.
You score every one of them. And your hit count only went up by one.
Look at why. The mole was told:
if <<touching [Hammer v]?> and <mouse down?>> then
change [score v] by (1)
end
The fix: hit by colour
Give the hammer two costumes. They are identical apart from the striking edge:
- Costume hammer — the edge painted black.
- Costume hit — the same hammer, edge painted blue.
Now the mole stops watching the hammer and watches the blue:
if <touching color [#003FFF]?> then
change [score v] by (1)
delete this clone
end
And the hammer only wears the blue edge for a moment, once per press:
when I receive [Play v]
switch costume to [hammer v]
forever
if <mouse down?> then
switch costume to [hit v]
change [hit v] by (1)
wait (0.15) seconds
switch costume to [hammer v]
wait until <not <mouse down?>>
end
end
Digits that keep up
Last lesson your digits showed the accuracy — a number worked out once, at the end. Score, hit and time are different. They change all through the game.
So the same blocks go inside a forever loop, and every clone deletes itself after 0.03 seconds.
when I start as a clone
go to [front v] layer
show
wait (0.03) seconds
delete this clone
Three readouts, three near-identical scripts
Look at what the digit sprite has to say for score, for hit, and for time:
Only four things change between them:
- the variable
- the ones-digit x
- the tens-digit x
- the y
Everything else is copied out three times. Add a fourth readout and you copy it a fourth time. Change your mind about how the digits work and you fix it in three places, and miss one.
My Blocks — a block you write yourself
In the block palette, at the very bottom, is My Blocks. Click Make a Block.
- Type a name: “Show Digits”.
- Click Add an input four times — one for each thing that changes.
- Name them: num, ones x, tens x, y.
- Click OK. A
definehat appears in your scripts.
Drag one of your three scripts under the define hat — just one — then drag the orange ovals from the hat into the places where the values used to be.
define Show Digits (num) (ones x) (tens x) (y)
if <(num) < (10)> then
switch costume to (letter (1) of (num))
go to x: (ones x) y: (y)
create clone of [myself v]
else
switch costume to (letter (1) of (num))
go to x: (tens x) y: (y)
create clone of [myself v]
switch costume to (letter (2) of (num))
go to x: (ones x) y: (y)
create clone of [myself v]
end
Now the whole forever loop is three lines:
forever
Show Digits (score) (-79) (-94) (155) :: custom
Show Digits (hit) (35) (20) (-145) :: custom
Show Digits (time) (140) (125) (155) :: custom
end
Why is this called a function?
Every language has this. Python calls it def, JavaScript calls it function, Scratch calls it a My Block. It is the same idea in all of them: name a job once, then ask for it by name.
A pupil who understands today’s block has met one of the biggest ideas in programming, and it will still be there in Level 3.
Basic — make the hammer fair 24 min
Open your Lesson 3 game.
Step 1 — prove the bug
Play a round holding the mouse button down the whole time. Write down your accuracy. It should be silly.
Step 2 — the second costume
Right-click your hammer costume and choose duplicate. Rename the copy hit. Paint its striking edge blue, and make sure the first costume’s edge is black.
Step 3 — the hammer’s new script
Replace the point in direction (90) swing with the costume swap: switch to hit, change hit by 1, wait 0.15, switch back, wait until the button is up.
Step 4 — the mole watches the colour
In the mole’s clone script, change <touching [Hammer v]?> and <mouse down?> to touching color [ ]? and eyedrop the blue off your hammer.
Step 5 — try to cheat again
Hold the button down and sweep. Nothing should happen. Then click properly and score.
The mole scores without me clicking at all.
Your blue is somewhere else on the screen — usually in the backdrop, sometimes in the hole. Pick a colour nothing else uses, or make the blue brighter.
I click right on the mole and nothing happens.
The blue edge is not actually overlapping the mole. Move the hammer’s rotation centre so the edge sits under the pointer, or make the blue strip taller.
The colour in my block is not quite the blue I painted.
Do not choose the colour from the picker sliders. Click the swatch, then click the eyedropper and click the blue on your hammer. That gets it exact.
No more cheating
One swing can score one point at most. Prove it to yourself.
- Hold the mouse down and sweep the whole board — the score must not move.
- Click properly on a mole — the score must go up by one.
- Your accuracy can never be more than 100%.
Your digits, everywhere
The deck’s mission, first half. Turn off every grey monitor.
- Score, hit and time all shown in your own digits.
- Each one in its own corner, and each one keeping up as it changes.
- No flickering.
Teacher note
The 0.03 second wait is worth pausing on. Ask what happens at 0.3 (a visible flicker) and at 0 (the clone dies before it draws, and the number vanishes). It is the first time most of them have had to tune a number rather than look one up.
Write your own block
The deck’s mission, second half. The game must not change — only the code.
- Make a My Block called Show Digits with four inputs.
- Move the digit code into it, once.
- Call it three times: score, hit, time.
- Play the game. It should look and feel exactly as it did before.
Teacher reveal — the proof it worked
Ask for a fourth readout — a high score, say — after the My Block is finished. It is one line. Before the My Block it was fourteen. That comparison is the whole point of the lesson and it is worth doing out loud.
Two things to watch for:
- Two scripts dragged under one define hat. Every call then does both, and the digits overlap.
- A pupil typing the variable name into the input instead of dragging the variable oval in. Then the block shows the word “score” instead of the number.
Build the whole menu
This is the big one, and the reward says so. Give your game a proper menu, and let the player set up the round before it starts.
- Board: 3×3, 4×4 or 5×5 holes
- Clock: 30, 60 or 90 seconds
- Difficulty: easy, medium or hard
Summary 5 min
- Blocks can be exactly right and still say the wrong thing.
mouse down?means “held”, not “pressed”. - Two costumes that differ in one colour let
touching color [ ]?tell a swing from a hold. - A number that changes needs its digits made in a
foreverloop, with each clone lasting 0.03 seconds. - My Blocks let you name a job once and ask for it by name. Every programming language has this.
- When you find yourself copying blocks for the third time, make a block instead.
- My Block
- A block you make yourself. Everywhere else it is called a function.
- Input
- A hole in your block. You fill it in when you use the block.
- touching color
- True when the sprite is touching that exact colour anywhere on screen.
- Cheat
- A way to win that the game never meant to allow.
Hand in
Share your finished Whack-a-mole and send the link to your teacher. That is the arc complete — next lesson starts the Car Race.