A fun 2D space shooter game built with Python and Pygame. Defend your ship from waves of invading aliens!
Alien Invasion is a classic arcade-style shooter where you:
- Control a ship at the bottom of the screen using LEFT and RIGHT arrow keys
- Shoot aliens with the SPACE bar
- Survive waves of aliens that move horizontally and descend toward you
- Earn points for each alien destroyed
- Lose when aliens reach the bottom or hit your ship
- Progress through increasingly difficult levels
✅ Player Controls
- Arrow keys for smooth left/right movement
- Space bar to fire bullets (max 3 on screen)
- Press P to restart after game over
- Press Q to quit
✅ Game Mechanics
- Fleet of aliens that move horizontally and drop down when hitting screen edges
- Collision detection for bullets hitting aliens
- Collision detection for aliens hitting the ship
- Game over when all lives are lost
- Level progression with increasing difficulty
- Score tracking and lives display
✅ Visual Elements
- Sky blue background
- Ship sprite at the bottom
- Green alien sprites in formation
- Black bullet indicators
- Game over screen with restart instructions
- On-screen score and lives counter
- Python 3.7 or higher
uvpackage manager
If you don't have uv installed, follow the official uv installation guide:
macOS/Linux:
curl -LsSf https://astral.sh/uv/install.sh | shWindows:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"Or install via Homebrew (macOS):
brew install uvFor other installation methods, see the uv documentation.
- Navigate to the project directory:
cd alien-invasion- Install dependencies:
uv sync- Generate the alien sprite image (if not already created):
uv run create_alien_image.pyuv run alien_invasion.pyThe game window will open at 1200x800 resolution.
| Key | Action |
|---|---|
| LEFT Arrow | Move ship left |
| RIGHT Arrow | Move ship right |
| SPACE | Fire bullet |
| P | Play again (when game over) |
| Q | Quit game |
You can customize game parameters in settings.py:
- Screen resolution:
screen_width,screen_height - Ship speed:
ship_speed - Bullet settings:
bullet_speed,bullet_width,bullet_height,bullets_allowed - Alien settings:
alien_speed,fleet_drop_speed - Difficulty scaling:
speedup_scale(multiplier for speed increase per level)
self.ship_speed = 2.0 # Faster ship
self.alien_speed = 2.0 # Faster aliens
self.speedup_scale = 1.2 # More aggressive difficulty scalingalien-invasion/
├── alien_invasion.py # Main game loop and controller
├── ship.py # Ship sprite class
├── bullet.py # Bullet sprite class
├── alien.py # Alien sprite class
├── settings.py # Game configuration and settings
├── create_alien_image.py # Script to generate alien sprite
├── images/
│ ├── ship.bmp # Ship sprite image
│ └── alien.bmp # Alien sprite image
└── README.md # This file
- Check Events: Handle player input (keyboard)
- Update: Move ship, bullets, and aliens
- Detect Collisions: Check for bullet-alien hits, alien-ship hits, and aliens reaching bottom
- Render: Draw all sprites and UI elements
- Refresh: Update display at 60 FPS
- +10 points for each alien destroyed
- Lives: Start with 3
- Game Over: When you lose all lives
- Each completed level (all aliens destroyed) triggers:
- Speed increase for aliens
- Speed increase for bullets (optional)
- New fleet generated at original positions
- Game continues until all lives are lost
Manages the overall game flow, sprite groups, collisions, and game state.
Key Methods:
run_game()- Main game loop_check_events()- Handle user input_update_bullets()- Update bullet positions_update_aliens()- Update alien positions_check_collisions()- Detect all collisions_create_fleet()- Generate alien formation_ship_hit()- Handle ship collision_start_new_level()- Progress to next level
Player-controlled sprite that fires bullets.
Key Methods:
update()- Move ship based on input flagscenter_ship()- Reset ship positionblitme()- Draw ship on screen
Projectile fired by the ship.
Key Methods:
update()- Move bullet upwarddraw_bullet()- Draw bullet on screen
Enemy sprite in the invading fleet.
Key Methods:
update()- Move alien horizontallycheck_edges()- Detect screen boundary collision
Configuration class for all game parameters.
Key Methods:
increase_difficulty()- Scale up game speed
- Don't waste bullets - You can only have 3 bullets on screen at once
- Position matters - Try to intercept aliens early before they descend too far
- Don't get cornered - Move to the center when large groups approach
- Each level gets harder - Aliens move faster with each completed level
- Keyboard response - Game runs at 60 FPS for smooth, responsive controls
- Ensure pygame is installed:
uv pip list | grep pygame - Check Python version:
python --version(requires 3.7+)
- Run
python create_alien_image.pyto generate the alien sprite - Ensure
images/directory exists
- Adjust
ship_speed,alien_speedinsettings.py - Modify
speedup_scalefor difficulty progression
Here are some ideas to expand the game:
- 🎵 Sound Effects - Add explosion and shooting sounds
- 💥 Power-ups - Extra lives, rapid fire, shields
- 🎨 Better Graphics - Improved sprite artwork
- 🎯 Obstacles - Bunkers/shields for strategic gameplay
- 📊 High Scores - Persistent leaderboard
- 🌙 Boss Battles - Special enemy waves with unique patterns
- 🏆 Achievements - Complete challenges for badges
This project teaches:
- Object-oriented programming in Python
- Game loop structure and timing
- Sprite management with Pygame
- Collision detection algorithms
- Event-driven programming
- Game state management
- Difficulty scaling mechanics
Free to use and modify for educational purposes.
Created as a learning project using Python and Pygame.
Enjoy the game! 🚀👽