pygame
This game runs as a desktop app
Space Invaders uses pygame.sprite groups, real-time rendering at 60fps, and keyboard polling — none of which exist in a browser environment. Run it locally for the full experience with moving aliens, destructible shields, and progressive levels.
How to run it locally
01Install pygame: pip install pygame
02Download space_invaders.py from the GitHub repo below
03Run it: python space_invaders.py
04Arrow keys to move, Space to shoot, R to restart
space_invaders.py — collision logic
GitHub ↗
"""
PROJECT 8: Space Invaders Clone
=================================
Highlight: Using Pygame's groupcollide to manage complex intersections
between player bullets, enemy bullets, and destructible shields.
"""
def handle_collisions(self):
# Player bullets vs enemies
hits = pygame.sprite.groupcollide(
self.enemies, self.player_bullets, True, True)
for enemy in hits:
self.score += enemy.points
# Enemy bullets vs player
if pygame.sprite.spritecollide(self.player, self.enemy_bullets, True):
self.lives -= 1
if self.lives <= 0:
self.game_over = True
# Bullets vs destructible shields
p_shield_hits = pygame.sprite.groupcollide(
self.shields, self.player_bullets, False, True)
for shield in p_shield_hits:
shield.hit()
e_shield_hits = pygame.sprite.groupcollide(
self.shields, self.enemy_bullets, False, True)
for shield in e_shield_hits:
shield.hit()
Concepts practiced
Spritespygame.sprite.Sprite
OOPPlayer, Enemy, Bullet
Collisionsgroupcollide()
Game Statesreset, level, over
Shootingcooldown timer
Patternsenemy wall bounce
Reflection
I built a Space Invaders clone using pygame's sprite system, organising the player, enemies, bullets, and destructible shields as individual pygame.sprite.Sprite subclasses managed in groups. I learned how pygame.sprite.groupcollide and spritecollide make collision detection between multiple groups clean and efficient, and how to structure a game with proper state management across reset, active, and game-over phases. The hardest debugging problem was shields being destroyed by the wrong bullets; both player and enemy bullets were hitting shields inconsistently. I traced it to the dokilla flags in groupcollide being set incorrectly, and fixed it by carefully setting them to False for shields (so they persist and take damage) and True for bullets (so they are consumed on impact).