Last Updated: 3/6/2026
API Reference
SnakeGame Class
The main game class that manages all game logic and rendering.
Constructor
constructor()Initializes the game, sets up the canvas, loads high score, and registers event listeners.
Public Methods
startNewGame()
startNewGame(): voidResets and starts a new game session.
- Resets grid to initial size
- Clears snake to center position
- Resets score, level, and speed
- Spawns initial food
- Clears pause and game over states
Private Methods
updateCanvasSize()
private updateCanvasSize(): voidRecalculates canvas dimensions and isometric projection matrices based on current grid size and viewport.
toIso(gridX: number, gridY: number)
private toIso(gridX: number, gridY: number): { x: number; y: number }Transforms grid coordinates to isometric screen coordinates with perspective projection.
Parameters:
gridX: Grid x-coordinategridY: Grid y-coordinate
Returns: Screen coordinates { x, y }
draw()
private draw(): voidRenders the complete game scene:
- Clears canvas
- Draws ground plane (checkerboard)
- Draws grid lines
- Draws ground border
- Sorts and renders all game objects (snake, food) with shadows
update()
private update(): voidGame loop tick function that:
- Moves snake in current direction
- Checks collisions
- Handles food consumption
- Triggers grid expansion when needed
checkCollision(head: Position)
private checkCollision(head: Position): booleanChecks if the given position collides with walls or snake body.
Parameters:
head: Position to check
Returns: true if collision detected
spawnFood()
private spawnFood(): voidSpawns food at a random empty grid position.
expandGrid()
private expandGrid(): voidDoubles the grid size, advances level, and increases game speed.
togglePause()
private togglePause(): voidToggles pause state and shows/hides pause overlay.
endGame()
private endGame(): voidEnds the game, saves high score if applicable, and shows game over screen.
Rendering Methods
drawGroundPlane()
private drawGroundPlane(): voidDraws the checkerboard ground pattern using Path2D batching.
drawGridLines()
private drawGridLines(): voidDraws subtle grid lines (skipped when tiles are too small).
drawGroundBorder()
private drawGroundBorder(): voidDraws the border around the game grid.
drawBlock(gx, gy, topColor, rightColor, leftColor)
private drawBlock(
gx: number,
gy: number,
topColor: string,
rightColor: string,
leftColor: string
): voidDraws a 3D isometric block at grid position (gx, gy).
Parameters:
gx: Grid x-coordinategy: Grid y-coordinatetopColor: CSS color for top facerightColor: CSS color for right-facing sidesleftColor: CSS color for left-facing sides
drawBlockShadow(gx, gy)
private drawBlockShadow(gx: number, gy: number): voidDraws a shadow on the ground beneath a block.
getBlockHeight(gx, gy)
private getBlockHeight(gx: number, gy: number): numberCalculates the screen-space height of a block with perspective scaling.
Storage Methods
getHighScore()
private getHighScore(): numberRetrieves high score from localStorage.
Returns: High score value (0 if none saved)
saveHighScore(score)
private saveHighScore(score: number): voidSaves high score to localStorage.
loadHighScore()
private loadHighScore(): voidLoads and displays high score in UI.
Types
Position
type Position = { x: number; y: number }Represents a grid coordinate.
Direction
type Direction = 'UP' | 'DOWN' | 'LEFT' | 'RIGHT'Represents snake movement direction.
Constants
ISO_MIN_WIDTH
const ISO_MIN_WIDTH = 300Minimum isometric view width in pixels.
ISO_MAX_WIDTH
const ISO_MAX_WIDTH = 1200Maximum isometric view width in pixels.