```html Snake

Snake

Score: 0
High: 0
Arrow keys or WASD to move · Space to start/pause · R to restart
``` ```typescript type Cell = { x: number; y: number }; type Direction = { x: number; y: number }; const GRID = 20; const CELL = 20; const INITIAL_TICK_MS = 150; const MIN_TICK_MS = 60; const SPEEDUP_EVERY = 5; // apples const SPEEDUP_DELTA = 10; // ms class Game { private canvas: HTMLCanvasElement; private ctx: CanvasRenderingContext2D; private scoreEl: HTMLElement; private highEl: HTMLElement; private snake: Cell[]; private dir: Direction; private pendingDir: Direction | null; private food: Cell; private score: number; private high: number; private tickMs: number; private alive: boolean; private started: boolean; private paused: boolean; private tickId: number | null = null; constructor(canvas: HTMLCanvasElement, scoreEl: HTMLElement, highEl: HTMLElement) { this.canvas = canvas; this.ctx = canvas.getContext('2d')!; this.scoreEl = scoreEl; this.highEl = highEl; // Load high score from localStorage const storedHigh = localStorage.getItem('snake.high'); this.high = storedHigh ? parseInt(storedHigh, 10) : 0; this.updateHighDisplay(); this.restart(); } start(): void { if (this.started && this.paused) { this.paused = false; this.tick(); } else if (!this.started) { this.started = true; this.paused = false; this.tick(); } } togglePause(): void { if (!this.started || !this.alive) return; this.paused = !this.paused; if (!this.paused) { this.tick(); } } restart(): void { // Reset to initial state this.snake = [ { x: 10, y: 10 }, { x: 9, y: 10 }, { x: 8, y: 10 } ]; this.dir = { x: 1, y: 0 }; // Moving right this.pendingDir = null; this.score = 0; this.tickMs = INITIAL_TICK_MS; this.alive = true; this.started = false; this.paused = true; this.updateScoreDisplay(); this.spawnFood(); this.draw(); } private spawnFood(): void { let valid = false; let x: number, y: number; while (!valid) { x = Math.floor(Math.random() * GRID); y = Math.floor(Math.random() * GRID); valid = true; for (const segment of this.snake) { if (segment.x === x && segment.y === y) { valid = false; break; } } } this.food = { x, y }; } private tick(): void { if (this.tickId) { window.clearTimeout(this.tickId); } if (this.paused || !this.alive) { this.draw(); return; } // Apply pending direction if (this.pendingDir) { // Prevent reverse direction if (this.pendingDir.x !== -this.dir.x || this.pendingDir.y !== -this.dir.y) { this.dir = this.pendingDir; } this.pendingDir = null; } // Calculate new head position const head = this.snake[0]; const newHead = { x: head.x + this.dir.x, y: head.y + this.dir.y }; // Check wall collision if (newHead.x < 0 || newHead.x >= GRID || newHead.y < 0 || newHead.y >= GRID) { this.gameOver(); return; } // Check self collision (excluding tail that will move) for (let i = 0; i < this.snake.length - 1; i++) { if (newHead.x === this.snake[i].x && newHead.y === this.snake[i].y) { this.gameOver(); return; } } // Move snake this.snake.unshift(newHead); // Check apple collision if (newHead.x === this.food.x && newHead.y === this.food.y) { this.score++; this.updateScoreDisplay(); // Speed up if needed if (this.score % SPEEDUP_EVERY === 0) { this.tickMs = Math.max(MIN_TICK_MS, this.tickMs - SPEEDUP_DELTA); } this.spawnFood(); } else { this.snake.pop(); } this.draw(); // Schedule next tick this.tickId = window.setTimeout(() => this.tick(), this.tickMs); } private gameOver(): void { this.alive = false; this.paused = true; // Update high score if (this.score > this.high) { this.high = this.score; localStorage.setItem('snake.high', this.high.toString()); this.updateHighDisplay(); } this.draw(); } private updateScoreDisplay(): void { this.scoreEl.textContent = `Score: ${this.score}`; } private updateHighDisplay(): void { this.highEl.textContent = `High: ${this.high}`; } private draw(): void { // Clear canvas this.ctx.fillStyle = '#000'; this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height); // Draw apple this.ctx.fillStyle = '#c0392b'; this.ctx.fillRect(this.food.x * CELL, this.food.y * CELL, CELL, CELL); // Draw snake for (let i = 0; i < this.snake.length; i++) { const segment = this.snake[i]; this.ctx.fillStyle = i === 0 ? '#7ed957' : '#3aa635'; this.ctx.fillRect(segment.x * CELL, segment.y * CELL, CELL, CELL); } // Draw overlays if (!this.started) { this.drawOverlay('Press Space to start'); } else if (this.paused) { this.drawOverlay('Paused'); } else if (!this.alive) { this.drawOverlay('Game Over — press R to restart'); } } private drawOverlay(text: string): void { this.ctx.fillStyle = '#fff'; this.ctx.font = '20px sans-serif'; this.ctx.textAlign = 'center'; this.ctx.textBaseline = 'middle'; this.ctx.fillText(text, this.canvas.width / 2, this.canvas.height / 2); } public setDirection(x: number, y: number): void { if (this.paused || !this.started || !this.alive) return; // Prevent reverse direction if (x === -this.dir.x && y === -this.dir.y) return; this.pendingDir = { x, y }; } } // Top-level setup const canvas = document.getElementById('game') as HTMLCanvasElement; const scoreEl = document.getElementById('score')!; const highEl = document.getElementById('high')!; const game = new Game(canvas, scoreEl, highEl); // Keyboard handling window.addEventListener('keydown', (e) => { switch (e.key) { case 'ArrowUp': case 'w': case 'W': e.preventDefault(); game.setDirection(0, -1); break; case 'ArrowDown': case 's': case 'S': e.preventDefault(); game.setDirection(0, 1); break; case 'ArrowLeft': case 'a': case 'A': e.preventDefault(); game.setDirection(-1, 0); break; case 'ArrowRight': case 'd': case 'D': e.preventDefault(); game.setDirection(1, 0); break; case ' ': e.preventDefault(); if (!game.started) { game.start(); } else { game.togglePause(); } break; case 'r': case 'R': e.preventDefault(); game.restart(); break; } }); ```