- Learnt28 min · timer, round, repeat until, lives, or
- Basic25 min · a sixty-second round
- Challenge 1–327 min · a sprint, a hurry-up, two endings
- Extrabonus · fast answers score double
Review — where we got to 5 min
(pick random (1) to (10)), kept inaandb, makes a new question every time.(join (a) (join [ × ] (b)))glues the question together.<(answer) = ((a) * (b))>lets Scratch check it.
Quick-fire
Your round asks five questions. How could it keep asking for exactly one minute instead?
Reveal the answer
It needs a clock, and a loop that stops when the clock says so. Scratch has both. You will meet them in the next ten minutes.
Today’s Topic 3 min
(timer)andreset timer— Scratch’s own stopwatch(round ((60) - (timer)))— a countdown in whole secondsrepeat until— a loop that stops itself- Three lives, and a round that ends on time OR on lives
Learning outcome
By the end of this lesson you will be able to:
- Time something with the timer, and show the time left.
- Loop until something is true, instead of a fixed number of times.
- End a round when time runs out or when the lives do.
Learnt — a clock and three lives 28 min
1 · Scratch’s own stopwatch
A stopwatch counts up from 0. Scratch has one built in, and it is always running.
- Click Sensing. It is the light blue drawer.
- Find
(timer)near the bottom. Tick the box beside it. - A box appears on the Stage. Watch it count up in seconds.
- Click
reset timer, just above it. The box jumps back to 0.
2 · Time left, in whole seconds
Players want to see how long is left, not how long has gone. That is a take-away sum.
((60) - (timer))
(round ((60) - (timer)))
(round ()) tidies it to 57.(round ()) is in Operators. It turns 56.73 into 57 and 56.2 into 56. A whole number is easier to read at speed.
3 · A loop that stops itself
repeat (5) counts to 5 and stops. We do not know how many questions fit in a minute. So we need a different loop.
repeat until in Control runs its inside again and again. Before each go, it checks its diamond. When the diamond is true, it stops.
when flag clicked
reset timer
repeat until <(timer) > (10)>
set [time left v] to (round ((10) - (timer)))
end
set [time left v] to (0)
say [Time's up!] for (2) seconds
How to use this: Click the green flag to start it. Nothing else to press — watch it through, then use ↺ Reset to run it again.
4 · Three lives
A game you cannot lose is not much of a game. Give the player three lives. A wrong answer takes one away.
- Make a variable called
lives. - At the start,
set [lives v] to (3). - In the else half, add
change [lives v] by (-1). - Make the loop
repeat until <(lives) = (0)>.
when flag clicked
set [score v] to (0)
set [lives v] to (3)
repeat until <(lives) = (0)>
set [a v] to (pick random (1) to (10))
set [b v] to (pick random (1) to (10))
ask (join (a) (join [ × ] (b))) and wait
if <(answer) = ((a) * (b))> then
change [score v] by (1)
else
change [lives v] by (-1)
say (join [It is ] ((a) * (b))) for (1) seconds
end
end
say (join [Out of lives! Score: ] (score)) for (4) seconds
How to use this: Click the green flag, then type your answer in the box and press Enter.
5 · Time OR lives
Our round should end when the time is up or the lives are gone. The <<> or <>> block in Operators joins two diamonds. It is true if either side is true.
repeat until <<(timer) > (60)> or <(lives) = (0)>>
end
| Time over 60? | Lives at 0? | Round ends? |
|---|---|---|
| no | no | no — keep asking |
| yes | no | yes |
| no | yes | yes |
6 · The clock needs its own script
ask [] and wait stops its script until you press Enter. If time left lives in that same loop, it freezes while you think.
So the clock gets a script of its own. Two scripts run at the same time — you did this in Level 1.
when flag clicked
set [score v] to (0)
set [lives v] to (3)
reset timer
broadcast [clock v]
repeat until <<(timer) > (60)> or <(lives) = (0)>>
set [a v] to (pick random (1) to (10))
set [b v] to (pick random (1) to (10))
ask (join (a) (join [ × ] (b))) and wait
if <(answer) = ((a) * (b))> then
change [score v] by (1)
else
change [lives v] by (-1)
say (join [It is ] ((a) * (b))) for (1) seconds
end
end
say (join [Round over! Score: ] (score)) for (4) seconds
when I receive [clock v]
repeat until <<(timer) > (60)> or <(lives) = (0)>>
set [time left v] to (round ((60) - (timer)))
end
How to use this: Click the green flag, then type your answer in the box and press Enter. Keep going until the round ends.
Basic — a sixty-second round 25 min
Open your game from last lesson. Change one thing at a time, and test after each.
Step 1 — two new variables
- Make
livesandtime left. Leave both ticked. - A space in a variable name is fine.
time leftreads better thantl.
Step 2 — swap the loop
- Drag your blocks out of
repeat (5)and throw the repeat away. - Get
repeat untilfrom Control. - Build
<<(timer) > (60)> or <(lives) = (0)>>and drop it in the diamond. - Put your question blocks back inside.
Step 3 — the start of the round
- Above the loop:
set [lives v] to (3), thenreset timer, thenbroadcast [clock v]. - In the else half:
change [lives v] by (-1).
Step 4 — the clock
- Start a new script with
when I receive [clock v]. - Put the same repeat until inside it. Right-click the diamond and Duplicate it.
- Inside:
set [time left v] to (round ((60) - (timer))).
Step 5 — the end
- Under the loop, say the score with a join.
- Play one round well. Then play one getting three wrong fast.
Your two scripts should match the pair in Learnt section 6. The embed there is the finished round.
The round ends straight away.
Look at the diamond. It must be <(timer) > (60)>. With the arrow the other way, the loop is finished before it starts.
time left says 60 and never moves.
The clock script never started. Check the broadcast and the hat both say clock, spelt the same.
I run out of lives but it keeps asking.
Look at the diamond. It must say lives, not score, and or, not and.
time left shows long numbers like 41.7333.
The (round ()) block is missing around the take-away.
A 30-second sprint
Make the round last 30 seconds instead of 60.
- time left starts at 30.
- The round ends on its own at 30 seconds.
- Running out of lives still ends it early.
Teacher note — the number lives in three places
60 appears in the round’s diamond, the clock’s diamond and the clock’s take-away. Change only one and time left runs below zero, or the round stops early. A variable called round length, set once, fixes this for good. Let them discover the need first.
Hurry up!
Make the last ten seconds feel different, so the player knows time is short.
- With 10 seconds or less left, the Wizard changes colour.
- He says “Hurry!” while he waits.
- Before that, he looks normal.
How to use this: Click the green flag to start it. Nothing else to press — watch it through, then use ↺ Reset to run it again.
Teacher reveal — the clock script does the work
The round script is stuck in its ask block, so the change belongs in the clock. The demo above is 20 seconds long; the game uses the same blocks with 60.
when flag clicked
clear graphic effects
reset timer
repeat until <(timer) > (20)>
set [time left v] to (round ((20) - (timer)))
if <(time left) < (11)> then
set [color v] effect to (120)
say [Hurry!]
end
end
clear graphic effects
say [Time's up!] for (2) seconds
Watch for pupils who put the clear graphic effects only at the end. The next round then starts in the hurry colour.
Two endings
The player should know WHY the round ended.
- Running out of time says “Time’s up!” and the score.
- Running out of lives says “Out of lives!” and how many were right.
- Only one ending ever shows, never both.
- Every wrong answer also says how many lives are left.
How to use this: Click the green flag, then type your answer in the box and press Enter. Keep going until the round ends.
Teacher reveal — one if / else after the loop
if <(lives) = (0)> then
say (join [Out of lives! You got ] (join (score) [ right.])) for (4) seconds
else
say (join [Time's up! You scored ] (score)) for (4) seconds
end
Test lives first. A player can lose their last life at 60.2 seconds, and then both are true. Asking about lives first gives the fairer ending.
The wrong-answer line becomes three joins:
say (join [It is ] (join ((a) * (b)) (join [. Lives left: ] (lives)))) for (2) seconds
The change-lives block must come before this say, or it reports one life too many.
Fast answers score double
A right answer given in under 3 seconds is worth 2 points. A slower one is worth 1.
- The Wizard says “Fast! Double points.” for a quick answer.
- Slow right answers still score 1.
- Wrong answers still cost a life, fast or slow.
Teacher reveal
set [asked at v] to (timer)
ask (join (a) (join [ × ] (b))) and wait
if <(answer) = ((a) * (b))> then
if <((timer) - (asked at)) < (3)> then
change [score v] by (2)
say [Fast! Double points.] for (1) seconds
else
change [score v] by (1)
say [Correct!] for (1) seconds
end
else
change [lives v] by (-1)
end
This is the stopwatch trick every racing game uses: never reset the clock, just remember when something started.
Summary 4 min
Today the game got a clock, and a way to lose.
(timer)counts up by itself.reset timersends it back to 0.(round ((60) - (timer)))is the time left, in whole seconds.repeat untilkeeps going until its diamond is true.<<> or <>>ends the round on time OR on lives.- A clock that must keep ticking gets its own script.
- Timer
- Scratch’s stopwatch. It counts seconds, and never stops by itself.
- Round
- To tidy a number to the nearest whole one. 56.7 becomes 57.
- Repeat until
- A loop that stops when something becomes true.
- Or
- True when either side is true.
Next lesson: a streak that makes right answers worth more, and an easy or hard level.