Snake
A learning exercise exploring browser game development with the HTML5 Canvas API and TypeScript. Hit the AI button to load a Go WebAssembly opponent that uses BFS pathfinding to chase the food.
SNAKE
Arrow keys · WASD · swipe on mobile
How it works
Canvas API
All rendering uses the CanvasRenderingContext2D. Each frame redraws the background, grid, food (with glow via shadowBlur), and each snake segment using roundRect().
Game loop
requestAnimationFrame drives rendering at the display's native refresh rate. Game state updates (snake movement) are gated by a configurable tick interval using delta-time, so game speed is independent of frame rate.
Go → WebAssembly
The AI opponent's pathfinding runs entirely in Go, compiled to .wasm via GOOS=js GOARCH=wasm. Go exposes a snakeAI() function to JavaScript using syscall/js.
BFS pathfinding
The AI finds the shortest path from its head to the food using breadth-first search, treating the snake's own body as obstacles. The grid wraps around (toroidal topology), so BFS handles modular coordinates.
GOOS=js GOARCH=wasm go build -o public/wasm/game.wasm ./wasm/
# copy wasm_exec.js from Go std lib
cp $(go env GOROOT)/misc/wasm/wasm_exec.js public/wasm/