Skip to content
Tutorials Center
My learning

Topics

Lesson 117 of 132 ยท Projects

Project: Snake Game

Build the classic Snake game in React with a pure reducer, a setInterval game loop, keyboard controls, collision detection, scoring, pause and restart - every stage runs live.

Snake is a small game with a big lesson: how to run a game loop in React. The trick is to separate three things. The rules live in a pure reducer (given a state and an action, return the next state). The clock is an effect that dispatches a tick action on an interval. The input is another effect that listens for the keyboard and dispatches turn actions. If you have not met reducers, read reducers and effect cleanup first.

The board is a grid of cells. The snake is an array of {x, y} cells with the head first. Each tick the head moves one cell in the current direction; if it lands on food the snake grows, otherwise the tail is dropped.

Stage 1: a snake that moves

The reducer handles two actions. Because turn only stores the next direction and tick applies it, two quick key presses within one tick can never reverse the snake into itself. The wrap makes the snake loop around the edges for now.

Stage 1: moving and turning

Both effects use an empty dependency list: the interval and the key listener are set up once and removed on unmount. They can do this because they only call dispatch, which is stable, and never read state. That is a big win of reducers: the loop never goes stale.

Stage 2: food, growth, score and game over

Now the walls are deadly and so is the snake's own body. Food is placed on a random free cell. To keep the reducer pure we do not call Math.random() inside it; the clock passes a random number in the action, and the reducer turns it into a cell. This makes the game replayable and testable.

Stage 2: the complete rules

The subtle line is const body = ate ? state.snake : state.snake.slice(0, -1). When the snake does not eat, its tail cell is free again, so the head may legally move into the cell the tail is leaving. When it eats, the tail stays and the full body counts as an obstacle.

Stage 3: pause, speed and touch buttons

Last, polish. Pausing stops the interval (the effect depends on paused), speed increases with the score, and on-screen arrow buttons make the game playable without a keyboard. Because the interval now depends on paused and delay, it is recreated when they change, and cleanup clears the old one.

Stage 3: pause, speed and buttons

Ideas to extend it: store the best score in memory, add walls that wrap, or render the board with a <canvas> as in the paint project.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the interval effect in stage 1 use an empty dependency array and still work?

    A reducer lets the effect send actions without reading the current state, so there are no stale values to worry about and no need to restart the timer.
  2. 2. Why is Math.random() called outside the reducer and passed in the action?

    Reducers should be predictable and testable, and React may call them more than once. Passing the random number in keeps them pure.
  3. 3. Why does turn store next while tick copies it into dir?

    The opposite-direction check compares against the direction actually used last tick. A pending direction only takes effect when the next tick runs.
  4. 4. Why may the head move into the cell the tail is leaving when the snake does not eat?

    When no food is eaten, the tail is dropped as the head advances, so the collision check uses the body without its last cell.

Summary

  • Keep the rules in a pure reducer; drive it with tick and turn actions.
  • Use an interval effect for the clock and a keydown effect for input, each with cleanup.
  • Pass random numbers into actions to keep reducers pure.
  • Put values the interval depends on (paused, delay) in the effect's dependency list so it restarts correctly.
  • Offer on-screen buttons, pause and restart for accessibility and playability.

Next: Project: Hangman and Word Guess