A classic Snake game built with Rust and the Piston game engine, featuring smooth grid-based animations and progressive difficulty scaling.
- About
- Features
- Screenshots
- Prerequisites
- Installation
- How to Play
- Game Mechanics
- Project Structure
- Dependencies
- Building for Release
- Troubleshooting
- License
Rust Snake is a modern implementation of the classic Snake arcade game, built using the Piston game engine in Rust. The game demonstrates smooth animations, grid-based movement, and progressive difficulty scaling while maintaining the nostalgic feel of the original.
The implementation showcases Rust's performance capabilities combined with Piston's 2D graphics rendering, resulting in a responsive and visually appealing gaming experience.
- 🎮 Classic Snake Gameplay - Eat food, grow longer, avoid collisions
- ✨ Smooth Animations - Interpolated movement with easing functions for fluid visual experience
- 🎯 Grid-Based Movement - Precise 20x20 grid system for predictable gameplay
- 📈 Progressive Difficulty - Snake speed increases as you consume more food
- 🎨 Modern Visual Design - Clean aesthetic with distinct colors for snake head, body, and food
- ⚡ 60 FPS Rendering - Smooth visual updates at 60 frames per second
- 🖥️ Fixed Window Size - Consistent 800x600 resolution across all systems
📸 Screenshots coming soon!
Feel free to contribute by adding gameplay screenshots to this section.
Before building this project, ensure you have the following installed:
Install Rust using rustup (recommended):
# Install rustup (Rust installer and version manager)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Install the stable Rust toolchain
rustup install stable
# Verify installation
rustc --version
cargo --versionPiston requires some system libraries for graphics and window management:
sudo apt-get install \
libasound2-dev \
libx11-dev \
libxrandr-dev \
libxi-dev \
libxcursor-dev \
libxinerama-dev \
libgl1-mesa-dev \
libwayland-dev \
libxkbcommon-devsudo dnf install \
alsa-lib-devel \
libX11-devel \
libXrandr-devel \
libXi-devel \
libXcursor-devel \
libXinerama-devel \
mesa-libGL-devel \
wayland-devel \
libxkbcommon-devel# Install Xcode Command Line Tools
xcode-select --installWindows users typically don't need additional dependencies. Visual Studio Build Tools or MSVC should be sufficient.
git clone https://github.com/your-username/rust-snake.git
cd rust-snake# Build and run in development mode
cargo run
# Or build first, then run
cargo build
cargo runThe game window should open immediately with the title "Snake".
| Key | Action |
|---|---|
↑ (Up Arrow) |
Move snake upward |
↓ (Down Arrow) |
Move snake downward |
← (Left Arrow) |
Move snake leftward |
→ (Right Arrow) |
Move snake rightward |
ESC |
Exit the game |
- Eat the red food to grow your snake
- Avoid collisions with walls and your own body
- Survive as long as possible while achieving the highest score
- The snake starts at position (0, 0) moving right
- Food spawns randomly within the game window
- The snake grows each time it consumes food
- Speed increases progressively with each food consumed
The snake uses a sophisticated animation system:
- Grid-based positioning: Movement snaps to a 20-pixel grid
- Smooth interpolation: Positions are interpolated between grid cells using quadratic easing
- Direction locking: Cannot reverse direction (e.g., can't go left while moving right)
- Progressive speed: Movement speed increases by 10% with each food consumed (capped at minimum 0.1)
- Food collision: Snake head coordinates must match food coordinates exactly
- Wall collision: Game ends when snake hits window boundaries
- Self-collision: Game ends when snake head collides with any body segment
| Element | Color (RGBA) | Description |
|---|---|---|
| Background | [0.15, 0.15, 0.15, 1.0] |
Dark gray background |
| Snake Head | [0.0, 1.0, 0.6, 1.0] |
Bright cyan-green |
| Snake Body | [0.3, 0.3, 0.3, 1.0] |
Medium gray |
| Food | [1.0, 0.2, 0.2, 1.0] |
Bright red (60% of grid size) |
rust-snake/
├── Cargo.toml # Project manifest and dependencies
├── README.md # This documentation file
├── LICENSE # MIT License
└── src/
├── main.rs # Entry point, window configuration
├── app.rs # Main game application logic
├── snake.rs # Snake entity with movement and rendering
├── food.rs # Food spawning and rendering
└── vec2.rs # 2D vector utility for positions
Game entry point responsible for:
- Creating the Piston window (800x600)
- Configuring update rate (60 UPS) and frame rate (60 FPS)
- Initializing the game application
- Running the main game loop (render, update, input)
Core game application containing:
- Game state management (snake, food, window dimensions)
- Render pipeline (clear screen, draw food, draw snake)
- Update loop (snake movement, collision detection)
- Input handling (keyboard events)
Snake entity implementation:
- Head and body segment management
- Direction state machine (LEFT, RIGHT, UP, DOWN)
- Smooth animation with easing functions
- Speed progression system
- Collision detection with food
Food system:
- Random position spawning within window bounds
- Grid-aligned placement
- Rendering with offset sizing
2D vector utility:
- Position representation
- Collision detection
- Random position generation
| Crate | Version | Purpose |
|---|---|---|
piston_window |
0.132.0 | Game window and event loop |
piston2d-graphics |
0.45.0 | 2D graphics rendering |
rand |
0.9.0 | Random number generation for food spawning |
To create an optimized production build:
# Build in release mode with optimizations
cargo build --release
# Run the release build
cargo run --releaseRelease builds are significantly faster due to Rust's optimization passes.
After building:
- Debug:
target/debug/snake - Release:
target/release/snake
Cause: Missing system dependencies or graphics drivers.
Solution:
- Install system dependencies (see Prerequisites)
- Update graphics drivers
- Ensure you're running in a graphical environment (not SSH without X11 forwarding)
Cause: Running debug build or insufficient hardware.
Solution:
# Build in release mode for better performance
cargo run --releaseCause: Wayland/X11 issues on Linux.
Solution:
# Try forcing X11 backend
export GDK_BACKEND=x11
cargo run
# Or try running with Wayland
export GDK_BACKEND=wayland
cargo runCause: Event loop timing issues.
Solution:
- Ensure no other application is capturing keyboard input
- Try running in a clean terminal session
- Check that 60 UPS/FPS settings match your monitor's refresh rate
Cause: Outdated Rust version or dependency conflicts.
Solution:
# Update Rust toolchain
rustup update
# Clear build cache and rebuild
cargo clean
cargo build- Release mode: Always use
--releasefor best performance - Close background applications: Free up system resources
- Update graphics drivers: Ensure optimal GPU performance
This project is licensed under the MIT License - see the LICENSE file for details.
- ✅ Free to use for personal and commercial projects
- ✅ Modify and distribute
- ✅ Include original copyright notice
- ❌ No warranty provided
Contributions are welcome! Feel free to:
- 🐛 Report bugs
- 💡 Suggest features
- 🎨 Add screenshots or visual improvements
- 📝 Improve documentation
- 🔧 Submit pull requests
- Piston Game Engine - Excellent Rust game development framework
- Classic Snake arcade game - Original concept by Gremlin Industries (1976)
Happy Gaming! 🎮🐍