Space Invaders: Coach notes
Goal of the challenge
A ninja has finished the challenge when:
- ✅ Basic: the ship moves, the formation moves back and forth and drops down, the player can shoot with a cooldown.
- ⭐ Stretch: bullets hit aliens (score), aliens shoot back at random, lives go down on a hit; game-over screen.
- 🔥 Expert: formation speeds up with fewer aliens, game over on landing, restart with R.
Preparation
- Give the ninja the starter code. It contains
main.pyand theimages/folder with ship.png, enemy.png and laser.png. - Window, clock, font, colours, constants, loading images and
maak_vijanden()are already in the starter code. - Check that pygame is available on the laptop (see Installing Thonny).
Common questions
“The aliens move, but they don’t drop down”
The edge check has to come after the move, not before it. The ninja first works out the new rechts/links after shifting, and only then the edge check. Check that alien.y += ALIEN_VAL really sits inside the if block.
“All the aliens drop down twice at once” One frame can touch both the right edge and the left edge (if the formation is wide and crosses both at the same time). Fix: check the edge once per frame, not once per alien. Move the edge check outside the alien loop.
“The bullet only fires once”
The ninja removes the bullet as soon as it leaves the screen, but forgets to remove it when it hits an alien. Or the other way round: the bullet disappears after the collision, but the alien doesn’t. Check both remove() calls.
“I’m looping over the list while removing from it”
for k in speler_kogels while you call speler_kogels.remove(k) skips items you don’t expect. Use for k in speler_kogels[:] (a copy of the list) so the original list changes safely.
“The player shoots insanely fast”
COOLDOWN_MS = 500 has to be checked through pygame.time.get_ticks(). Check that the ninja keeps track of laatste_schot and compares it with nu - laatste_schot >= COOLDOWN_MS before adding a bullet.
“The score goes up but the text doesn’t show”
font.render() returns a Surface. It still has to be put on the screen with screen.blit(). Check that the ninja didn’t forget the second step.
Hints per level
✅ Basic
- Stars:
random.seed(42)→ 70×pygame.draw.circlewithrandom.randint→random.seed()to reset. That way the stars don’t flicker. - Alien movement: loop over
aliens, doalien.x += ALIEN_SNL * alien_richting. Then: work outrechts = max(a.right for a in aliens)andlinks = min(a.left for a in aliens). Ifrechts >= BREEDTE or links <= 0: flipalien_richtingand doalien.y += ALIEN_VALfor every alien. - Shooting: check
pygame.time.get_ticks() - laatste_schot >= COOLDOWN_MS. Add apygame.Rectabove the ship:pygame.Rect(speler.centerx - KOGEL_B // 2, speler.top - KOGEL_H, KOGEL_B, KOGEL_H).
⭐ Stretch
- Collision bullet ↔ alien:
for k in speler_kogels[:]: for alien in aliens[:]: if k.colliderect(alien): remove k, remove alien, score += 10, break. - Aliens shooting:
for alien in aliens: if random.random() < SCHIET_KANS: add bullet below alien. - Lives:
for k in alien_kogels[:]: if k.colliderect(speler): remove k, levens -= 1; if levens <= 0: game_over = True.
🔥 Expert
- Speed:
restant = len(aliens) / (RIJEN * KOLOMMEN); snelheid = max(1, round(1 + 3 * (1 - restant))). Recalculate this every frame before the alien loop. - Landing:
for alien in aliens: if alien.bottom >= HOOGTE - 50: game_over = True; break. - Reset: make a
reset()function that sets all theglobalvariables again. Call it onpygame.K_rwhengame_overis True.