Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Make a browser Snake game by storing the snake and food as grid coordinates, advancing the rules at a timed interval, and drawing the current state on a canvas. This keeps game speed independent of the screen’s refresh rate and makes movement, collision checks, and rendering easier to reason about.

Set up the canvas and grid

Create an HTML canvas and give it a CSS size. In JavaScript, choose a logical grid—for example, 20 columns by 20 rows—and a cell size in pixels. The canvas might be 400 by 400 pixels, but the game state should use integer grid coordinates such as (4, 8), not pixel positions. Convert a cell to pixels only when drawing it.

Canvas is a drawing surface: its shapes are not separate DOM elements that can be moved after they are drawn. The usual animation approach is to clear or redraw the surface as the scene changes. See MDN’s drawing graphics guide and MDN’s canvas animation guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the game state and rules

Keep the rules separate from the drawing code. A compact state model needs an ordered list of snake cells, a current direction, a queued direction, a food cell, a score, and a play state such as ready, playing, or game over. These are implementation choices, not a browser-standard Snake model.

#1 Best Overall
Magnetic Snakes and Ladders Board Game Set - 9.6 Inches
  • This is an old Indian board game, 2+ players; suitable for fun of all ages; the playing field has numbered grid squares; the purpose of the game is to roll the dice, with the help or hindrance of ladders and snakes respectively
  • Portable and foldable board: The complete set measures 9.6 x 8.3 x 0.9 inches (24.5 x 24.5 x 2.4 cm) and weighs 0.4 kg (0.86 lb); store the chess pieces and dice in the box
  • Our powerful magnets can hold all your game parts in place. Even if you accidentally bump into it, your snake ladder board game will stay still.
  • The magnetic characteristics of the board and pieces mean that your game will remain as it is Complete accessories: The set comes with four different red, green, yellow and blue chess pieces and a pair of dice
  • Movement: On each game step, add one cell in the current direction.
  • Food and growth: If the new head lands on food, increase the score and keep the tail in place for that step. Otherwise remove the last tail cell.
  • Walls: This example design treats leaving the grid as game over. Wraparound is another possible rule, but choose one and use it consistently.
  • Self-collision: A move into a snake cell is game over, except when that cell is the tail’s old position and the tail is moving away on the same step. For that exception, remove the tail before testing occupancy, and only do so on a non-growth move.

When placing food, choose an unoccupied grid cell. If every cell is occupied, handle that full-board state explicitly—for example, declare a win—instead of retrying placement indefinitely.

Read input without allowing instant reversals

Use keyboard event listeners to queue direction changes. Do not immediately change the direction several times between movement steps: a quick pair of opposite inputs could otherwise send the snake back into itself. At each game step, accept at most one queued direction, and reject a direction opposite to the current one. For example, if the snake is moving right, ignore a request to move left.

Choose controls and show them near the canvas. If the game has a start or restart action, make it visible and connect it to the same state model rather than relying on an undocumented key.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Gothink Wooden Snakes and Ladders Game - 13 Inch Educational Board Game for Children Ages 3 and Up & Adults - Classic Board Game Includes 6 Counters & 2 Dice, Rules
  • 【CLASSIC SNAKES AND LADDERS】The board features the classic Snakes and Ladders design, with bright and colorful illustrations that are sure to delight players of all ages. A board game for 2 to 6 players - throw the dice and move along the board, they can climb ladders and advance toward the winning spot or lose their place and ride the snakes back down. It is an exciting family game.
  • 【NO READING REQUIRED】 This is a great game for children who haven't learned how to read yet. The numbers on the gameboard give preschoolers all they need to know!
  • 【SAFE AND DURABLE】This Snakes and Ladders is made from superior pine, which is tough and dense, well-constructed and feels smooth.The wood is certified by the Forest Stewardship Council (FSC) and meets universal sustainability standards. All the materials pass the toy safe test of EN71 (European Toy Safety Standard) and ASTM (American Toy Safety Standard)
  • 【SKILL DEVELOPMENT】This snakes and ladders board game is an educational toy that develops fine motor skills, colour recognition, hand-eye coordination, basic numeracy and problem solving skills.
  • 【WONDERFUL GIFT CHOICE】This Snakes and Ladders will be a nice gift for your friends and a whole family of all ages on every occasion. It is a good gift choice for kids and adults on birthdays, Christmas, Halloween, Thanksgiving, and other festivals.

Advance the game by elapsed time

Use requestAnimationFrame() to schedule drawing at browser repaint opportunities, but do not treat each callback as one Snake move. It is a one-shot callback request: a continuing animation must schedule another callback. Its rate generally follows the display refresh rate, which may vary; MDN lists 60, 75, 120, and 144 Hz as examples, not guarantees. MDN also warns that animation progress should use the callback timestamp or another time source, otherwise animation can run faster on high-refresh-rate screens. See MDN’s requestAnimationFrame() documentation.

Track elapsed time in the callback and advance the game only when a chosen movement interval has passed. For example, a 150-millisecond step interval is a design setting, not a value prescribed by the browser. The callback can accumulate elapsed time, run discrete game steps as needed, then redraw once. This keeps the game’s movement cadence separate from the display’s repaint cadence.

A typical loop has this shape:

let previousTime = 0;
let elapsed = 0;
const stepMs = 150;

function frame(timestamp) {
  const delta = timestamp - previousTime;
  previousTime = timestamp;
  elapsed += delta;

  while (elapsed >= stepMs && state.playing) {
    updateGame();
    elapsed -= stepMs;
  }

  drawGame();
  requestAnimationFrame(frame);
}

requestAnimationFrame(frame);

In a complete implementation, initialize or reset the time values when starting or restarting. Also decide what to do with unusually large time gaps: cap catch-up steps or reset the accumulator so a delayed frame does not cause a burst of movement.

Rank #3
Sale
ColorGo Magnetic Snakes and Ladders Board Game Set, 9.76 Inches
  • 【ColorGo SNAKES AND LADDERS】Hailing from ancient India, this classic world-renowned travel game is a delightful engaging activity for kids of all ages. Go forward or backward, experience surprise when you find the bottom of the stairs or the head of snakes. Our magnetic snakes and ladders board game keeps you engrossed from start to finish
  • 【HIGH-QUALITY CONSTRUCTION】 The chess board and game pieces are crafted from superior quality HIPS plastic maintaining longevity. It includes 1 Playing Board, 2 Dice and 4 Pawns
  • 【PERFECT MAGNETISM】The chess board every individual pieces have a magnetic attraction to the 100-square playing field; light magnetism would make it stable to play while not hard to move the pieces while playing in windy weather, outdoors or travelling
  • 【PROTABLE AND FOLDABLE DESIGN】Open size of snake games board is 9.76*9.76*0.77 Inches, while folded size is 9.76*4.88*1.54 Inches. This perfectly sized snakes and ladders board is small enough to travel with, but also large enough to play comfortably
  • 【EXQUISITE GIFT】Novel style - suitable for home, company, school, journey, bus or picnic. The rules are simple enough and no reading required to play. ColorGo's well-desigined color box packaging is very suitable as a gift for your family,friends and everyone you care about

Update movement, collisions, food, and score

Keep one game step in a function such as updateGame(). Apply the queued turn, calculate the candidate head cell, determine whether it hits food, and then apply the selected collision and body rules. A reliable order is:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Apply a legal queued turn.
  2. Calculate the next head coordinate.
  3. Check whether it crosses a wall and whether it will grow by reaching food.
  4. If it will not grow, remove the tail cell before checking the remaining body; this permits moving into the tail’s vacated cell.
  5. Check the candidate head against the remaining body. If occupied, end the game.
  6. Add the new head. If it reached food, increase the score and place food in an empty cell; otherwise the earlier tail removal completes the move.

Keep the collision order aligned with the growth rule. If the snake eats food, the tail does not vacate a cell on that turn, so the head cannot safely enter the tail’s current cell.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Draw the current state

Make drawGame() read the state and render it without changing game rules. Clear the canvas, draw the snake’s cells, draw the food, and show the score and any ready or game-over message. Convert each grid coordinate to its pixel origin by multiplying by the cell size. Separating this function from movement makes it easier to inspect whether a bug is in the rules or in the display.

Rank #4
Solid Wood Ludo and Snake and Ladder Board Game Set -11.82 inches, Classic Board Game 2 in 1.Ludo Game Set.Snake and Ladder Game Set.
  • Classic game set: Ludo game on the front, snake and ladder game on the back. A very classic game that can be owned simultaneously.
  • Solid wood craftsmanship manufacturing: This double-sided game board is made of bamboo, with a smooth and clean surface, clear and perfectly printed board patterns, making it a game board worth cherishing.
  • Idea Ideal Family Game: The board size is 11.82 inches (30 centimeters), suitable for 2-4 players to start the game simultaneously in the Ludo and Snake Ladder games, equipped with 2 dice and 4 colored chess pieces (a total of 20 pieces).
  • Chessboard design: The chessboard is slotted for easy storage of chess pieces, and a specially designed cloth bag is used to protect the chessboard. At the end of the game, it is convenient to store the game set.
  • Provide Ludo Board Game+Snakes&Ladders game tutorial cards. For your satisfaction, please feel free to contact us if you have any dissatisfaction or questions. The entire set weighs 2.03lbs (0.92kg) .

Pause, restart, and handle hidden tabs

Browsers commonly pause animation callbacks in background tabs and hidden iframes. When the player returns, a timestamp gap may be much larger than one game step. Decide deliberately whether to pause, reset accumulated elapsed time, or cap catch-up moves; do not let a hidden-tab interval create uncontrolled movement. MDN documents the callback behavior and timing considerations in its requestAnimationFrame() reference.

On restart, restore the initial snake, direction, score, and food, clear any queued turn, and reset the animation timing state. Make sure only one continuing animation loop is scheduled; starting a second loop can make the game appear to speed up.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the finished game

  • Confirm the snake moves one grid cell per game step, regardless of how many redraws occur between steps.
  • Try food directly ahead and confirm the snake grows and the score changes.
  • Test wall impact and self-collision against the exact rules chosen for the game.
  • Test a turn into the tail’s old cell on a non-growth move, then test the same situation while eating.
  • Send several quick direction inputs and confirm they cannot produce an unintended reversal within one movement step.
  • Switch away from the tab and return; verify the selected pause or catch-up behavior and restart the game to check timing resets.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.