Editor

Shoot down the aliens

Pygame challenges · Alien Wave, step 4 of 4 · 40 XP

Bullets fly straight through the aliens.

Your task

A bullet hits an alien when its tip (x, y) is inside the alien's rectangle. pygame can check that for you: pygame.Rect(alien).collidepoint(bullet) is True when the point is inside (the left and top edges count as inside, the right and bottom edges do not).

Write hits(bullets, aliens). Go through the bullets in order. A bullet inside an alien knocks that alien out and is used up, so neither goes into the lists you return. A bullet takes at most one alien, the first one in the list that it is inside, and an alien already knocked out cannot be hit again by a later bullet. Each alien knocked out is worth 10 points.

Return three things, (bullets_left, aliens_left, points): the bullets that hit nothing, the aliens still flying (each list in its old order) and the points scored. Leave the lists you were given as they were: copy aliens with list(aliens) and remove from the copy.

The game calls bullets, aliens, points = hits(bullets, aliens) every frame and adds points times the wave number to your score. It is the last piece: with it, the waves speed up, the aliens drop bombs, and you have three lives.

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
a miss changes nothinghits([(100, 100)], [(10, 10, 28, 20)])([(100, 100)], [(10, 10, 28, 20)], 0)
a hit knocks out the alien and uses up the bullethits([(20, 15)], [(10, 10, 28, 20), (60, 10, 28, 20)])([], [(60, 10, 28, 20)], 10)
two bullets, two alienshits([(70, 25), (20, 15)], [(10, 10, 28, 20), (60, 10, 28, 20)])([], [], 20)
one bullet takes only the first alien it is insidehits([(20, 15)], [(10, 10, 28, 20), (15, 12, 28, 20)])([], [(15, 12, 28, 20)], 10)
an alien cannot be hit twicehits([(20, 15), (22, 25)], [(10, 10, 28, 20)])([(22, 25)], [], 10)
the lists given are unchangedb = [(20, 15)] a = [(10, 10, 28, 20)] hits(b, a)(b, a)([(20, 15)], [(10, 10, 28, 20)])