gekro
GitHub LinkedIn

Snake

Play Snake on a 20 x 20 grid with keyboard, swipe or on-screen buttons. Pick a speed and choose between deadly walls or wrap-around edges.

Score 0

Best -

Length 3

Arrow keys or WASD steer, Space or P pauses. On a touch screen swipe on the board or use the buttons. The game waits until your first move. Changing speed or walls starts a new game.

How the rules work

Each step the head moves one cell. Eating food adds one segment and one point, and new food appears on a uniformly random empty cell. Running into yourself ends the game. The tail moves out of the way on the same step, so following right behind your own tail is safe. With walls on, the edge ends the game; with wrap on, you come out on the opposite side. Fill all 400 cells and you win.

As-is, no warranty. These apps are free under their listed license and run entirely in your browser. Use at your own risk — don't blame me if your PC catches fire, your dog runs away, or the math turns out wrong. Verify anything that actually matters. None of this is professional financial, medical, legal, or engineering advice.

© 2026 Rohit Burani · MIT · Built at gekro.com · View source ↗

Guide

How to play

Steer the snake to the food. Each piece you eat adds one segment and one point. Do not run into your own body, and do not run into the edge if walls are on.

  • Keyboard: arrow keys or W A S D steer. Space or P pauses and resumes.
  • Touch: swipe on the board in the direction you want to go, or use the four buttons under it. One long swipe can chain several turns.
  • Start: the game waits until your first move, or until you press Start. It cannot start by turning straight back, because the snake starts facing right.

The game pauses itself when you switch to another tab and waits for you to resume.

The snake is smooth and rounded, and its colour runs from a sky-blue head through violet to a rose tail, so a long snake shows which end is which. The food is a glowing amber berry. The board is a soft checker of two tints. Two eyes on the head show which way the snake faces. The only decoration that moves is the frame around the board, which changes colour when a game starts or ends; if your device asks for reduced motion, that is switched off too. The snake itself always steps one cell at a time.

Speed and walls

Two settings change the game, and changing either starts a new game:

SettingOptions
SpeedSlow 6, Normal 9, Fast 13 steps per second
WallsWalls end the game, or Wrap around

With wrap-around on, the board has no edge: leave the right side and you come in on the left. The board border turns dashed to show it. Your best score is stored separately for each of the six combinations, so a fast wrap-around score never replaces a slow walled one.

A result only counts as a best if it is a whole number of food eaten, at least one, and no more than the largest score the board allows (397, since the snake starts with three of the 400 cells). Filling every cell is a win.

How it works

The engine is a pure function with no page attached: step(state, direction, rng) returns the next state and never changes the old one. That makes the rules easy to pin down with tests, and a game replays exactly when given the same random numbers.

  • Tail rule. On a normal step the tail moves out of its cell at the same moment the head moves in, so you may step into the cell the tail is leaving. On a step that eats, the tail stays and the snake grows, so that cell is still occupied.
  • Food. New food is picked uniformly over the empty cells only, by counting the free cells and choosing the nth one. It can never land on the snake, and the test suite checks that every free cell comes up about equally often.
  • Turn queue. Pressing two keys within one step (up then left, say) is a normal way to make a U-turn. The game keeps up to two pending turns. Each new turn is checked against the one before it rather than against the current heading, so a turn that would reverse into the neck is dropped, however quickly keys arrive.
  • Steady speed. The loop adds up the time since the last frame and takes a step each time a full interval has passed, so a 144 Hz screen plays at the same speed as a 60 Hz one. A frame delta is capped at a quarter of a second, so a stalled tab cannot fast-forward the snake.
  • Drawing. The board is a canvas sized for your screen’s pixel density (capped at 2x) and its colours are read from the site’s own theme, so it follows light and dark mode. It is rounded shapes and circles plus short text overlays such as Paused and Game over, and it redraws when you switch between light and dark.

The tests run a few hundred random and food-seeking games on several board sizes, with and without wrap-around, and check after every single step that no two segments overlap, every segment is on the board and next to the previous one, the length equals the starting length plus the food eaten, and the food is never on the snake. A scripted path that visits every cell wins on 4 x 4, 6 x 6 and 8 x 8 boards.

Privacy and storage

Nothing is uploaded and there is no account. Your best scores are saved in your browser’s local storage under one key, as small numbers inside a small JSON object, one per speed and wall combination. The page address holds your speed and wall setting so you can share it. Clearing site data removes the scores.

Limits

  • There is one board size, 20 x 20, and one starting length.
  • There is no obstacle mode, power-ups or multiplayer.
  • Changing speed or walls, or pressing Restart, ends the game in progress. If its score beat your best for those settings, the best is kept.
  • The game keys (arrows, WASD, Space, P) are only captured while focus is inside the game or while a game is running, wherever focus is. If the game is paused and focus is somewhere else on the page, only Space and P act (to resume); the arrow keys and WASD scroll the page as usual, and they do not resume the game. Holding Space or P does not flip pause on and off repeatedly. Space still presses a focused button or opens the focused “How the rules work” section instead of pausing.

Everything runs locally in your browser.

All Fun tools →

For informational purposes only. Not financial, medical, or legal advice. You are solely responsible for how you use these tools.