- Learnt28 min · the mouse, distance, comparing, if / else
- Basic24 min · build the draft hunt
- Challenge 1–327 min · boiling, the sand, nothing here
- Extrabonus · a detector that grows
Review — Shooting Game, finished 5 min
The last four lessons built a shooting game.
- Each zombie was a clone with its own
(hp). - Waves got harder, and every kill paid coins.
- The shop checked before it sold anything:
<not <(coins) < (cost)>>.
Quick-fire
You have 100 coins. The upgrade costs 100.
Is <(coins) < (cost)> true or false?
Reveal the answer
False. 100 is not less than 100. So the <not <>> turns it into true, and you can buy.
Questions like this are what today is all about.
Today’s Topic 3 min
- A sprite that follows the mouse
(distance to [Treasure v])— how far away something is- Comparing two numbers with
<() < ()>and<() > ()> if / thenandif / then / else<mouse down?>— digging with the mouse button
Learning outcome
By the end of this lesson you will be able to:
- Measure how far one sprite is from another.
- Ask a yes-or-no question by comparing two numbers.
- Choose between three answers with an if / else chain.
Learnt — hot, warm or cold 28 min
The game, on paper first
- A chest hides at a random spot in the sand.
- You move a metal detector with the mouse.
- The detector says Hot!, Warm or Cold.
- When it says Hot!, you press the mouse button to dig.
You need two sprites: a Detector and a Treasure.
1 · A detector that follows the mouse
You met go to [mouse-pointer v] in Level 1. Inside a forever, it makes a sprite stick to the mouse.
- Open Motion. Drag out
go to [random position v]. - Click the little arrow. Pick
mouse-pointer. - Put it inside a
forever, underwhen flag clicked. - Click the green flag. Move the mouse. The detector follows.
Sensing has two more mouse blocks. (mouse x) is how far left or right the mouse is. (mouse y) is how far up or down.
go to [mouse-pointer v]
go to x: (mouse x) y: (mouse y)
The long way looks like extra work. In Challenge 2 you will see why it is useful.
2 · How far away? distance to
(distance to [Treasure v]) is a round Sensing block. It holds one number: how many steps away the Treasure is.
- Open Sensing. Find
(distance to [mouse-pointer v]). - Click its arrow. Pick
Treasure. - Click the block itself. A little bubble shows the number.
- Drag the Treasure somewhere else. Click the block again. The number changes.
when flag clicked
forever
go to [mouse-pointer v]
say (round (distance to [Treasure v]))
end
(round ()) from Whack-a-mole makes it a whole number.How to use this: Click the green flag, then move the mouse around the desert. The detector follows it and says how far away the chest is. Nothing else to press.
3 · A question with a yes or no answer
A pointy block is a question. Its answer is only ever true or false. You met some already, like <mouse down?>.
Operators has three that compare two numbers:
<(distance to [Treasure v]) < (60)>
<(distance to [Treasure v]) > (150)>
The third is <() = ()>, for “exactly the same”. You will use it next lesson.
| Distance | <(distance to [Treasure v]) < (60)> | <(distance to [Treasure v]) > (150)> |
|---|---|---|
| 40 | true | false |
| 100 | false | false |
| 300 | false | true |
4 · if / then, or if / then / else
A pointy question fits the pointy slot in an if block.
if / thendoes something when the answer is yes. When it is no, it does nothing.if / then / elsealways does one of two things. Yes runs the top part. No runs the bottom part.
when flag clicked
forever
go to [mouse-pointer v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
else
say [Cold]
end
end
How to use this: Click the green flag, then move the mouse towards the chest and away again. Watch the detector's word change. Nothing else to press.
5 · Three answers: the chain
Two words is not enough. We want Warm in the middle. Put a second if / else inside the first one’s else.
when flag clicked
forever
go to [mouse-pointer v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
end
How to use this: Click the green flag, then move the mouse slowly towards the chest. The detector says Cold, then Warm, then Hot!. Nothing else to press.
6 · Bury it, then dig
Now the chest has to hide. At the flag it does three things:
hide— nobody can see it.switch costume to [chest v]— shut, ready for this game.go to x: (pick random (-180) to (200)) y: (pick random (-150) to (-40))— a random spot on the sand.
Digging is <mouse down?>. It is true while the mouse button is held. We only dig when Hot!, so that check goes inside the Hot! part.
when flag clicked
hide
switch costume to [chest v]
go to x: (pick random (-180) to (200)) y: (pick random (-150) to (-40))
when I receive [found v]
switch costume to [open v]
show
start sound [Coin v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
if <mouse down?> then
broadcast [found v]
end
else
... :: grey
end
if / then with no else: not digging means doing nothing.Basic — build the draft hunt 24 min
Start a new project. Build one step at a time, and play after each.
How to use this: Click the green flag, then move the mouse over the sand to sweep the detector. When it says Hot!, press the mouse button to dig. Click the green flag again to hide the chest somewhere new.
Step 1 — the desert
- Click the Stage, then the backdrop button.
- Pick
Desertfrom the library.
Step 2 — the two sprites
- Paint a sprite called
Detector: a grey circle with a stick for a handle. Or pickMagic Wand. - Paint a sprite called
Treasurewith two costumes:chest(shut) andopen. A box with a lid is enough. - Add the
Coinsound to the Treasure.
Step 3 — follow the mouse
- On the Detector:
when flag clicked, thenforever. - Inside it:
go to [mouse-pointer v]. - Play. The detector sticks to the mouse.
Step 4 — the three words
- Add an
if / elseunder the go to. Put<(distance to [Treasure v]) < (60)>in its slot. - Top part:
say [Hot!]. - Bottom part: a second
if / elseasking<(distance to [Treasure v]) < (150)>. - Its top part says
Warm. Its bottom part saysCold. - Play with the chest showing. Check all three words.
Step 5 — bury the chest
- On the Treasure, build the flag stack: hide, costume, random spot.
- Play. The chest vanishes. The words still work.
- Press the flag a few times. The Hot! spot moves each time.
Step 6 — dig
- In the Hot! part, add an
if / then. Put<mouse down?>in its slot. - Inside it:
broadcast [found v]. Make a new message calledfound. - On the Treasure:
when I receive [found v], open costume, show,start sound [Coin v]. - On the Detector:
when I receive [found v], thenstop [other scripts in sprite v]andsay [You found it!].
The whole Detector
when flag clicked
forever
go to [mouse-pointer v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
if <mouse down?> then
broadcast [found v]
end
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
end
when I receive [found v]
stop [other scripts in sprite v]
say [You found it!]
It always says Warm, even right on top of the chest.
Your first question is <(distance to [Treasure v]) < (150)>. Swap the two numbers round: 60 must come first.
The number never changes.
The dropdown still says mouse-pointer. The detector is always 0 steps from the mouse! Change it to Treasure.
After finding it, the detector keeps following the mouse.
The stop [other scripts in sprite v] is missing. That block ends the forever loop.
The chest is already open when the game starts.
Add switch costume to [chest v] to its flag stack.
Boiling!
Add a fourth word for when you are nearly on top of it.
- Under 30 steps, the detector says Boiling!.
- Hot!, Warm and Cold still work.
- You can still dig when it says Boiling!.
Teacher reveal — the four-word chain
if <(distance to [Treasure v]) < (30)> then
say [Boiling!]
if <mouse down?> then
broadcast [found v]
end
else
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
if <mouse down?> then
broadcast [found v]
end
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
end
The trap is the dig. Most pupils add Boiling! and forget to copy the <mouse down?> check into it. Then digging on Boiling! does nothing. Let them find it by playing.
Stay on the sand
A metal detector does not fly. Keep it on the ground.
- While the mouse is on the sand, the detector follows it.
- When the mouse goes up into the sky, the detector stays put.
- The sand ends at about y = -30.
How to use this: Click the green flag, then move the mouse over the sand, then up into the sky. The detector stops at the edge of the sand. Press the mouse button to dig when it says Hot!.
Teacher reveal — the long way pays off
when flag clicked
forever
if <(mouse y) < (-30)> then
go to x: (mouse x) y: (mouse y)
end
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
if <mouse down?> then
broadcast [found v]
end
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
end
go to [mouse-pointer v] would work here too. The long version is worth showing, because it leads to a better one: keep following left and right with set x to (mouse x), and only stop the up and down. Ask who can build that.
Nothing here!
Right now, digging only works when it is Hot!. Let the player dig anywhere.
- A dig that is Hot! opens the chest, as before.
- A dig anywhere else says Nothing here! for one second.
- While it says so, the detector stays still. Digging takes time!
How to use this: Click the green flag, then move the mouse over the sand. Press the mouse button anywhere to dig. Far away it says Nothing here!; close enough and the chest opens.
Teacher reveal — the dig gets its own if / else
when flag clicked
forever
go to [mouse-pointer v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
if <mouse down?> then
if <(distance to [Treasure v]) < (60)> then
broadcast [found v]
else
say [Nothing here!] for (1) seconds
end
end
end
The dig moves out of the chain and sits below it. Now it is an if / then / else, because a dig always gets an answer. This is the clearest example of the two shapes side by side — worth pointing at.
The say [Nothing here!] for (1) seconds pauses the loop. So the detector freezes for that second. It feels right, so we keep it.
A detector that grows
Words are good. A picture is faster. Make the detector show how close it is.
- Far from the chest, the detector is small.
- The closer it gets, the bigger it grows.
- Right on top of it, it is at its biggest.
How to use this: Click the green flag, then move the mouse over the sand. The detector grows as you get close. Press the mouse button to dig when it says Hot!.
Teacher reveal — size from distance
when flag clicked
forever
go to [mouse-pointer v]
set size to ((120) - ((distance to [Treasure v]) / (5))) %
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
if <mouse down?> then
broadcast [found v]
end
else
if <(distance to [Treasure v]) < (150)> then
say [Warm]
else
say [Cold]
end
end
end
At 0 steps the size is 120%. At 400 steps it is 40%. Dividing by 5 keeps it from going below zero. Pupils who try ((200) - (distance to [Treasure v])) find the detector vanish when it is far away — a good bug to explain.
Summary 5 min
go to [mouse-pointer v]in a forever makes a sprite follow the mouse.(mouse x)and(mouse y)give its two numbers.(distance to [Treasure v])is how many steps away a sprite is. It works even when that sprite is hidden.<() < ()>and<() > ()>compare two numbers. The answer is true or false.if / thenmay do nothing.if / then / elsealways does one of two things.- An if / else inside an else makes a chain. Ask the smallest question first.
- Distance
- How many steps one sprite is from another.
- Boolean
- A pointy block whose answer is only true or false.
- Compare
- Ask if one number is less than, more than, or equal to another.
- Chain
- An if / else inside an else, so there are more than two answers.
Hand in
Save your hunt. Next lesson you get five digs — and you can lose.