- 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 number into “87%”.- Everything starts on
when I receive [Play v]. So a Retry button can start it too. - Use
delete this cloneon Over. If not, 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 cannot happen in a fair game. So the game is not fair. Today we find out why.
Today’s Topic 3 min
- A cheat that is in your game right now
- Two costumes, and hitting by colour
- Digits that keep up when a number changes
- 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 stop it.
- Show a variable that changes in your own digits.
- Turn copied code into one block of your own, 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 the same, but the edge that hits is different:
- Costume hammer: paint the edge black.
- Costume hit: the same hammer, with the edge painted blue.
Now the mole stops looking for the hammer. It looks for the blue:
if <touching color [#003FFF]?> then
change [score v] by (1)
delete this clone
end
The hammer only shows 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. That number is 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. 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 needs 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 three times. A fourth readout means a fourth copy. If you change how the digits work, you must fix three places. It is easy to miss one.
My Blocks — a block you write yourself
Look at the very bottom of the block list. There 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. Drop them where the numbers 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 coding 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. You name a job once. Then you ask for it by name.
If you understand today’s block, you have met one of the biggest ideas in coding. You will use it again in Level 3.
Basic — make the hammer fair 24 min
Open your Lesson 3 game.
Step 1 — prove the bug
Play a round with the mouse button held 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 the edge that hits blue. Check that the first costume’s edge is black.
Step 3 — the hammer’s new script
Take out the point in direction (90) swing. Use the costume swap in its place: 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 [ ]?. Use the eyedropper to pick the blue from your hammer.
Step 5 — try to cheat again
Hold the button down and sweep. Nothing should happen. Then click one at a time and score.
The mole scores without me clicking at all.
Your blue is somewhere else on the screen. It is often in the backdrop, or 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 really over the mole. Move the hammer’s 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 with the sliders. Click the swatch. Then click the eyedropper and click the blue on your hammer. That gets it just right.
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 once on a mole. The score must go up by one.
- Your accuracy can never be more than 100%.
Your digits, everywhere
This is the first half of the main mission. Turn off every grey monitor.
- Score, hit and time all shown in your own digits.
- Each one in its own corner. Each one keeps 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
This is the second half of the main mission. The game must not change. Only the code changes.
- 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 just like 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. Give your game a menu. 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 (how hard it is): easy, medium or hard
Summary 5 min
- Blocks can be right and still say the wrong thing.
mouse down?means “held”, not “pressed”. - Use two costumes with one different colour. Then
touching color [ ]?can tell a swing from a hold. - A number that changes needs its digits made in a
foreverloop. Each clone lasts 0.03 seconds. - My Blocks let you name a job once and ask for it by name. Every coding language has this.
- Are you copying blocks for the third time? Make a block instead.
- My Block
- A block you make yourself. Other coding languages call it a function.
- Input
- A hole in your block. You fill it in when you use the block.
- touching color
- True when the sprite touches that exact colour on screen.
- Cheat
- A way to win that the game was not meant to allow.
Hand in
Share your finished Whack-a-mole and send the link to your teacher. Whack-a-mole is done. Next lesson starts the Car Race.