A premium, high-performance Snake game built with a Rust WASM core and a TypeScript frontend. This project demonstrates a modern dual-loop architecture, procedural visual effects, and strict engineering standards.
- Unified Engine: Single
requestAnimationFrameloop with high-precision timestamp accumulation (Fix Your Timestep) for stutter-free 60FPS rendering decoupled from physics logic. - Intelligent Pause: Full pause/resume support via
Space,Enter, or dedicated mobile controls with visual modal. - Mobile First: Responsive board scaling, touch-optimized D-pad, and safe-area (notch) support.
- High-DPI Scaling: Automatic DPR detection and canvas scaling for pixel-perfect rendering on Retina and mobile displays.
- Rust WASM Core: Optimized game state management with self-contained random generation via
js-sys. - Premium Visuals: Neon Dark aesthetic with glassmorphism UI and real-time particle feedback.
- Procedural Audio: Dynamic sound effects generated using the Web Audio API.
- Modern Build Stack: Powered by Vite 8.0.8, TypeScript 6.0.3, and Rust 2024.
- Persistence Layer: Decoupled
StorageProviderfor managed high-score tracking. - Production Ready: Express.js server with security headers and Gzip compression.
The project is designed with a strict separation of concerns, utilizing a layered approach that isolates the pure domain logic from integration details.
snake_game/
├── src/ # Rust Domain Logic
│ ├── snake/ # Snake body and movement state
│ ├── world/ # Game grid and collision logic
│ └── utils/ # Cross-platform utility traits
├── frontend/ # TypeScript Application Layer
│ ├── engine/ # Modular Game Engine (Renderer, Particles, Sound)
│ ├── index.ts # Application Entry & Orchestration
│ └── index.css # Design System & Token Definitions
├── pkg/ # Compiled WebAssembly Bindings
├── server/ # Production Node.js/Express Server
└── Makefile # Central Build Orchestrator
This codebase strictly adheres to the following engineering standards:
- Zero-Comment Policy: Clarity is achieved through expressive naming and modular structure; comments are prohibited in production code.
- Naming & Intent: Abbreviations are avoided in favor of descriptive names that explain purpose (e.g.,
renderingContextoverctx). - Complexity Limits: Functions are capped at 40 lines, and files at 300 lines to ensure readability and single responsibility.
- Parameter Constraints: Functions accept a maximum of 4 parameters; complex signatures are refactored into typed configuration objects.
- Exact-Pinning: All dependencies in
package.jsonandCargo.tomlare pinned to exact versions for deterministic builds. - Explicit Error Handling: Robust error propagation using
ResultandOption(Rust) and typed error handling (TS). - Testing Integrity: Every public function requires at least one happy-path and one edge-case test to verify semantic invariants.
-
Clone the repository
git clone <your-repo-url> cd snake_game
-
Install dependencies
# Root & Server dependencies npm install # Frontend Application dependencies cd frontend && npm install && cd ..
-
Setup environment
cp env.template .env
| Command | Profile | Description |
|---|---|---|
make dev |
Dev | Start WASM build with Vite hot-reload |
make build-all |
Prod | Full production build (WASM + Frontend) |
make start |
Prod | Launch Express server on port 3000 |
| Command | Description |
|---|---|
make wasm |
Compile Rust to WebAssembly with wasm-bindgen glue |
make check |
Run cargo fmt and clippy for quality assurance |
make test |
Execute Rust unit and integration tests |
make clean |
Purge all build artifacts and node_modules |
Game parameters are managed via environment variables in the .env file:
| Variable | Description | Recommended/Default |
|---|---|---|
VITE_BASE_URL |
Base path for high-level deployment (e.g., /snake-game/) |
/ |
VITE_WORLD_WIDTH |
Grid dimension (square) of the game world | 64 (Code fallback: 32) |
PORT |
Production server listening port | 3000 |
DEV_SERVER_PORT |
Vite development server port | 8000 |
NODE_ENV |
Mode (development or production) |
development |
COMPRESSION |
Enable Gzip compression in production | true |
- Health Check:
GET /healthreturns JSON status, timestamp, and version.
- Movement: Arrow Keys / WASD / Mobile D-pad
- Commands:
SpaceorEnterto Start, Restart, or Toggle Pause - Mobile: Dedicated
⏸button for quick pause/resume - Settings: Real-time difficulty/speed selection on Home Screen
The following Dockerfile is provided as a reference for containerized deployment. Note: You must build the project locally or via make build-all before creating the image if using a simple scratch build.
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN make build-all
EXPOSE 3000
CMD ["npm", "start"]ISC License - Copyright (c) 2026