- Learnt28 min · timer, best time, three chests, START
- Basic24 min · the finished hunt
- Challenge 1–327 min · the record, play again, medals
- Extrabonus · a Hot zone that shrinks
Review — where we got to 5 min
<<> or <>>said yes for either snake.<not <touching [Hole v]?>>stopped digging twice.- One watcher script ended the game, whoever took the last dig.
- The beep’s wait was the distance divided by 200.
Quick-fire
You press the green flag. Which of these should go back to its first value?
digs lefttimebest time
Reveal the answer
digs left and time — but not best time. A record that resets every game is not a record. Today’s job is to keep it.
Today’s Topic 3 min
(timer)andreset timer— Scratch’s own stopwatch- Three chests in one round
- A best time the flag never resets, checked with
<<> or <>> - A start screen: a title, a START button, and one broadcast
Learning outcome
By the end of this lesson you will be able to:
- Time a game with the timer.
- Keep a record that only changes when it is beaten.
- Start a game from a button instead of the green flag.
Learnt — the clock and the front door 28 min
1 · Scratch has a stopwatch
(timer) is a round Sensing block. It counts seconds, all by itself. reset timer puts it back to 0.
- Open Sensing. Tick the box next to
(timer). - Watch the Stage. The number climbs with lots of decimals.
- Click
reset timer. It jumps back to 0. - Untick it. We will show a tidy
timevariable instead.
The timer has decimals: 12.4836. (round ()) makes it 12.
when flag clicked
reset timer
forever
set [time v] to (round (timer))
end
when flag clicked
forever
go to [mouse-pointer v]
if <<mouse down?> and <(distance to [Treasure v]) < (60)>> then
broadcast [found v]
end
end
when I receive [found v]
stop [other scripts in sprite v]
say (join [Found in ] (join (time) [ seconds!]))
How to use this: Click the green flag, then move the mouse to the chest and press the mouse button on it. The time stops and the detector says how long you took.
2 · A best time that stays
Make a variable called best time. Here is the one rule: nothing under the green flag touches it.
It changes in one place only: when a game ends. And only in two cases:
- there is no best time yet (it is still 0), or
- this time is smaller than the best.
Two cases, either one is enough. That is an or.
when I receive [found v]
stop [other scripts in sprite v]
if <<(best time) = (0)> or <(time) < (best time)>> then
set [best time v] to (time)
end
say (join [Best: ] (best time))
| best time | time | New best? |
|---|---|---|
| 0 | 30 | yes — the first game |
| 30 | 22 | yes — faster |
| 22 | 27 | no — slower |
How to use this: Click the green flag, then press the mouse button on the chest. Click the green flag again and play once more. The best time only changes when you beat it.
3 · Three chests in a row
One chest is quick. Three is a race. After each find, the chest counts itself and jumps somewhere new.
An open chest stays behind, as a trophy. It is a clone — made exactly like a hole. It comes from a new sprite, Open Chest, and goes to the Detector.
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]
change [treasures v] by (1)
start sound [Coin v]
create clone of [Open Chest v]
go to x: (pick random (-180) to (200)) y: (pick random (-150) to (-40))
when flag clicked
hide
when I start as a clone
go to [Detector v]
show
On the Detector, a watcher counts to three:
when flag clicked
forever
if <mouse down?> then
if <(distance to [Treasure v]) < (60)> then
broadcast [found v]
end
wait until <not <mouse down?>>
end
end
when flag clicked
set [treasures v] to (0)
wait until <(treasures) = (3)>
broadcast [win v]
when I receive [win v]
stop [other scripts in sprite v]
say [All three!]
How to use this: Click the green flag, then move the mouse over the sand. Press the mouse button to dig when it says Hot!. Find the chest three times.
4 · A start screen
A real game has a front door. The green flag only sets the scene:
- the title and the START button show;
- the detector hides.
Clicking START sends broadcast [start v]. Everything that plays now waits for that message instead of the flag.
when flag clicked
show
when this sprite clicked
broadcast [start v]
when I receive [start v]
hide
when flag clicked
hide
when I receive [start v]
show
forever
go to [mouse-pointer v]
end
How to use this: Click the green flag, then click START. The title and the button go, and the detector appears. Move the mouse to sweep it.
5 · One script runs the round
Last lesson, one watcher waited for the digs to run out. Now the round can end two ways: out of digs, or three chests found. One script watches both, and runs the clock while it waits.
when I receive [start v]
set [digs left v] to (10)
set [treasures v] to (0)
reset timer
repeat until <<(digs left) < (1)> or <(treasures) = (3)>>
set [time v] to (round (timer))
end
if <(treasures) = (3)> then
broadcast [win v]
else
broadcast [lost v]
end
Basic — the finished hunt 24 min
Open your hunt from last lesson. This is the last big build. Play after every step.
How to use this: Click the green flag, then click START. Move the mouse over the sand, listen for the beep, and press the mouse button to dig. Find three chests before your ten digs run out.
Step 1 — new variables
- Make
treasures,timeandbest time, for all sprites. - Tick all three, and digs left, so they show.
Step 2 — the Open Chest
- Duplicate the Treasure. Rename the copy
Open Chest. Delete its shut costume and all its scripts. - Give it the Hole’s two stacks: hide at the flag, and the clone script.
Step 3 — the Treasure moves on
- Change the Treasure’s “found” script: count,
Coin, clone of Open Chest, new random spot. - Change its
when flag clickedtowhen I receive [start v].
Step 4 — the title and START
- Paint a
Titlesprite with the game’s name. Paint aStartbutton. Or useButton2. - Give both their stacks from section 4.
Step 5 — the Detector waits for START
- Add
when flag clickedandhide. - In every other script, swap
when flag clickedforwhen I receive [start v]. - Add
showto the top of the follow script. - Add
wait until <not <mouse down?>>to the top of the dig script.
Step 6 — the round, and the win
- Delete last lesson’s watcher. Build the round script instead.
- Change the “found” script on the Detector into a “win” script, as below.
- Win once. Press the flag. Win again, slower. The best time must not change.
The whole Detector
when flag clicked
hide
when I receive [start v]
show
forever
go to [mouse-pointer v]
if <(distance to [Treasure v]) < (60)> then
say [Hot!]
end
if <<(distance to [Treasure v]) > (60)> and <(distance to [Treasure v]) < (150)>> then
say [Warm]
end
if <(distance to [Treasure v]) > (150)> then
say [Cold]
end
end
when I receive [start v]
wait until <not <mouse down?>>
forever
if <mouse down?> then
if <not <touching [Hole v]?>> then
if <(distance to [Treasure v]) < (60)> then
broadcast [found v]
else
change [digs left v] by (-1)
create clone of [Hole v]
end
end
wait until <not <mouse down?>>
end
end
when I receive [start v]
forever
if <<touching [Snake v]?> or <touching [Snake2 v]?>> then
change [digs left v] by (-1)
start sound [Bonk v]
wait (1) seconds
end
end
when I receive [start v]
forever
start sound [Computer Beep v]
wait ((distance to [Treasure v]) / (200)) seconds
end
when I receive [win v]
stop [other scripts in sprite v]
if <<(best time) = (0)> or <(time) < (best time)>> then
set [best time v] to (time)
end
say (join [All three in ] (join (time) [ seconds!]))
when I receive [lost v]
stop [other scripts in sprite v]
say [Out of digs!]
My first dig is up in the sky.
The dig script is missing its first wait until <not <mouse down?>>. The click on START was counted as a dig.
The best time goes back to 0 every game.
Something sets best time under a flag or a start hat. Delete it. Only the win script may change it.
The best time never changes at all.
The <(best time) = (0)> half of the or is missing. A best time of 0 can never be beaten.
Finding one chest counts as two.
The trophy is a clone of the Treasure, and the clone hears “found” too. Use a separate Open Chest sprite.
Nothing happens when I click START.
A sprite is covering the button, or the Detector is showing on top of it. The Detector must hide at the flag.
The record to beat
Show the best time on the start screen, so there is something to aim at.
- Before any game, the title says No best time yet.
- After a win, it says Best time: and the number.
- It hides with the title when the game starts.
Teacher reveal — on the Title
when flag clicked
show
if <(best time) = (0)> then
say [No best time yet]
else
say (join [Best time: ] (best time))
end
The same <(best time) = (0)> test as the win script, doing a different job. Worth pointing out that 0 means “nothing yet” here.
Play again
Right now, a second game needs the green flag. Close the loop without it.
- Three seconds after a win or a loss, the title and START come back.
- Clicking START begins a fresh round.
- The old holes and open chests are gone.
- The best time stays.
How to use this: Click the green flag, then click START. Find three chests, or run out of digs. After the round, click START again: the desert is cleared and the best time stays.
Teacher reveal — two small scripts in the right places
when I receive [win v]
wait (3) seconds
show
when I receive [lost v]
wait (3) seconds
show
On the Title and on Start. Then, on the Hole and on the Open Chest:
when I receive [start v]
delete this clone
The original ignores delete this clone, so it is safe to put on the sprite itself. The flag used to clear the clones for us. Now nothing does unless we ask.
Medals
A time is a number. A medal is a reward. Give one out.
- Under 20 seconds wins gold.
- Under 40 seconds wins silver.
- Anything slower wins bronze.
- The detector says the time first, then the medal.
Teacher reveal — the chain from Lesson 13, one last time
when I receive [win v]
stop [other scripts in sprite v]
if <<(best time) = (0)> or <(time) < (best time)>> then
set [best time v] to (time)
end
say (join [All three in ] (join (time) [ seconds!])) for (2) seconds
if <(time) < (20)> then
say [Gold medal!]
else
if <(time) < (40)> then
say [Silver medal!]
else
say [Bronze medal!]
end
end
The arc ends where it started: an if / else chain, smallest question first. Ask the room why gold must be checked before silver. It is the Hot!-before-Warm answer.
A shrinking Hot zone
Make each chest harder than the last.
- The first chest counts as found within 60 steps.
- Every chest you find makes that 15 steps smaller.
- The words and the dig both use the new size.
How to use this: Click the green flag, then click START. Press the mouse button to dig when it says Hot!. After each chest you must get closer before a dig counts.
Teacher reveal — one variable, three places
Add set [hot size v] to (60) near the top of the round script. In the Treasure’s “found” script, after the count:
change [hot size v] by (-15)
Then every 60 on the Detector becomes the variable:
if <(distance to [Treasure v]) < (hot size)> then
say [Hot!]
end
if <<(distance to [Treasure v]) > (hot size)> and <(distance to [Treasure v]) < (150)>> then
say [Warm]
end
And in the dig: <(distance to [Treasure v]) < (hot size)>. A pupil who changes only one of the three finds the words and the dig disagree. That is the argument for naming a number.
Summary 5 min
(timer)counts seconds by itself.reset timerputs it back to 0.- A record changes only when it is beaten. Keep it away from the green flag.
<<(best time) = (0)> or <(time) < (best time)>>covers the first game and every faster one.- A clone hears broadcasts too. Leave trophies from a separate sprite.
- A start screen is one broadcast. The flag sets the scene; START begins the game.
- Timer
- Scratch’s stopwatch. It counts seconds on its own.
- Record
- The best result so far. Only a better one replaces it.
- Start screen
- What the player sees before the game begins.
- Round
- One game, from START to a win or a loss.
The Treasure Hunt, finished
Four lessons ago the detector could only say Hot! or Cold. Since then you have used:
<() < ()>,<() > ()>and<() = ()>to compare;and,orandnotto build bigger questions;if / thenandif / then / elseto choose;(distance to [Treasure v]), the mouse and the timer to sense the world.
Hand in
Share your finished hunt and send the link to your teacher. Next lesson starts a new game: a robot that follows a line.