A classic space shooter game built with Pygame. Defend Earth from alien fleets and achieve the highest score!
- Python 3.8+
- Either uv or plain
python3 -m venv+pip
uv manages the virtual environment and installs pygame-ce for you, so it
works on Linux distros that block system-wide pip (PEP 668 /
"externally-managed-environment") and on Windows and macOS:
uv run alien_invasion.py --list-players # first run resolves+installs deps
uv run alien_invasion.py # play the gameAll other flags work too, e.g. uv run alien_invasion.py --windowed.
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
python alien_invasion.py
requirements.txtpinspygame-ce, a drop-inpygamereplacement with prebuilt wheels for modern Python on Linux, Windows and macOS.
python alien_invasion.py # fullscreen
python alien_invasion.py --windowed # 1200x800 window
python alien_invasion.py --windowed 800x600 # custom window size
python alien_invasion.py --player Ace --difficulty hard
python alien_invasion.py --list-players # show saved players, then exit
python alien_invasion.py --delete-player Ace # remove a player, then exitIf you installed with uv, replace python with uv run in the commands
above, e.g. uv run alien_invasion.py --windowed 800x600.
--player creates the player if they don't exist yet.
| Key | Action |
|---|---|
| ← → | Move spaceship left/right |
| SPACE | Auto-fire (hold) |
| P | Pause/Unpause game |
| M | Mute/Unmute sound |
| ENTER | Start new game |
| Q | Quit game (saves your progress) |
On the start screen you also manage players:
| Key | Action |
|---|---|
| N | New player (type a name, ENTER to confirm, ESC cancels) |
| TAB | Switch to the next player |
| D | Cycle difficulty: normal → hard → easy |
| DEL | Delete the current player |
- 🚀 Progressive difficulty: Speed increases with each level, at a steady 60 FPS
- 🖥️ Fullscreen or windowed (
--windowed) - 💥 Explosion effects for alien/ship destruction
- 🔊 Sound effects and background music
- 🌟 Starry animated background
- 👥 Player profiles: each player keeps their own high score, best level, games played and difficulty
- 🎚️ Three difficulties: easy, normal (the original balance) and hard
- 🏆 Top-pilots leaderboard on the start screen
- ⏸️ Pause functionality and a mute toggle
Profiles live in profiles.json next to the game. An older high_score.txt
is imported once into the first profile.
python -m pytest tests/ -v # headless, no display or audio needed
python tests/coverage_report.py # line coverage per module (stdlib only)With uv: uv run --with pytest python -m pytest tests/ -v.
- Windows: the game runs unchanged. Use
pygame-cefromrequirements.txt(it ships Windows wheels), and note that double-clicking runs fullscreen — add--windowed(or a shortcut that passes it) if you prefer a window. - Linux (Wayland): the game prefers SDL's native Wayland backend to avoid a hard crash in the X11/GLX path on some NVIDIA setups.
X Error ... BadValue ... GLX/ black window on Linux: force a video backend with theSDL_VIDEODRIVERvariable, e.g.SDL_VIDEODRIVER=wayland uv run alien_invasion.py --windowedorSDL_VIDEODRIVER=x11 .... The game already picks Wayland automatically when it detects a Wayland session.- Fullscreen fails to open: run with
--windowed(it also falls back to a scaled window automatically when a requested mode is unavailable). error: externally-managed-environment(pip): use uv (Option A) or avenv(Option B) above instead of systempip.- If sounds don't play:
- Ensure
.wav/.oggfiles exist insounds/ - Check system volume/mute status
- Ensure
- If missing images:
- Verify ship and alien images exist in
images/
- Verify ship and alien images exist in
- On Linux, install SDL dependencies (only if your
pygame-cebuild needs them):
sudo apt-get install python3-dev libsdl2-dev libsdl2-image-dev libsdl2-mixer-devDestroy alien waves, survive as long as possible, and top the leaderboard! 👾🛸
