Learning LibraryGame Development LibraryKids

Enemy waves: spawn a whole group with a loop

One enemy is easy. A whole wave of them, evenly spaced? That's a job for a loop — it builds the group and a list keeps them all together.

The big idea

An enemy wave is a list of enemies built by a loop, so you can make the whole group at once.

See it in code

1Start simple

A wave starts as an empty list. A loop runs three times, and each time it appends one enemy to the list:

python
wave = []
for count in range(3):
    wave.append("enemy")

print("Wave has", len(wave), "enemies")
Run it — the loop fills the list for us:
Wave has 3 enemies

The loop did the adding, and the list holds the group. Change the 3 and you change how many you get.

2A step further

Now real enemies. The loop makes three Enemy sprites, each a little further right, and keeps them in the wave list. Then a second loop draws them all:

python
from game import Stage, Enemy

screen = Stage.create()
Stage.clear(screen)

wave = []
for col in range(3):
    wave.append(Enemy(120 + col * 120, 90))

for enemy in wave:
    enemy.draw(screen)
Run it — one loop builds the wave, another draws it:
A row of three purple enemy sprites evenly spaced near the top of a dark play area.

Two loops, two jobs: one builds the wave, one draws it. No enemy was placed by hand.

3In our world

Now a bigger wave, with a spacing rule. The loop runs six times, placing each Enemy at 55 + col * 74 so they line up evenly — and a second loop draws them all:

python
from game import Stage, Enemy

screen = Stage.create()
Stage.clear(screen)

wave = []
for col in range(6):
    wave.append(Enemy(55 + col * 74, 90))

print("Wave has", len(wave), "enemies")
for enemy in wave:
    enemy.draw(screen)
Run it — one loop builds the wave, another draws it:
Wave has 6 enemies
A row of six purple enemy sprites evenly spaced near the top of a dark play area.

Six enemies, no copy-paste. The loop placed each one, and the wave list holds them all — so later you can move or check the whole group with one more loop.

The same idea, everywhere

Loops-plus-lists build any group: a row of coins, a deck of cards, a crowd of characters, a field of stars. Make them with a loop, keep them in a list, and handle them all together.

Try it yourself

Change range(6) to range(9) and shrink the spacing (* 74 to * 50) so nine fit. Then add a second row by wrapping it all in another loop that changes the y.

The common mistake

Making the wave but forgetting the second loop to draw it. Building the list fills wave with enemies, but you still have to loop over it and draw each one, or nothing shows up.

What it unlocks

Waves combine for loops, lists, and list methods to command sprites as a group.