- Learnt28 min · my speed, my lane, the bug, driving left, lives
- Basic24 min · turn the draft into a game
- Challenge 1–327 min · three speeds, two ways, three lives
- Extrabonus · hearts for lives
Review — the first crossing 5 min
- One hidden Car sprite makes every car as a clone.
- A clone is born as a copy of its parent: same place, same costume.
- A hop is a hat block:
when [up arrow v] key pressed. touching [Car v]?sends the frog back to the start.
Quick-fire
You want lane 2 to drive left. But every car runs the same clone stack. What does a car need?
Reveal the answer
Its own number. Each car must carry its own speed. Today it does.
Today’s Topic 3 min
- “For this sprite only” — a variable each clone keeps for itself
(my speed)and(my lane), set at birth- The bug you get with “for all sprites”
- A lane that drives left
- Three lives, reset on every flag, and a game over
Learning outcome
By the end of this lesson you will be able to:
- Give every clone its own speed with a “for this sprite only” variable.
- Make one sprite drive lanes at different speeds and in two directions.
- Count lives, reset them on the flag, and end the game at zero.
Learnt — every car its own speed 28 min
One stack, one speed
Last lesson every clone ran this block: change x by (3). Every car, in every lane, went right at 3. A real road is not like that.
A variable for this sprite only
You met this in Lesson 2-10. Every zombie had its own (hp). When you make a variable, Scratch asks a question:
- For all sprites — one box. Everything shares it.
- For this sprite only — every clone gets its own copy.
- Click the Car sprite. Open Variables.
- Click Make a Variable. Type
my speed. - Tick For this sprite only. Click OK.
- Do it again for
my lane. - Untick both boxes in the palette. Clones do not need a monitor each.
Set it, then clone
The lane stack sets the two numbers just before it makes the clone. The clone is born holding a copy of both.
when flag clicked
forever
go to x: (-230) y: (-120)
set [my lane v] to (1)
set [my speed v] to (2)
create clone of [myself v]
wait (2.5) seconds
end
when flag clicked
forever
go to x: (230) y: (-80)
set [my lane v] to (2)
set [my speed v] to (-1.5)
create clone of [myself v]
wait (4) seconds
end
when flag clicked
forever
go to x: (-230) y: (-40)
set [my lane v] to (3)
set [my speed v] to (3.5)
create clone of [myself v]
wait (2) seconds
end
A second later the parent sets different numbers for the next lane. The clone does not care. Its copy is its own.
The clone reads its own numbers
when I start as a clone
switch costume to (my lane)
if <(my speed) < (0)> then
point in direction (-90)
end
show
repeat until <<(x position) > (235)> or <(x position) < (-235)>>
change x by (my speed)
end
delete this clone
| Block | What it does with the clone’s own number |
|---|---|
switch costume to (my lane) | Lane 1 wears costume 1, lane 2 costume 2, lane 3 costume 3. |
if <(my speed) < (0)> then | A speed below zero means “going left”, so turn to face left. |
change x by (my speed) | 2 goes right slowly. −1.5 goes left. 3.5 goes right fast. |
<<(x position) > (235)> or <(x position) < (-235)>> | Off the right edge or off the left edge. Either way, it is done. |
One more block goes in the setup stack: set rotation style [left-right v]. Without it, a truck pointing left drives upside down.
How to use this: Click the green flag to start it. Nothing else to press — watch the three lanes, then use ↺ Reset to run it again.
The bug, on purpose
Now try it wrong, so you can see why the tick box matters. Here (my speed) is “for all sprites”.
How to use this: Click the green flag to start it. Nothing else to press — watch every car change speed at once, then use ↺ Reset to run it again.
Three lives
(lives) is the opposite kind. There is one frog and one game, so there is one number. Make it for all sprites.
| Variable | Kind | Why |
|---|---|---|
(my speed) | For this sprite only | Every car needs its own. |
(my lane) | For this sprite only | Every car needs its own. |
(lives) | For all sprites | The whole game shares one. |
A hit now costs a life:
when flag clicked
go to x: (0) y: (-160)
forever
if <touching [Car v]?> then
change [lives v] by (-1)
go to x: (0) y: (-160)
end
end
And one stack resets the lives, then watches them:
when flag clicked
set [lives v] to (3)
wait until <(lives) < (1)>
stop [other scripts in sprite v]
say [Game over!] for (2) seconds
stop [all v]
set [lives v] to (3)— every new game starts full. Put it here, above the wait, so the wait never sees last game’s 0.wait until <(lives) < (1)>— sits quietly all game.stop [other scripts in sprite v]— the frog stops checking for cars. No more lives are lost.say [Game over!] for (2) seconds— the player reads it.stop [all v]— then everything stops.
How to use this: Click the green flag, then click the Stage once and tap ↑ ↓ ← → to hop the Frog across.
Basic — turn the draft into a game 24 min
Open your crossing from last lesson. Test after every step.
Step 1 — the two variables
- On the Car, make
my speedandmy lane. - Tick For this sprite only for both.
Step 2 — set them in each lane
- In each lane stack, add the two set blocks between the go to and the clone.
- Lane 1: lane 1, speed 2. Lane 2: lane 2, speed −1.5. Lane 3: lane 3, speed 3.5.
- Change lane 2’s go to x from −230 to 230.
Step 3 — the clone uses them
- Drop
(my lane)into the costume block. - Drop
(my speed)intochange x by (3). - Add the if-block that points left, and the “or” test at the top of the repeat.
- Add
set rotation style [left-right v]to the setup stack.
Step 4 — check the costumes
Lane 2 must wear the truck. If it wears a car, drag the truck costume into second place.
Step 5 — lives
- Make
lives, For all sprites. Leave its box ticked. - Add the change block to the frog’s hit check.
- Add the game-over stack to the frog.
Step 6 — lose on purpose
Hop into three cars. Then click the flag again. Lives must be back to 3.
How to use this: Click the green flag, then click the Stage once and tap ↑ ↓ ← → to hop the Frog across.
Every car changes speed at the same moment.
my speed is “for all sprites”. Delete it and make it again with “for this sprite only” ticked.
The trucks drive left upside down.
The setup stack is missing set rotation style [left-right v].
The trucks appear and vanish at once.
The repeat only checks <(x position) > (235)>. A truck going left never gets there. Use the “or” test for both edges.
The game says Game over! the moment I click the flag.
set [lives v] to (3) is in a different stack. That stack and the wait race each other. Put the set in the same stack, above the wait.
Three speeds
- Still one Car sprite.
- Each lane moves at its own speed: one slow, one medium, one fast.
- No car ever changes speed once it is on the road.
One lane the other way
- The middle lane drives from right to left.
- Its vehicles face the way they drive, and stay the right way up.
- They disappear off the left edge, not the right.
How to use this: Click the green flag to start it. Nothing else to press — watch the three lanes, then use ↺ Reset to run it again.
Teacher note
Some pupils will make a second Truck sprite for the left lane. It works, and it is worth asking: what happens when we want a fourth lane? The point of today is one sprite and a number per clone. A pupil who instead checks <(y position) = (-80)> inside the clone has found a real second answer — say so.
Three lives
- Lives are shown on the Stage.
- A car costs one life and sends the frog back.
- At zero the player is told, and the game stops.
- The next green flag starts a fresh game with 3.
Teacher reveal — the frog’s stacks
when flag clicked
go to x: (0) y: (-160)
forever
if <touching [Car v]?> then
change [lives v] by (-1)
go to x: (0) y: (-160)
end
end
when flag clicked
set [lives v] to (3)
wait until <(lives) < (1)>
stop [other scripts in sprite v]
say [Game over!] for (2) seconds
stop [all v]
The most common bug is the reset in a third stack. Two flag stacks start in the same moment, and nobody can promise which runs first. Keep the set and the wait together.
Hearts, not numbers
A number in a grey box is fine. Hearts are better. Show the lives as hearts in the corner.
- Three hearts at the start.
- One goes each time a car hits the frog.
- The lives monitor is hidden.
How to use this: Click the green flag, then click the Stage once and tap ↑ ↓ ← → to hop the Frog across.
Teacher reveal — the Heart sprite
when flag clicked
hide
forever
if <(lives) > (0)> then
go to x: (-205) y: (150)
create clone of [myself v]
end
if <(lives) > (1)> then
go to x: (-172) y: (150)
create clone of [myself v]
end
if <(lives) > (2)> then
go to x: (-139) y: (150)
create clone of [myself v]
end
end
when I start as a clone
show
wait (0.03) seconds
delete this clone
The same pattern as Lesson 2-10, and that is the lesson: a trick you used once is a trick you own.
Summary 5 min
- For this sprite only gives every clone its own copy of a variable.
- Set the numbers, then make the clone. It is born holding a copy.
- A speed below zero drives left.
- For all sprites is one box. Use it for things the whole game shares, like
lives. - Reset a variable in the same stack that watches it.
- For this sprite only
- A variable each clone owns separately.
- For all sprites
- One variable, shared by every sprite and clone.
- Set at birth
- A number given to a clone as it is made, which it keeps.
- Lives
- How many more mistakes you can make. Zero ends the game.
Hand in
Share your game and send the link to your teacher. Next lesson the park floods. You will need logs.