Editor

March the wave

Pygame challenges · Alien Wave, step 3 of 4 · 30 XP

The wave marches off the side of the screen and never comes back.

Your task

Each alien is a rectangle (x, y, width, height), the same four numbers a pygame.Rect holds. The wave moves as one: every alien steps dx pixels sideways. When any alien would cross an edge, the whole wave steps down by drop instead, and dx turns around (-dx), so the next steps go back the other way.

Write move_wave(aliens, dx, left, right, drop). An alien would cross an edge if its new left side, x + dx, is less than left, or its new right side, x + width + dx, is more than right. Return two things, (new_aliens, new_dx): the moved aliens as a new list of tuples in the same order, and the new dx. Leave the list you were given as it was.

Look first, then move. One loop checks every alien, and only after it has finished do you move them, all the same way. If each alien decided for itself, the one at the edge would step down while the others marched on, and the wave would fall apart.

The game calls aliens, dx = move_wave(aliens, dx, 8, 472, 12) a few times a second, and more often as the wave gets smaller. Your can_fire and move_bullets run the cannon.

The function is at the top of the program, marked fix this. Press Run to play the game as it is, then Check when your function is ready.

What Check tests

CallShould give
the wave moves rightmove_wave([(10, 20, 28, 20), (50, 20, 28, 20)], 8, 0, 200, 12)([(18, 20, 28, 20), (58, 20, 28, 20)], 8)
the wave moves leftmove_wave([(30, 20, 28, 20)], -8, 0, 200, 12)([(22, 20, 28, 20)], -8)
one alien at the right edge: all step down and turnmove_wave([(10, 20, 28, 20), (166, 40, 28, 20)], 8, 0, 200, 12)([(10, 32, 28, 20), (166, 52, 28, 20)], -8)
the left edgemove_wave([(4, 20, 28, 20), (100, 20, 28, 20)], -8, 0, 200, 12)([(4, 32, 28, 20), (100, 32, 28, 20)], 8)
landing exactly on the edge is allowedmove_wave([(164, 20, 28, 20)], 8, 0, 200, 12)([(172, 20, 28, 20)], 8)
the list given is unchangedbefore = [(166, 40, 28, 20)] move_wave(before, 8, 0, 200, 12)before[(166, 40, 28, 20)]