- Learnt27 min · the hammer, nine holes, a mole that flips a coin
- Basic25 min · build the whole game
- Challenge 1–327 min · hammer and holes, the mole, the full game
- Extrabonus · two moles at once
Review — where we got to 5 min
Last lesson you finished the Shark Game and gave it a score.
- A variable is a box with a name. Use
set [score v] to (0)at the start. Usechange [score v] by (1)when it should go up. - “For all sprites” means everybody shares one box.
- Put
change [score v] by (1)beforedelete this clone. If not, it never runs.
Quick-fire
What is on the monitor after this runs?
when flag clicked
set [score v] to (0)
repeat (4)
change [score v] by (3)
end
Reveal the answer
12. It adds three, four times. change [score v] by (1) adds to what is already in the box. It does not replace it.
Today’s Topic 3 min
- A sprite that follows your mouse pointer
- Nine holes made with clones
- A mole that picks for itself if it comes up
- A clock, and a game that ends with win or lose
Learning outcome
By the end of this lesson you will be able to:
- Make a sprite follow the mouse and react when you click.
- Put clones in exact places, not random ones.
- Use chance to pick if something happens or not.
- End a game on a timer and say whether the player won.
Learnt — mouse, holes and moles 27 min
New blocks today
| Block | Category | What it does |
|---|---|---|
go to [mouse-pointer v] | Motion | Moves the sprite to where your pointer is. |
mouse down? | Sensing | True while you hold the mouse button down. It is like a key-pressed question. |
go to [front v] layer | Looks | Puts a sprite in front of all the others. Then nothing hides it. |
repeat until <> | Control | Loops until a question is true. A countdown works like this. |
() > () and () = () | Operators | Compare two numbers. They have pointed ends, so they go inside an if. |
The hammer follows the mouse
Put go to [mouse-pointer v] inside a forever. Now the hammer sticks to your pointer.
The swing is a second stack. If it were in the same stack, the hammer would stop following you while it swings. Two stacks can run at the same time, as in Topic 1.3.
when flag clicked
go to [front v] layer
forever
go to [mouse-pointer v]
end
when flag clicked
point in direction (90 v)
forever
if <mouse down?> then
point in direction (150 v)
else
point in direction (90 v)
end
end
Nine holes, one sprite
The holes sit on a grid. The x values are −132, 0, 132. The y values are 86, 0, −86. That makes nine places.
In Topic 1.10, clones went to random places. This time we want exact places. So the maker stack moves to each spot and makes a clone of itself there.
when flag clicked
hide
set size to (55) %
go to x: (-132) y: (86)
create clone of [myself v]
go to x: (0) y: (86)
create clone of [myself v]
go to x: (132) y: (86)
create clone of [myself v]
when I start as a clone
go to [back v] layer
show
The mole tosses a coin
A mole that pops up every time is boring. It is not a game. So we give it a 50% chance each turn. That means half the time.
pick random (0) to (1) gives you a 0 or a 1. It is like a coin. Ask if it came up 1. Only then does the mole come up.
when flag clicked
hide
forever
wait (pick random (1) to (1.5)) seconds
if <(pick random (0) to (1)) = (1)> then
go to x: ((pick random (-1) to (1)) * (132)) y: ((pick random (-1) to (1)) * (86))
switch costume to [half out v]
go to [front v] layer
show
wait (0.3) seconds
switch costume to [fully out v]
wait (0.5) seconds
hide
end
end
Getting smashed
A second stack on the mole watches for the hammer. It only counts if the mouse is down. If not, just waving the hammer over a hole would score.
when flag clicked
forever
if <touching [Hammer v]?> then
if <mouse down?> then
switch costume to [smashed v]
change [score v] by (1)
wait (0.2) seconds
hide
end
end
end
The referee
The clock is not part of the hammer or the mole. It is part of the game. So it gets its own hidden sprite. This sprite keeps time and says win or lose.
when flag clicked
hide
set [score v] to (0)
set [time v] to (30)
repeat until <(time) = (0)>
wait (1) seconds
change [time v] by (-1)
end
show
if <(score) > (5)> then
say [You win!] for (2) seconds
else
say [You lose!] for (2) seconds
end
stop [all v]
Basic — build the game 25 min
Step 1 — the hammer
Add your hammer sprite. Build the two stacks. One follows the mouse. One swings on mouse down?. Test it before you add anything else.
Step 2 — one hole
Add the hole sprite. Set its size so it looks right on your backdrop. The deck uses 300% for a small picture. Ours is 55%. Yours may be different.
Step 3 — nine holes
Build the maker stack. It needs nine pairs of go to x: () y: () and create clone of [myself v], one for each spot. Then add the when I start as a clone stack. It has go to [back v] layer and show.
Step 4 — the mole appears
Add the mole with its three costumes. Build the coin-toss stack. Watch it for a minute. Count how often it comes up. It should be about half the turns.
Step 5 — smashing
Add the second mole stack. Then test it. Swing at an empty hole: nothing happens. Wave the hammer over a mole without clicking: nothing happens. Click on a mole: it goes.
Step 6 — score and clock
Make the two variables, score and time. Add the referee sprite with the countdown. Right-click each monitor and choose large readout. Then drag score to the left and time to the right.
Step 7 — music
Click the Stage and add a sound. Give it set volume to (20) %. Then add a forever that plays it. Keep it at twenty per cent, so you can still hear the game.
The hammer disappears behind the holes.
The hammer needs go to [front v] layer. The holes need go to [back v] layer. The sprite that ran last goes on top. So put the right block on both.
I score just by holding the mouse down and waving about.
Your blocks are doing just what you told them. Think about why. The fix is to make only the head of the hammer count. You put a colour on the costume. Then you use touching color [#003FFF]? in place of touching [Hammer v]?. That is next lesson.
Hammer and holes
Get the board ready.
- Nine holes, made with clones, on the nine grid places.
- A hammer that follows your mouse and swings when you click.
- The hammer is always in front, the holes always behind.
A mole worth hitting
- The mole appears in a random hole, about half the time.
- It comes up, waits, and goes back down by itself.
- Click on it while it is up. You score 1 point and it goes down.
- Clicking an empty hole scores nothing.
Teacher note
The coin toss is the interesting block here. pick random (0) to (1) is the first time chance decides whether rather than where, and it is worth asking the class what the game would feel like without it. A mole that always appears is a chore; one that sometimes does not is a game.
The whole game
This is the main mission. Finish it.
- Nine holes, made with clones.
- Thirty seconds of play, counting down on the Stage.
- One point per mole smashed.
- Nine points or more in the thirty seconds wins. Less loses.
- A sprite says win or lose. Then everything stops.
Teacher note
Nine points in thirty seconds is deliberately tuned — with a 50% coin and a 1 to 1.5 second wait, about twelve moles appear in a round, so nine is a real target and not a formality. Pupils who find it too easy have usually shortened the wait; that is a fair change as long as they can say what they changed and why.
The verdict sprite needs go to [front v] layer before it shows — third time this lesson, and worth saying out loud.
Two moles at once
Here is a question: can you make more than one mole come out at once?
Do it. Two moles are up at the same time, in different holes. They are never both in the same one.
Summary 5 min
go to [mouse-pointer v]in a forever sticks a sprite to your pointer.mouse down?is the click.go to [front v] layerandgo to [back v] layerchoose what is on top. Use them, or Scratch chooses for you.- You can put clones in exact places: move, clone, move, clone. A clone stays where it was made.
pick random (0) to (1)is a coin toss. Chance picks if something happens or not.repeat until <(time) = (0)>is a countdown. A hidden referee sprite is a good place to keep it.
- Layer
- Which sprite is drawn in front of which.
- Coin toss
- A random pick between two things. It decides if something happens.
- Referee sprite
- A hidden sprite that runs the rules of the game. It is not a character.
Hand in
Share your project and send the link to your teacher. Keep it safe. The next three lessons build on this game.