How to Code Space Invaders in Scratch — Grids, Aliens, and Making 55 Enemies Move as One
55 aliens. 11 columns. 5 rows.
Getting them to move as one tight army — and turn around the moment any of them hits the edge — is the puzzle at the heart of Space Invaders. It sounds complicated. But the trick behind it is one of the most satisfying things you can build in Scratch.
▶ Watch: Scratch Game Dev — Making Your Own Space Invaders
Start with the grid
Space Invaders has a very specific look: 11 aliens across, 5 rows deep. That's not random — it's a grid. Knowing it's a grid is the first step to building it.
In Scratch, you don't draw 55 separate alien sprites. You draw one, then use clones.
A clone is an exact copy of a sprite that can act on its own. Create one alien, clone it 54 times, position each one in a grid pattern — and you've got your army without repeating yourself 55 times.
Here's how to lay out a single row:
Set x to -220
Repeat 11 times:
Create clone of myself
Change x by 40
Do that 5 times with a different starting y position for each row, and your 11×5 army appears on screen.
The tricky bit: moving as one
Individual movement is easy. Moving 55 clones in the same direction at the same time? That's the puzzle.
Each clone runs its own copy of the code. So how do you get all 55 to go the same way, turn around at the same time, and drop down when they reach the edge?
The answer: a shared variable.
Create a variable called direction. Set it to 1 for "moving right", -1 for "moving left". Every clone reads this variable and moves accordingly:
Move (direction × 2) steps
All 55 clones read the same number, so they all move the same way. Change direction from 1 to -1, and the whole army reverses — instantly, at the same time.
Who tells the army to turn?
No single clone is in charge. But any clone can trigger the turn.
When a clone touches the right edge of the screen, it broadcasts a message — something like "turn around". Every other clone hears it. The code that responds to this message flips the direction variable and drops the whole army down one row:
When I receive "turn around":
Set direction to -1
Change y by -20
One clone hits the wall. All 55 react.
This idea — one part of your program notices something and broadcasts a signal that others respond to — shows up in almost every game you'll ever build. In Scratch it's called a broadcast. In other languages it goes by different names, but the shape is the same.
Why this matters beyond Space Invaders
What you're really learning here is how to make things that share information.
The aliens don't each track their own direction. They all read from one place. Change it once, and everyone updates. That's clean, efficient, and it makes the code much easier to understand.
You'll see this same idea in:
- Multiplayer games — all players seeing the same game state at the same time
- Apps — a "dark mode" setting that changes the look of every page at once
- Anything where multiple moving parts need to stay in sync
Scratch makes this easy to feel. You can see the 55 clones marching together. You can break the variable and watch them go haywire. That's how the pattern sticks.
Watch the full build
▶ Watch: Scratch Game Dev — Making Your Own Space Invaders
This is Episode 4 of the Scratch series — the same series that walked through Pong, Breakout, and Frogger. Each game teaches one core pattern. Space Invaders is the grid-and-clones episode.
Want to try it yourself? Scratch is free and runs in your browser at scratch.mit.edu.