This is a classic Asteroids game implemented in Python using the Pygame library. The player controls a spaceship, navigating through space while avoiding and destroying asteroids. The game features sound effects, background music, and a scoring system.
- Object-Oriented Design: Uses classes for Ship, Asteroid, and Bullet objects
- Player-controlled spaceship with rotation and thrust
- Multiple asteroids that move across the screen with increasing difficulty
- Bullet shooting mechanics with collision detection
- Lives System: 3 lives with respawn on collision
- Level Progression: Automatic level advancement with increasing asteroid count
- High Score Tracking: Persistent high score saved to file
- Pause Functionality: Press 'P' to pause/unpause the game
- Restart Option: Press 'R' to restart after game over
- Collision detection for ship-asteroid and bullet-asteroid interactions
- Score tracking with level and lives display
- Sound effects for thrust, shooting, and explosions
- Background music and visual effects
- Error handling for missing assets
- Python 3.x
- Pygame library (install via
pip install pygame)
- Create a virtual environment (recommended):
python -m venv venv - Activate the virtual environment:
- On Windows:
venv\Scripts\activate - On macOS/Linux:
source venv/bin/activate
- On Windows:
- Install dependencies:
pip install -r requirements.txt - Ensure all game files are in the same directory structure.
- Run the game with:
python final.py - Use keyboard controls to play.
- Left Arrow: Rotate ship left
- Right Arrow: Rotate ship right
- Up Arrow: Thrust forward (activates thruster sound and visual) l- Spacebar: Shoot bullets
- Close window: Quit the game
final.py: Main game script containing all logicimages/: Directory containing game sprites and backgrounds (e.g.,ship.pngis loaded asship_img,ship_thrusted.pngasship_thrusted_imgin code)sounds/: Directory containing audio files for effects and music
- Game developed using Pygame
- Assets: Custom images and sounds (ensure proper licensing if redistributing)
The game includes configurable parameters in final.py:
NUM_ASTEROIDS: Number of asteroids (default: 5)ASTEROID_SPEED: Speed of asteroids (default: 2)BULLET_SPEED: Speed of bullets (default: 10)SHIP_SPEED: Speed of ship thrust (default: 10)- Note: In the code, ship images are referenced as
ship_imgandship_thrusted_imgto avoid conflict with theShipclass instance.
- Note: In the code, ship images are referenced as
FRICTION: Ship deceleration rate (default: 0.2)
- The game includes error handling for missing image and sound files
- Graceful degradation if audio assets are not found
- Informative error messages for debugging
- The game runs at 60 FPS
- Asteroids wrap around screen edges
- Bullets travel in straight lines
- Score increases for each destroyed asteroid
- Background music loops indefinitely
- Sound effects play only if assets are available