- Learnt28 min · a speed that grows, waves, landing, a best score
- Basic24 min · finish the game
- Challenge 1–327 min · the racing last one, three waves, two endings
- Extrabonus · one bonus life
Review — the march 5 min
- The Stage beats a drum. One broadcast moves all forty invaders.
(hit edge)is a flag: any invader raises it, the Stage reads it and lowers it.- The hidden original is row 0, so it never marches.
- A random row and column picks which invader drops a bomb.
Quick-fire
Your game has one army and then stops. Nothing is broken. Why would nobody play it twice?
Reveal the answer
Nothing changes. It never gets harder, and it never really ends. Today it speeds up, comes in waves, and finishes with a win or a loss.
Today’s Topic 3 min
- A beat that gets faster as the army thins
- Three waves, each one faster
- Two endings: a win screen and a lose screen
- A best score the flag never resets
Learning outcome
By the end of this lesson you will be able to:
- Work out a wait from a variable, so a game speeds up by itself.
- Run a game in waves with a counter and a broadcast.
- End a game two ways and keep a best score.
Learnt — a game that ends 28 min
1 · Faster as it thins
In the real game, the last few invaders race. We do not need a new variable for that. We already know how many are left.
Change one block in the drum. Instead of wait (0.5) seconds:
wait ((invaders left) / (80)) seconds
| invaders left | ÷ 80 | feels like |
|---|---|---|
| 40 | 0.5 seconds | last week’s speed |
| 20 | 0.25 seconds | twice as fast |
| 4 | 0.05 seconds | ten times as fast |
| 1 | about 0.01 seconds | racing |
To see it without playing a whole game, this demo lets you zap invaders by clicking them. It is a test stack, like the click test in Lesson 2-41:
when this sprite clicked
if <(my row) > (0)> then
change [invaders left v] by (-1)
delete this clone
end
How to use this: Click the green flag and wait for the army. Then click invaders to zap them — the fewer are left, the faster the rest march.
2 · Waves
A wave is one army. When the last invader of a wave goes, a new, faster army arrives. After wave 3, you win.
- Make
wave. The Stage sets it to 1 on the flag. - Change the builder’s hat to
when I receive [new wave v]. The Stage broadcastsnew waveat the end of its flag stack. - In the invader, the last one now broadcasts
wave clearedinstead ofyou win. - The Stage receives
wave cleared. After wave 3 it broadcastsyou win. Before that, it counts the wave, adds 2 to the step, and calls the next wave.
when I receive [wave cleared v]
if <(wave) = (3)> then
broadcast [you win v]
else
change [wave v] by (1)
change [step v] by (2)
wait (1) seconds
broadcast [new wave v]
end
change [invaders left v] by (-1)
if <(invaders left) = (0)> then
broadcast [wave cleared v]
end
delete this clone
A Banner sprite announces each wave. Its costumes are named “Wave 1”, “Wave 2” and “Wave 3”:
when I receive [new wave v]
switch costume to (join [Wave ] (wave))
go to [front v] layer
show
wait (1.5) seconds
hide
(join [Wave ] (wave)) builds the costume’s name. Mind the space after “Wave”.How to use this: Click the green flag, then click every invader to zap the wave. Watch wave and step go up, and after wave 3 the win screen.
3 · The landing line
The army drops a little at every edge. If it gets low enough, it has landed and you lose. A thin green line just above the ship marks the spot.
- Paint a Ground sprite: one long, thin green line across the Stage.
- On the flag:
go to x: (0) y: (-112), thenwait until <touching [Invader v]?>. - Then
broadcast [game over v]. - Give the Banner a GAME OVER costume and a YOU WIN costume.
when flag clicked
go to x: (0) y: (-112)
wait until <touching [Invader v]?>
broadcast [game over v]
when I receive [game over v]
switch costume to [Game Over v]
go to [front v] layer
show
broadcast [game end v]
when I receive [you win v]
switch costume to [You Win v]
go to [front v] layer
show
broadcast [game end v]
How to use this: Click the green flag and watch. Nothing to press — the army marches down until it touches the green line.
4 · A best score
Both endings broadcast game end. The Stage hears it once, keeps the best score, and stops the game.
when I receive [game end v]
if <(score) > (best)> then
set [best v] to (score)
end
stop [all v]
Basic — finish the game 24 min
Open last week’s game. By the end of Basic it is a whole game with a start and an end.
Step 1 — speed
- Change the drum’s wait to
wait ((invaders left) / (80)) seconds. - Play until ten are left. You should feel the difference.
Step 2 — waves
- Make
wave, and change the builder’s hat. - Update the Stage’s flag stack to match the one below.
- Change
you wintowave clearedin the invader. - Add the Stage’s wave-cleared stack.
- Delete the Ship’s
when I receive [you win v]stack. The Banner does that job now.
when flag clicked
set [score v] to (0)
set [lives v] to (3)
set [wave v] to (1)
set [step v] to (6)
set [frame v] to (0)
broadcast [new wave v]
Step 3 — the Banner
- Paint five costumes: Wave 1, Wave 2, Wave 3, You Win, Game Over.
- Spell the wave ones exactly: “Wave”, a space, the number.
- Give it the three receive stacks.
Step 4 — the endings
- Add the Ground sprite and its stack.
- Change the Ship: at no lives left it still broadcasts
game over. The Banner now answers. - Make
best, and add the Stage’sgame endstack. - Play three games in a row. The best score must only ever go up.
The wave banner says nothing, or shows the wrong costume.
The costume name and the join do not match. “Wave 2” and “Wave2” are different names. Check the space inside (join [Wave ] (wave)).
Wave 2 arrives, but the army never moves.
The builder’s last line, broadcast [army ready v], is missing, so the drum never starts again.
Wave 2 has eighty invaders in it.
The builder is still on when flag clicked as well as when I receive [new wave v], so it runs twice at the start.
The game ends the moment it starts.
The Ground is too high, or too thick, and the army is touching it already. Move it down under the front row’s drop path, just above the ship.
best goes back to 0.
Something sets it on the flag. Look for set [best v] to (0) and delete it.
The last one races
- A full army marches at last week’s speed.
- Every hit makes the rest a little faster.
- The last invader of a wave is the fastest thing in the game.
Three waves
- Clearing an army brings the next one.
- The Banner says which wave is coming.
- Each wave steps further on every beat than the one before.
- Score and lives carry on from wave to wave.
Teacher note — test with a small army
Clearing forty invaders three times takes a while. Pupils can test the waves with a 2 × 4 army — change the two repeats — then put 5 × 8 back. Changing a number to test faster is a real game-maker’s habit, worth naming out loud.
Two endings and a record
- After wave 3, YOU WIN shows and the game stops.
- If the army lands, or your lives run out, GAME OVER shows.
- The best score is kept from game to game.
- The flag always starts a fresh game: wave 1, three lives, score 0.
Teacher reveal — one message after two endings
Pupils often copy the best-score check into both endings. It works, but now there are two copies to keep in step. Both endings broadcasting game end, and one stack on the Stage answering it, is the tidy version. It is the same idea as the drum: one place decides.
How to use this: Click the green flag, click the stage once, then hold ← or → to move and the space bar to fire. Clear three waves to win.
One bonus life
Arcade games reward a good player with a spare life. Yours will too — but only once.
Teacher notes — the laser switch, again
This is Lesson 2-42’s switch in a new place. A variable bonus given says whether the life has been handed out. The check needs both halves of the <<> and <>>: without the switch, the life is given on every loop once the score is past 1000.
when flag clicked
set [bonus given v] to (0)
forever
if <<(score) > (999)> and <(bonus given) = (0)>> then
change [lives v] by (1)
set [bonus given v] to (1)
say [Bonus life!] for (1) seconds
end
end
A sharp pupil will point out that wait until <(score) > (999)> also gives it only once. True — and worth praising. The switch is the version that still works when a game can give the bonus back, such as a “reset bonus” each wave.
Summary 5 min
- A wait worked out from a variable makes a game speed up by itself:
wait ((invaders left) / (80)) seconds. - A wave is a counter and two broadcasts: cleared, and new.
- Adding to the step each wave is levelling up.
- Two endings can share one message. One place keeps the best score.
- A variable the flag never resets remembers between games.
- Wave
- One army. Clear it and the next one comes, faster.
- Difficulty
- How hard the game is. Here it grows as you win.
- Win screen
- What the player sees when they have beaten the game.
- Best score
- The highest score so far. The flag does not reset it.
That is Space Invaders
Four lessons ago it was one hidden sprite. Now it is a grid of forty clones that each know their place, a laser with a switch, an army that marches to one drum, bombs, lives, waves and two endings.
Share it
Give it a title and instructions on its project page, share it on the Scratch website, and send the link to your teacher. Play it once more tomorrow, cold, and fix the first thing that annoys you.