A modern Snake game implementation showcasing the power of Rust + WebAssembly + TypeScript. Features high-performance game logic in Rust compiled to WebAssembly, with a sleek TypeScript frontend using Vite and modern web technologies.
- Classic Snake gameplay with wraparound borders
- Cartoon-style snake with eyes and tongue animation
- Points system - earn points by eating food
- Real-time score display
- Responsive controls using arrow keys
- Adjustable speed controls - 5 speed levels (1x to 5x)
- Game states - Playing, Won, Lost
- 20x20 game board for extended gameplay
- Shadcn/ui inspired design with modern card-based layout
- Dark/light mode support with CSS custom properties
- Rust - High-performance game logic and algorithms
- WebAssembly - Seamless integration between Rust and JavaScript
- TypeScript - Type-safe frontend development with modern ES features
- wasm-pack - Rust to WebAssembly compilation
- Vite - Fast development server and build tool
- Inter Font - Modern typography with advanced OpenType features
- Modular design - Clean separation of concerns across multiple modules
- CSS Custom Properties - Modern styling with CSS variables
- SVG Icons - Scalable vector graphics for UI elements
snake-game/
โโโ ๐ฆ Rust + WebAssembly Backend
โ โโโ Cargo.toml # Rust package configuration
โ โโโ Cargo.lock # Dependency lock file
โ โโโ src/ # Rust source code
โ โโโ lib.rs # Main library entry point
โ โโโ direction.rs # Movement direction enum
โ โโโ game.rs # Game coordinator & WASM interface
โ โโโ rnd.rs # Random number utilities
โ โโโ game/ # Core game modules
โ โโโ jungle.rs # Game board & food management
โ โโโ snake.rs # Snake entity & movement logic
โ โโโ status.rs # Game state enum
โ
โโโ ๐จ TypeScript Frontend
โโโ www/
โโโ package.json # Node.js dependencies
โโโ tsconfig.json # TypeScript configuration
โโโ index.html # HTML entry point
โโโ src/
โ โโโ main.ts # Application bootstrapper
โ โโโ style.css # Shadcn/ui inspired styles
โ โโโ config/
โ โ โโโ constants.ts # Game configuration & colors
โ โโโ ui/
โ โ โโโ template.ts # HTML template generator
โ โ โโโ elements.ts # DOM element helpers
โ โ โโโ icons.ts # SVG icon components
โ โโโ game/
โ โ โโโ controller.ts # Game state management
โ โโโ renderer/
โ โ โโโ canvas.ts # Canvas rendering engine
โ โโโ utils/
โ โโโ rnd.ts # Frontend random utilities
โโโ dist/ # Production build output
โโโ pkg/ # Generated WASM bindings
- Rust (latest stable version)
- Node.js (v16 or later)
- Yarn package manager
- wasm-pack tool for building WebAssembly packages
-
Clone the repository:
git clone https://github.com/m76o/snake-game.git cd snake-game -
Install wasm-pack (if not already installed):
cargo install wasm-pack
-
Install frontend dependencies:
cd www yarn install
-
Start the development server:
cd www yarn devThis will:
- Build the Rust code to WebAssembly
- Start the Vite development server
- Open the game in your browser (usually at
http://localhost:5173)
-
Build for production:
cd www yarn build -
Preview production build:
cd www yarn preview
From the www/ directory:
yarn wasm- Build only the WebAssembly moduleyarn dev- Build WASM + start development serveryarn build- Build WASM + create production buildyarn preview- Preview production build locally
- Start the game by clicking the "Play" button
- Move the snake using arrow keys:
- โ Up Arrow - Move up
- โ Down Arrow - Move down
- โ Left Arrow - Move left
- โ Right Arrow - Move right
- Adjust game speed using the ยซ and ยป buttons (1x to 5x speed)
- Eat the red food to grow the snake and earn points
- Avoid hitting yourself - the game ends if the snake collides with its body
- Win condition - Fill the entire board with the snake
- Restart - Click "Play Again" after game over
// High-performance game logic compiled to WebAssembly
Game::new() -> Game // Game coordinator exposed to JS
โโโ Snake // Entity with movement & collision logic
โโโ Jungle // Board management & food generation
โโโ Direction // Movement enums (Up, Down, Left, Right)
โโโ GameStatus // State tracking (Playing, Won, Lost)// Modular, type-safe frontend architecture
SnakeGameApp // Application bootstrapper
โโโ GameController // State management & event handling
โโโ GameRenderer // Canvas drawing & animation engine
โโโ UI Components // Template, elements, icons
โโโ Configuration // Constants & color schemes- Performance First: Rust handles compute-intensive game logic
- Type Safety: Full TypeScript coverage with strict mode enabled
- Modular Design: Clean separation of concerns across layers
- Memory Efficiency: Direct WASM memory access for snake cells
- Modern Web: CSS custom properties, ES2022 features, Vite bundling
- Edition: 2024 (latest Rust edition)
- Crate type:
cdylibfor WebAssembly compilation - Dependencies:
wasm-bindgenfor JS/WASM interop
- Target: ES2022 for modern JavaScript features
- Module: ESNext with bundler resolution
- Strict typing: Enabled for better code quality
- WASM build fails: Ensure
wasm-packis installed and up to date - TypeScript errors: Check that all dependencies are installed with
yarn install - Game not loading: Verify that the WASM module builds successfully before starting dev server
- The game automatically rebuilds WASM when you run
yarn dev - Changes to Rust code require rebuilding the WASM module
- TypeScript changes are hot-reloaded by Vite
- Check browser console for any runtime errors
This project is available under the MIT License.
