This is the outcome from my first playdate with my new friend Bob (https://www.ibm.com/products/bob). Done while killing some time at the airport. Really impressive!
A classic snake game implementation in Python using Pygame. Control the snake to eat food, grow longer, and avoid hitting the walls or yourself!
- Two Game Modes:
- ๐๏ธ Classic Mode: Game ends when snake hits walls
- ๐ช Fun Mode: Snake wraps around walls (appears on opposite side)
- Interactive startup menu to select game mode and configure settings
- Configurable food items (1-50 items, default 4) with colorful variety (Red, Orange, Yellow, Blue)
- Pause functionality - Press 'P' to pause/resume the game
- High Score System:
- ๐ Separate high scores for each game mode
- ๐พ Automatically saved and persisted across sessions
- ๐ Displayed during gameplay and in menu
- Smooth controls using arrow keys
- Score tracking with real-time display
- Game over detection with restart option
- Return to menu option after game over
- Clean and simple graphics with improved UI
- Comprehensive unit tests with CI/CD integration
- Type hints for better code quality
- Run the game and you'll see a menu with game mode options
- Use UP/DOWN arrow keys to select a mode (Classic or Fun)
- Use LEFT/RIGHT arrow keys to adjust the number of food items (1-50)
- Press ENTER or SPACE to start the game
- Traditional snake gameplay
- Game ends when you hit a wall
- More challenging!
- Snake wraps around walls
- Hit the right wall โ appear on the left
- Hit the top wall โ appear at the bottom
- Endless fun!
- Use Arrow Keys to control the snake's direction:
- โฌ๏ธ UP - Move up
- โฌ๏ธ DOWN - Move down
- โฌ ๏ธ LEFT - Move left
- โก๏ธ RIGHT - Move right
- Press P to pause/resume the game
- Eat the colorful food items (red, orange, yellow, blue) to grow longer and increase your score
- Avoid hitting your own body (in both modes)
- Press SPACE to restart after game over
- Press ESC to return to menu after game over or during gameplay (press twice to confirm quit)
- Python 3.7 or higher
- pip (Python package installer)
- Clone this repository:
git clone https://github.com/anbie/snake-game.git
cd snake-game- Install the required dependencies:
pip install -r requirements.txtSimply run the main Python file:
python snake_game.py- pygame: Game development library for Python
- pytest: Testing framework
- pytest-cov: Code coverage plugin for pytest
- coverage: Code coverage measurement
See requirements.txt for specific version requirements.
You can modify the following constants in snake_game.py to customize the game:
WINDOW_WIDTH: Width of the game window (default: 640)WINDOW_HEIGHT: Height of the game window (default: 480)BLOCK_SIZE: Size of each snake segment (default: 20)SPEED: Game speed/FPS (default: 10)DEFAULT_NUM_FOOD_ITEMS: Default number of food items (default: 4)MIN_FOOD_ITEMS: Minimum food items allowed (default: 1)MAX_FOOD_ITEMS: Maximum food items allowed (default: 50)- Colors:
WHITE,BLACK,RED,GREEN,BLUE,ORANGE,YELLOW FOOD_COLORS: Array of colors for food itemsSNAKE_INNER_OFFSET: Offset for snake inner detail (default: 4)SNAKE_INNER_SIZE: Size of snake inner detail (default: 12)
Note: Food items can now be configured at runtime through the menu (1-50 items).
snake-game/
โโโ .github/
โ โโโ workflows/
โ โโโ tests.yml # GitHub Actions CI/CD workflow
โโโ snake_game.py # Main game file
โโโ test_snake_game.py # Comprehensive unit tests
โโโ requirements.txt # Python dependencies
โโโ README.md # This file
โโโ .gitignore # Git ignore file
- The snake starts with 3 segments
- Configurable food items (1-50, default 4) are present on the board with different colors (Red, Orange, Yellow, Blue)
- Each food eaten adds 1 point to the score and 1 segment to the snake
- When a food is eaten, a new one spawns to maintain the configured number of food items
- Food spawns randomly on the grid, avoiding the snake's body and other food
- Hitting your own body ends the game in both modes
- Press 'P' to pause/resume at any time
- Game ends when snake hits any wall
- More challenging and traditional gameplay
- Snake wraps around when hitting walls:
- Hit right wall โ appear on left side
- Hit left wall โ appear on right side
- Hit top wall โ appear at bottom
- Hit bottom wall โ appear at top
- Only hitting yourself ends the game
- Allows for longer gameplay sessions
This project includes comprehensive unit tests covering all game functions.
- Install test dependencies:
pip install -r requirements.txt- Run tests with pytest:
pytest test_snake_game.py -v- Run tests with coverage:
pytest test_snake_game.py -v --cov=snake_game --cov-report=term-missing- Run tests with unittest:
python -m unittest test_snake_game.py -vThe test suite includes:
- 100+ unit tests covering all game functions in both modes
- Initialization and reset tests for both Classic and Fun modes
- Movement and collision detection tests
- Wall collision tests (Classic mode)
- Wall wrapping tests (Fun mode)
- Food placement and eating mechanics tests
- Game state and scoring tests
- Mode switching tests
- High score functionality tests:
- High score persistence and loading
- Separate high scores for each mode
- High score updates on game over
- Corrupted file handling
- UI rendering tests
- Menu navigation tests
- Integration tests for complete game scenarios
- Boundary and edge case tests
Tests run automatically on every commit via GitHub Actions:
- Tests run on Python 3.11, 3.12, 3.13, and 3.14
- Automatic code coverage reporting
- Tests run on both push and pull requests
- Coverage reports uploaded as artifacts
View test results in the Actions tab of the repository.
Contributions, issues, and feature requests are welcome! Feel free to check the issues page.
This project is open source and available under the MIT License.
anbie
- GitHub: @anbie
Give a โญ๏ธ if you enjoyed this project!
Made with โค๏ธ and Python