The entire Snake Classic Game — markup, styles, and logic — lives in a singleDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/ac-unefm/snake-game/llms.txt
Use this file to discover all available pages before exploring further.
index.html file. There is no build step, no external files, and no module system. Opening the file in any modern browser is all it takes to play. The file is composed of three top-level sections inside <head> and <body>.
File layout
Section 1: <style> block
The entire CSS lives in a single embedded
<style> tag inside <head>. It contains:- CSS custom properties defined on
:root(design tokens for colours, used throughout). - A universal box-sizing reset and a
bodyflex layout. - Component styles for the skip link,
.sr-onlyutility, header (h1), HUD (#hud), session bar (#session-bar), canvas (#canvas,#game-region), status message (#message), results overlay (#results-overlay,#results-panel), help overlay (#help-overlay,#help-panel), the D-pad (#dpad,.dpad-btn), and the<footer>. - Responsive
@mediaqueries for mobile portrait (max-width: 480px), landscape (max-height: 500px), tablets (481px–768px), andprefers-reduced-motion.
Section 2: HTML <body>
Semantic markup in document order:
- Skip link —
<a href="#game-region" class="skip-link">as the very first child of<body>. <header>— contains only the<h1>Snake</h1>title.#btn-help— absolutely-positioned help button (?) outside<main>, witharia-haspopup="dialog".<main>— contains: screen-reader instructions (#game-instructions,.sr-only), the HUD (#hud), session bar (#session-bar), the game region (#game-region><canvas id="canvas">), the status message div (#message), the D-pad (#dpad), and the ARIA alert region (#game-alert).#help-overlay—role="dialog"modal with help content;aria-hidden="true"when closed.#results-overlay—role="dialog"modal showing session results;aria-hidden="true"when closed.<footer>— copyright, author link, and version string.
Section 3: <script> block
A single inline
<script> tag at the bottom of <body> contains the full JavaScript engine, in this order:- DOM references —
constbindings to every element the JS touches. - Board constants —
GRID_SIZE,COLS,ROWS. - Speed constants —
BASE_TICK,MIN_TICK,STEP_SCORE,STEP_MS. - Session constants —
SESSION_ATTEMPTS. - Colour palette —
COLORSobject. reducedMotionflag — read once at startup frommatchMedia.- Global state — mutable
letvariables for per-attempt and session state. - Storage functions —
readBestScore(),saveBestScore(). - Speed helpers —
getLevel(),getTick(). - Session bar —
updateSessionBar(). - Game initialisation —
init(),placeFood(). - Game loop —
scheduleNext(),tick(),update(). - Rendering —
drawGrid(),drawFood(),drawSegment(),draw(). - UI messaging —
flashMessage(),showMessage(),hideMessage(),announceAlert(). - Session / end-game logic —
captureAttempt(),endGame(),showSessionResults(),startNewSession(). - Help modal —
openHelp(),closeHelp(). - Game start —
startGame(). - Input handlers — keyboard (
keydown), D-pad touch/click, canvas swipe (touchstart/touchend). - Startup call —
init()thendraw()to paint the initial idle frame.
CSS custom properties
All design tokens are declared on:root and referenced throughout the stylesheet via var():
Global state variables
State is split into two groups oflet variables.
Per-attempt state
| Variable | Type | Description |
|---|---|---|
snake | {x, y}[] | Ordered array of grid cells; index 0 is the head |
dir | {x, y} | Current movement direction (applied each tick) |
nextDir | {x, y} | Buffered direction set by input handlers |
food | {x, y} | Current food cell position |
score | number | Food items eaten this attempt |
running | boolean | Whether the game loop is active |
gameOver | boolean | Whether the attempt has ended |
loopId | number | setTimeout handle, used to cancel the loop |
flashTimer | number | null | Handle for temporary message timeout |
alertTimer | number | null | Handle for ARIA alert debounce timeout |
attemptStart | number | Date.now() value when the attempt began |
attemptSteps | number | Count of grid cells traversed |
attemptMaxLevel | number | Highest level reached during the attempt |
attemptMinTick | number | Shortest tick interval seen (= peak speed) |
Session state
| Variable | Type | Description |
|---|---|---|
sessionAttempts | AttemptRecord[] | Array of completed attempt records (max 5) |
currentAttempt | number | Current attempt index, 0–4 |
best | number | All-time best score, loaded from localStorage on startup |
Data flow
Constants
| Constant | Value | Description |
|---|---|---|
GRID_SIZE | 20 | Pixel size of each cell |
COLS | 20 | Grid columns (derived: canvas.width / GRID_SIZE) |
ROWS | 20 | Grid rows (derived: canvas.height / GRID_SIZE) |
BASE_TICK | 130 ms | Starting tick interval |
MIN_TICK | 45 ms | Minimum tick interval (speed cap) |
STEP_SCORE | 5 | Points per level-up |
STEP_MS | 12 ms | Speed increase per level |
SESSION_ATTEMPTS | 5 | Attempts per session |
SWIPE_MIN_PX | 20 | Minimum pixel distance to register a swipe |