This is a simple 2D Space Invader game built using Python's Pygame library. The objective of the game is to shoot down enemies and score as high as possible before they reach the player. The game ends if any enemy reaches below a certain point on the screen.
- Player Movement: Move left or right using the arrow keys.
- Shooting Mechanism: Shoot bullets using the spacebar.
- Multiple Enemies: Six enemies appear on the screen, moving side to side and advancing toward the player.
- Score Tracking: The score is displayed on the screen, increasing with each enemy hit.
- Game Over Screen: Displays "Game Over" when an enemy reaches below a certain point.
-
Clone the Repository:
git clone https://github.com/yourusername/space-invader-game.git cd space-invader-game
-
Install Dependencies:
- Ensure that Python 3.x and Pygame are installed on your system. You can install Pygame using:
pip install pygame
- Ensure that Python 3.x and Pygame are installed on your system. You can install Pygame using:
-
Run the Game:
python space_invader.py
- Background Image:
background.png
- Icons and Sprites:
ufo.png
,player.png
,enemy.png
,bullet.png
- Sounds:
laser.wav
,explosion.wav
Place these assets in the root directory of your project. If you need free resources, consider using royalty-free image and sound sources like OpenGameArt or Freesound.
- Left Arrow: Move left
- Right Arrow: Move right
- Spacebar: Fire bullet
- Player: Defined at a fixed Y position and controlled by arrow keys.
- Enemies: Multiple enemies with randomized X and Y starting positions.
- Bullet: Shoots from the player’s current position with a "ready" and "fire" state.
- Collision Detection: Calculated using Euclidean distance, triggering when the bullet hits an enemy.
- Game Over Condition: The game ends when an enemy crosses a specific Y threshold.
player(x, y)
: Draws the player on the screen at the given coordinates.enemy(x, y, i)
: Draws the enemy at the given coordinates.fire_bullet(x, y)
: Updates the bullet position as it fires.isCollision(enemyX, enemyY, bulletX, bulletY)
: Detects collisions between the bullet and enemies.show_score(x, y)
: Renders the current score on the screen.game_over_text()
: Displays the game-over text when the game ends.
This project is licensed under the MIT License. Feel free to fork, modify, and use this code for personal and educational purposes.
Enjoy playing Space Invader! If you encounter any issues or have suggestions, please feel free to open an issue or pull request.