Snake
The classic Snake game — eat dots, grow longer, don't hit the wall or yourself.
Overview
Snake is the classic arcade game that defined a generation of mobile phones. Control a snake that moves continuously around the playfield, eating food pellets to grow longer. Hit a wall or your own body and the game ends. Each pellet adds one segment and a small amount of speed, so the game gets progressively harder as your snake grows.
This implementation uses keyboard controls for direction (arrow keys or WASD) and tracks your high score in browser storage. The classic 20x20 playfield is small enough that endgame play becomes a tight planning puzzle — every move has to leave a future move available.
How it works
The game runs on a discrete grid with a fixed tick rate. On each tick, the snake's head moves one cell in the current direction; the cell at the snake's tail position is freed up. If the new head cell contains a food pellet, the tail does not retract that tick, effectively growing the snake by one segment, and a new pellet spawns in a random empty cell.
Collision detection is a constant-time hash-set lookup of the head position against the body. Wall collisions check whether the head position falls outside the grid bounds. The speed-up mechanism reduces the tick interval by a small percentage every few pellets, capping at a minimum that's still humanly playable.
Examples
- A 20x20 playfield has 400 cells; filling the entire board with snake body is the theoretical maximum at 400 pellets eaten.
- Eating your first 50 pellets is straightforward; reaching 100 requires planning paths that don't trap the snake.
- The "Hamiltonian cycle" perfect-play strategy traces a path that visits every cell exactly once, guaranteeing infinite survival until the pellet stack is exhausted.
- Tight corners are the leading cause of game-over: a turn that lands you adjacent to your own body without a future escape route ends the run.
FAQ
Can the snake move backwards?
No — moving directly into your own neck would instantly self-collide, so the input is ignored.
Does difficulty increase?
Yes. The tick interval shortens with each pellet, so the snake speeds up as it grows.
What happens when I eat the food?
The snake grows by one segment, a new pellet spawns in a random empty cell, and your score increments.
Is there a maximum length?
The snake can fill the entire grid, leaving no empty cell for a new pellet. At that point the run is complete.
Are the controls customizable?
Both arrow keys and WASD work. Touch controls are not implemented in this version.