Skip to content

m76o/snake-game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

24 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Snake Game with Rust + WebAssembly + TypeScript

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.

๐Ÿ“ธ Screenshot

Snake Game Screenshot

๐ŸŽฎ Features

  • 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

๐Ÿ› ๏ธ Technology Stack

Core Technologies

  • Rust - High-performance game logic and algorithms
  • WebAssembly - Seamless integration between Rust and JavaScript
  • TypeScript - Type-safe frontend development with modern ES features

Build & Development

  • wasm-pack - Rust to WebAssembly compilation
  • Vite - Fast development server and build tool
  • Inter Font - Modern typography with advanced OpenType features

Architecture

  • 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

๐Ÿ“ Project Structure

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

๐Ÿš€ Getting Started

Prerequisites

  • Rust (latest stable version)
  • Node.js (v16 or later)
  • Yarn package manager
  • wasm-pack tool for building WebAssembly packages

Installation

  1. Clone the repository:

    git clone https://github.com/m76o/snake-game.git
    cd snake-game
  2. Install wasm-pack (if not already installed):

    cargo install wasm-pack
  3. Install frontend dependencies:

    cd www
    yarn install

Development

  1. Start the development server:

    cd www
    yarn dev

    This will:

    • Build the Rust code to WebAssembly
    • Start the Vite development server
    • Open the game in your browser (usually at http://localhost:5173)
  2. Build for production:

    cd www
    yarn build
  3. Preview production build:

    cd www
    yarn preview

Available Scripts

From the www/ directory:

  • yarn wasm - Build only the WebAssembly module
  • yarn dev - Build WASM + start development server
  • yarn build - Build WASM + create production build
  • yarn preview - Preview production build locally

๐ŸŽฏ How to Play

  1. Start the game by clicking the "Play" button
  2. Move the snake using arrow keys:
    • โ†‘ Up Arrow - Move up
    • โ†“ Down Arrow - Move down
    • โ† Left Arrow - Move left
    • โ†’ Right Arrow - Move right
  3. Adjust game speed using the ยซ and ยป buttons (1x to 5x speed)
  4. Eat the red food to grow the snake and earn points
  5. Avoid hitting yourself - the game ends if the snake collides with its body
  6. Win condition - Fill the entire board with the snake
  7. Restart - Click "Play Again" after game over

๐Ÿ—๏ธ Architecture Overview

๐Ÿฆ€ Rust + WebAssembly Backend

// 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)

๐ŸŽจ Modern TypeScript Frontend

// 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

๐Ÿ”ง Key Design Principles

  • 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

๐Ÿ”ง Configuration

Rust Configuration (Cargo.toml)

  • Edition: 2024 (latest Rust edition)
  • Crate type: cdylib for WebAssembly compilation
  • Dependencies: wasm-bindgen for JS/WASM interop

TypeScript Configuration (tsconfig.json)

  • Target: ES2022 for modern JavaScript features
  • Module: ESNext with bundler resolution
  • Strict typing: Enabled for better code quality

๐Ÿ› Troubleshooting

Common Issues

  1. WASM build fails: Ensure wasm-pack is installed and up to date
  2. TypeScript errors: Check that all dependencies are installed with yarn install
  3. Game not loading: Verify that the WASM module builds successfully before starting dev server

Development Tips

  • 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

๐Ÿ“ License

This project is available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published