A classic Snake game built with Rust and Bevy, a powerful data-driven game engine.
This is a modern implementation of the classic Snake game featuring:
- A grid-based board system
- Snake movement and collision detection
- Food spawning and consumption mechanics
- Score tracking
- Customizable game title via environment variables
- ECS (Entity Component System) architecture using Bevy
- Language: Rust
- Game Engine: Bevy 0.15.3
- Other Dependencies:
dotenvy- Environment variable managementrand- Random number generation for food placement
snake_game/
├── src/
│ ├── main.rs # Application entry point
│ ├── core/ # Core game logic and data
│ │ ├── components.rs # Bevy components (game entities)
│ │ ├── constants.rs # Game constants
│ │ ├── events.rs # Game events
│ │ ├── resources.rs # Bevy resources (global state)
│ │ └── mod.rs
│ └── plugins/ # Bevy plugins (feature modules)
│ ├── board/ # Board/grid system
│ ├── camera/ # Camera control
│ ├── controls/ # Player input handling
│ ├── food/ # Food mechanics
│ ├── game/ # Game state management
│ ├── score/ # Score tracking
│ ├── shared/ # Shared systems
│ ├── snake/ # Snake mechanics
│ │ ├── helpers.rs # Snake helper functions
│ │ └── mod.rs
│ └── mod.rs
├── assets/
│ └── fonts/ # Game fonts
├── Cargo.toml # Rust project manifest
└── README.md # This file
- Rust (latest stable version recommended)
- Cargo (comes with Rust)
-
Clone or extract the project:
cd snake_game -
Run the game:
cargo run
-
To run with custom game title, create a
.envfile:GAME_TITLE=My Custom Snake Game
Create an optimized release build:
cargo build --releaseThe compiled binary will be in target/release/.
- Arrow Keys or WASD - Move the snake
- ESC - Exit game (or close window)
- Guide your snake to eat food pellets
- Avoid colliding with walls and yourself
- Your score increases with each food consumed
- Game ends on collision
This project uses the Entity Component System (ECS) pattern provided by Bevy:
- Components - Data attached to entities (e.g., snake position, direction)
- Systems - Logic that operates on components
- Resources - Shared global state (e.g., score, game state)
- Plugins - Organized modules bundling related systems and components
Each feature (snake, food, board, etc.) is implemented as a separate plugin for modularity and maintainability.
Game settings can be customized via environment variables (using .env file):
GAME_TITLE- Window title (default: "My Snake Game")
Check core/constants.rs for additional game constants like board size, grid cell size, etc.
-
core/- Pure game logic and data structures- Independent of rendering or input
- Contains the game's business rules
-
plugins/- Bevy-specific implementations- Each plugin handles a specific game feature
- Contains systems that process components and resources
# Debug build (faster compilation, slower runtime)
cargo build
# Run with logging
RUST_LOG=debug cargo run- Check code:
cargo check - Run tests:
cargo test - Format code:
cargo fmt - Lint:
cargo clippy
Potential features for expansion:
- Difficulty levels
- High score persistence
- Power-ups and obstacles
- Multiplayer support
- Sound effects and music
- Menu system
- Different game modes
This project is open source. Feel free to use and modify as needed.
Enjoy the game! 🐍