How plutov/snake works
Project Type and Purpose
• This is a browser‐based Snake game written in Go, compiled to WebAssembly, and served locally via the wasmserve HTTP server. There is no separate “backend” in the traditional sense; the Go code becomes the frontend logic running inside the browser’s WASM VM.
File Structure Analysis
• All Go source files live at the project root:
• main.go – Entrypoint: sets up the game loop, ties in with JavaScript/HTML for rendering and input.
• input.go – Captures and normalizes keyboard (or pointer) events via JS syscalls.
• game.go – Core game loop, tick scheduling, state transitions (running, game over).
• board.go – Manages grid geometry, boundaries, collision detection.
• snake.go – Defines the snake’s body data structure and movement logic.
• food.go – Spawns food items, checks for consumption events.
• go.mod/go.sum – Module dependencies (standard lib + wasm), no external libraries.
• README.md – Shows that we use wasmserve to host the generated .wasm and any HTML/JS glue.
Key Components
a. wasmserve HTTP Server
b. Browser Environment
c. WebAssembly Module (your compiled Go binary)
d. JavaScript/HTML Glue (auto‐generated by Go’s wasm support)
e. Game Submodules inside WASM:
– Input Module
– Game Engine (game.go)
– Board Module
– Snake Module
– Food Module
Component Relationships and Data Flow
1) wasmserve serves index.html, wasm_exec.js, and game.wasm over HTTP to the Browser.
2) Browser loads HTML + JS glue (wasm_exec.js) which instantiates the Go WASM module.
3) User presses keys → JS event handler (in wasm_exec.js) → Input Module in Go.
4) Input Module sends events to Game Engine.
5) Game Engine calls Board, Snake, Food modules each tick to update positions, detect collisions, spawn food.
Architectural Patterns & Design Principles
• Modular separation of concerns: input, state management (Engine), domain entities (Board, Snake, Food).
• Event‐driven loop: centralized Game Engine controlling tick rate.
• Host communication via JS syscall boundary (bridge between Go/WASM and Browser DOM/Canvas).