A classic Snake game implementation using Pygame. Control a snake to eat food, grow longer, and avoid collisions with walls and yourself.
This Snake game features:
- Grid-based movement
- Food generation and collection
- Score tracking
- Game over detection with restart option
- Visual grid system for orientation
- Use the arrow keys to control the snake's direction
- Eat red food blocks to grow longer and increase your score
- Avoid hitting the walls or running into your own snake body
- When game over occurs, press 'R' to restart
The game is built using Pygame and structured with object-oriented programming:
- SnakeGame class: Encapsulates all game functionality
- Color constants: Defines colors for visual elements
- Grid system: Simplifies positioning and collision detection
- Game state management: Tracks game over conditions and handles restarts
- Snake Movement: The snake moves in the direction set by the player
- Food Generation: Food spawns at random locations not occupied by the snake
- Collision Detection:
- Wall collisions end the game
- Self-collisions end the game
- Food collisions increase score and snake length
- Game Over: When collision occurs, displaying final score and restart option
- Python 3.x
- Pygame library
- Ensure Python is installed on your system
- Install Pygame:
pip install pygame
- Run the game:
python snake.py
- Up Arrow: Move up
- Down Arrow: Move down
- Left Arrow: Move left
- Right Arrow: Move right
- R key: Restart game after game over