A minimal Snake game written in Rust using SDL2.
- Control a snake with WASD keys.
- Eat red food pellets to grow longer and increase your score.
- The game speeds up as your score increases.
- Avoid hitting the walls or your own tail — that's game over.
- Fill the entire board to win!
- Top 5 high scores are persisted to disk and displayed on the game-over screen.
- Press Escape to pause, unpause, or restart after game over.
| Key | Action |
|---|---|
| W | Move up |
| A | Move left |
| S | Move down |
| D | Move right |
| Escape | Pause / Unpause / Restart |
- Rust — edition 2021 (stable).
- SDL2 and SDL2_mixer development libraries.
macOS (Apple Silicon):
brew install sdl2 sdl2_mixerThe .cargo/config.toml already points to Homebrew's library path. For Intel Macs, adjust the config or use /usr/local/lib.
Linux (Debian/Ubuntu):
sudo apt-get install libsdl2-dev libsdl2-mixer-devOther distributions: install the equivalent sdl2 and sdl2_mixer development packages.
Windows:
Download the SDL2 and SDL2_mixer VC development zips and set the SDL2_LIB_DIR environment variable. See the rust-sdl2 Windows guide for details.
git clone https://github.com/your-username/snek.rs.git
cd snek.rs
cargo run --releaseFor development:
cargo runRun the test suite:
cargo test| File | Responsibility |
|---|---|
src/main.rs |
SDL2 init, event loop, delta-time tick accumulator, input & audio |
src/context.rs |
Game state: snake position/direction, food spawning, collision, score |
src/renderer.rs |
SDL2 drawing: background, walls, snake, food, pixel-font score HUD |
src/audio.rs |
SDL2_mixer wrapper — generates WAV sounds programmatically |
src/high_scores.rs |
Loads/saves top-5 high scores to ~/.snek_highscores |
The game renders at up to 60 FPS with game logic ticking at a variable rate (200 ms → 60 ms as the score increases). A delta-time accumulator ensures consistent timing regardless of frame rate. Movement is intentionally discrete (grid-based, cell-by-cell) — no interpolation.
- On-screen pixel-font score display with colour-coded game states
- Progressive difficulty — game speeds up as you eat
- Top-5 high scores persisted in
~/.snek_highscores - Sound effects (eat chirp, death buzz, win fanfare) — no external audio files needed
- 30 unit tests covering game logic
- Cross-platform CI (Linux, macOS, Windows)
- Board size and keybindings are not configurable.
- SDL2_mixer must be installed as a system library (not vendored).
MIT
