Home › Guides › Make a Snake Game With AI: Classic Rules, Your Twist

Make a Snake Game With AI: Classic Rules, Your Twist

The rules and numbers behind a good snake game, and how to have an AI agent build one: grid, tick speed, controls, walls or wrap-around, and your own twists.

Updated December 2, 2026 · 1339 words · more on this topic

Snake is a grid, a tick and one rule: do not run into yourself. Describe the grid size, how often the snake steps, whether the edges kill or wrap, and how it is steered, and the agent builds it in the emulator preview. Then add the twist that makes it yours: obstacles, a second kind of food, a shrinking arena or a second snake on the same screen.

The rules, stated exactly

Here is the complete rule set.

  • The snake moves one cell per tick in its current direction.
  • The player can turn left or right. Turning straight back is not allowed.
  • Eating food adds one segment at the tail and puts new food on an empty cell.
  • Running into its own body ends the run. The edges either end the run or wrap.
  • The score is the number of foods eaten.

Three details are easy to miss. Food must never appear on the snake. The head may enter the cell the tail is leaving on that same tick, because the tail moves away. And if the snake fills the whole grid, the player has won, so there should be a screen for it.

Grid and tick speed

Snake moves in steps. Two numbers set the feel: how many cells the grid has, and how long a tick lasts.

SettingPhone, portraitBrowser
Grid15 wide, 20 tall20 by 20
Starting tick160 ms140 ms
Fastest tick90 ms70 ms

A tick of 150 milliseconds is between six and seven steps per second. On a phone, a swipe takes about as long as a step once the tick drops under 90 milliseconds, and the player can no longer react in time. Keys are faster than thumbs, so a browser version can go lower.

Grid size changes the length of a run. A small grid is tense from the first food. A large one has a slow first minute.

If stepping looks jerky, ask for the snake to glide between cells.

Steering: swipes, buttons and keys

Swipes on a phone. The whole screen is the swipe area. The turn should register once the finger has moved about 20 to 30 pixels, not when it lifts. Waiting for the lift adds a delay that players feel as lag.

Buttons on a phone. Four arrows at the bottom are exact, and thumbs never cover the grid, but they take a quarter of the screen. A leaner version has two buttons, turn left and turn right as seen from the snake.

Arrow keys in a browser. Use the arrow keys and WASD together, and make sure the arrow keys do not scroll the page. Add swipes too, since many people will open the link on a phone. Browser games made here accept keys and touch.

The reverse-direction bug

Nearly every snake game ships with this bug once. The snake is moving right. The player presses up and then left, quickly, both inside one tick. A naive game stores "up" on the first press, then checks "left" against "up" and accepts it, because left is not the reverse of up. On the next tick the snake moves left, straight into its own neck. The player sees a snake die in open space.

The fix has two parts:

  1. Compare with the last step made. A new turn is checked against the direction of the last real step, or the last turn waiting in the queue, never against a value that key presses can change between ticks.
  2. Queue the inputs. Keep up to two waiting turns and apply one per tick. Up then left becomes a clean U-turn over two ticks, which is what the player meant.

Without a queue the second input is dropped and fast players feel ignored. With a queue longer than two, the snake keeps turning after the player has stopped. If quick turns kill your snake, send this:

Queue up to two turns and apply one per step. A turn must never reverse the snake into itself, even when two keys are pressed inside one step.

Walls or wrap-around

Walls are the classic rule. The edges are a danger, a long snake gets boxed in and runs are shorter. Draw a clear border so the player can see where the grid ends.

Wrap-around means a head that leaves on the right comes back on the left. The only thing that can end the run is the snake itself. Runs are longer and scores are higher, which suits a small phone grid.

They are different games. If you offer both, keep a best score for each.

When it speeds up

  • Per food. The tick shortens by 2 milliseconds with every food.
  • In steps. Every 5 foods the tick drops by 10 milliseconds. The player feels a level-up.
  • Fixed. The player picks slow, normal or fast at the start, and food is worth 1, 2 or 3 points.

Always set a floor. Without one the game turns unplayable where it should turn hard, and the best score records where the speed broke. From 150 milliseconds, minus 10 every 5 foods with a floor of 80, the top speed arrives after 35 foods.

Two descriptions to paste

A snake game for phones on a grid 15 wide and 20 tall. The snake starts 3 cells long and steps every 160 milliseconds. I swipe anywhere to turn. An apple adds one segment and one point. Every 5 apples the step gets 10 milliseconds shorter, down to 90. Hitting a wall or my own body ends the run. Queue up to two swipes so quick turns are not lost. A game-over card shows the best score and Play again.

A browser snake game on a 20 by 20 grid, played with arrow keys or WASD, and with swipes on a phone. The edges wrap around. Red food is worth 1 point. Now and then a gold food appears for 5 seconds and is worth 5. Every 5 foods a stone block appears on an empty cell, never right in front of the head. Hitting a block or my own body ends the run. The snake gets faster with each food, up to a limit.

Twists that make it yours

Obstacles. A block appears every 5 foods, never within 3 cells ahead of the head. The grid gets harder, not only faster.

Two foods. A timed gold food makes the player weigh a risky detour against a safe line. A blue food that removes three segments is a relief late in a run.

A shrinking arena. Every 20 seconds the outer ring of cells flashes for 3 seconds and turns to wall. It adds time pressure and guarantees an end.

Two players on one screen. Two snakes in two colours share the grid. Hitting the other snake's body loses the round, and a head-on crash is a draw. On a keyboard one player has WASD and the other the arrows. On a phone each player gets two turn buttons on their own edge. The two-player guide covers the layouts.

High scores

The game-over card needs three things: the score of this run, the best score and a clear mark when the best was beaten. A best score for the session is a small request. One that must still be there next week, or a top ten shared between players, is kept in a scores list on the game's backend, which the leaderboard guide explains.

Messages for after the first run

  • "My swipes feel late. Register the turn as soon as my finger has moved 25 pixels."
  • "The start is too slow. Begin at 130 milliseconds per step."
  • "Food sometimes appears inside the snake. Only place it on empty cells."
  • "Add a settings toggle between swipes and four arrow buttons."
  • "Add an easy mode where the edges wrap around, with its own best score."
  • "In the phone version, vibrate briefly when the snake eats."

Your turn

Open the builder, paste one of the two descriptions and play the classic before you add a twist.

Frequently asked

How fast should the snake move?

Start with one step every 140 to 160 milliseconds, which is six or seven steps per second. Speed up as the snake eats, and stop at about 90 milliseconds on a phone or 70 in a browser with keys.

Why does my snake die when I turn quickly?

Two turns arrived inside one step and the second one pointed the snake back into its own neck. The game should queue the turns, apply one per step and compare each with the last step that was really made.

Are swipes or buttons better for snake on a phone?

Swipes keep the whole screen for the grid and suit most players. Buttons are more exact at high speed but take up part of the screen. Many games offer both in a settings toggle.

Should the snake die at the walls or wrap around?

Walls give the classic, harder game with shorter runs. Wrap-around is more forgiving and suits small phone grids. They play differently, so keep a separate best score for each.

Can two people play snake on one screen?

Yes. Two snakes share the grid and each player has their own controls: two sets of keys on a keyboard, or two turn buttons on each player's edge of a phone.

Build it now — free to start

Type the idea, sign in, and the agent builds it in your browser. Change anything by describing it.

Open the builder ›