- Learnt27 min · a switch, a hit, points by row, a counter
- Basic25 min · arm the ship
- Challenge 1–327 min · one at a time, the back row pays, clear the army
- Extrabonus · the mystery saucer
Review — the army from one sprite 5 min
- A repeat inside a repeat builds a grid of clones.
(my row)and(my column)are “For this sprite only”. Every clone keeps its own copy.switch costume to (my row)gives each row its own look.- Two comparisons keep the ship on the Stage.
Quick-fire
You make the space bar fire a laser. You hold it down. What goes wrong?
Reveal the answer
A laser every frame — a solid beam. Today we fix it with a switch: a variable that says whether you may fire.
Today’s Topic 3 min
- A switch: one laser at a time
- An invader that knows when it is hit
- Points that come from the row
- Counting what is left
Learning outcome
By the end of this lesson you will be able to:
- Use a variable as a switch: 1 for yes, 0 for no.
- Remove a clone and the thing that hit it, with one broadcast.
- Work out a score from a clone’s own variable.
Learnt — a laser with rules 27 min
1 · One laser at a time
A switch is a variable with only two values. We call ours (laser ready). 1 means “you may fire”. 0 means “wait”.
- Paint a sprite called Laser: a thin bar, about 6 wide and 18 tall.
- Make
laser ready, For all sprites. The Ship and the Laser both need it. - On the Ship, a second flag stack:
set [laser ready v] to (1)and aforever. - Inside: an
if <> thenwith an<<> and <>>— space pressed, and laser ready is 1. - Inside the if:
set [laser ready v] to (0)first, thencreate clone of [Laser v].
when flag clicked
set [laser ready v] to (1)
forever
if <<key [space v] pressed?> and <(laser ready) = (1)>> then
set [laser ready v] to (0)
create clone of [Laser v]
end
end
The laser’s job is to fly up, then switch the ship back on:
when flag clicked
hide
when I start as a clone
go to [Ship v]
show
repeat until <(y position) > (170)>
change y by (10)
end
set [laser ready v] to (1)
delete this clone
How to use this: Click the green flag, click the stage once, then hold the space bar to fire. Hold ← or → to move the ship.
2 · A hit
When a laser touches an invader, both must go. But only one of them should do the noticing. If the laser deleted itself first, the invader would never see it.
So the invader notices. Then it tells the laser with a broadcast.
- In the invader’s clone stack, after
show, addwait until <touching [Laser v]?>. - Then
broadcast [laser hit v], the points, anddelete this clone. - On the Laser:
when I receive [laser hit v], switch the ship back on, and delete.
when I start as a clone
switch costume to (my row)
show
wait until <touching [Laser v]?>
broadcast [laser hit v]
change [score v] by (((6) - (my row)) * (10))
change [invaders left v] by (-1)
delete this clone
when I receive [laser hit v]
set [laser ready v] to (1)
delete this clone
Points by row
The back row should pay the most. Each invader already knows its row, so it can work out its own price:
| my row | 6 − my row | × 10 |
|---|---|---|
| 1 (back) | 5 | 50 |
| 2 | 4 | 40 |
| 3 | 3 | 30 |
| 4 | 2 | 20 |
| 5 (front) | 1 | 10 |
3 · Counting what is left
Scratch cannot count clones for you. And a deleted clone cannot count itself. So we count as we make them, and count down as they go.
- Make
invaders leftandscore, both For all sprites. - In the builder, before the loops:
set [invaders left v] to (0). - Next to
create clone of [myself v]:change [invaders left v] by (1). - On the Stage:
when flag clickedthenset [score v] to (0).
when flag clicked
hide
set [invaders left v] to (0)
set [my row v] to (1)
repeat (5)
set [my column v] to (1)
repeat (8)
go to x: (((my column) * (40)) - (180)) y: ((172) - ((my row) * (32)))
create clone of [myself v]
change [invaders left v] by (1)
change [my column v] by (1)
end
change [my row v] by (1)
end
when flag clicked
set [score v] to (0)
How to use this: Click the green flag, click the stage once, then hold ← or → to aim and the space bar to fire.
When it reaches 0
The invader that brings the count to 0 is the last one. It can say so, just before it goes:
change [invaders left v] by (-1)
if <(invaders left) = (0)> then
broadcast [you win v]
end
delete this clone
when I receive [you win v]
say [You win!]
Basic — arm the ship 25 min
Open last week’s project. The army and the ship are there already.
Step 1 — the laser
- Paint the Laser sprite. Make it thin and bright.
- Give it the flag stack and the clone stack.
- Make the switch and the Ship’s fire stack.
- Hold space. Count the lasers in the air: it must always be one.
Step 2 — the hit
- Add the wait-until, broadcast and delete to the invader’s clone stack.
- Add the Laser’s
when I receive [laser hit v]stack. - Shoot one invader. Both must vanish, and you must be able to fire again.
Step 3 — the score
- Make
scoreand reset it on the Stage. - Add the points sum to the invader.
- Shoot a front-row invader: +10. Now a back-row one: +50.
Step 4 — the counter and the win
- Add the two counting blocks to the builder.
- Add the count-down and the
if <> thento the invader. - Add
when I receive [you win v]to the Ship. - Delete the click test from last week. It has done its job.
Holding space gives a stream of lasers.
The fire stack is missing set [laser ready v] to (0), or the <<> and <>> is not checking the switch.
I can only fire once, ever.
Nothing switches the ship back on. The laser needs set [laser ready v] to (1) in two places: at the top, and when it receives laser hit.
The laser flies straight through the invaders.
The wait until <touching [Laser v]?> is on the flag stack, not the clone stack. Only clones are ever visible to hit.
Every hit is worth the same.
my row is For all sprites again. Every invader reads 6, and 6 − 6 is 0.
invaders left starts at 80 on the second game.
set [invaders left v] to (0) is missing, so the count from the last game carries on.
One at a time
- Space fires one laser from the ship.
- While it is in the air, space does nothing.
- The moment it is gone, the next one can fire.
The back row pays
- The front row is worth 10.
- Each row further back is worth 10 more.
- The back row is worth 50.
- The score starts at 0 every game.
Teacher note — two ways, one better
Some pupils write five if <> then blocks, one per row. It works. Then ask: what if the army had ten rows? The sum (((6) - (my row)) * (10)) does every row in one block. Let them find the 6 themselves — try 5 first and see what the back row pays.
Clear the army
- invaders left starts at 40 and counts down on every hit.
- When the last invader goes, the game says you won.
- Press the flag again and everything starts fresh.
Teacher reveal — why the invader checks for 0
A pupil’s first idea is often a wait until <(invaders left) = (0)> on the Stage. On the flag it is already 0, before the first clone is made — so the game wins at once. Checking inside the invader, right after the count-down, can only fire on a real last hit.
How to use this: Click the green flag, click the stage once, then hold ← or → to aim and the space bar to fire.
The mystery saucer
Now and then a saucer crosses the very top of the Stage. Hit it for a surprise prize.
How to use this: Click the green flag, click the stage once, then hold ← or → to move and the space bar to fire. The saucer crosses every 5 to 10 seconds.
Teacher notes — the Shooting Game walker, again
This is the zombie spawner from Lesson 2-9: a hidden original that waits a random time and makes a clone, and a clone that walks until it is across or hit. Pupils who see that have understood clones, not just copied them.
when flag clicked
hide
forever
wait (pick random (5) to (10)) seconds
go to x: (-225) y: (165)
create clone of [myself v]
end
when I start as a clone
show
repeat until <(x position) > (225)>
change x by (3)
if <touching [Laser v]?> then
broadcast [laser hit v]
change [score v] by ((pick random (1) to (6)) * (50))
delete this clone
end
end
delete this clone
broadcast [laser hit v] is the same message the invaders send, so the laser needs no new blocks at all.
Summary 4 min
- A switch is a variable with two values: 1 for yes, 0 for no.
- Switch off as you fire. Switch on when the laser is gone.
- When two sprites touch, let one notice and tell the other with a broadcast.
- A clone can work out its own price from its own variable:
(((6) - (my row)) * (10)). - Deleted clones cannot be counted. Count up as you make them, down as they go.
- Switch
- A variable that is only ever 1 or 0 — on or off.
- Hit
- The moment two sprites touch in a game.
- Counter
- A variable that goes up or down by one to keep count.
- Checkpoint
- A version of your game that works and is worth saving.
Next lesson: the army marches — all forty together — and fires back.