Skip to content
Laviyx Learn

Try

Mini project

Snake Game with JavaScript and Canvas

The classic snake game drawn on an HTML canvas, with a game loop, keyboard and touch controls, growing speed, collision checks and a saved high score.

Beginner 1 week 1 studentUpdated

HTML canvasJavaScriptlocalStorage

The problem

Building a small game is one of the best ways to understand loops, arrays, events and state. Snake looks simple, but it needs a game loop, a grid, collision detection and careful input handling — the same ideas used in bigger games and animations.

Features to build

  • Draw the board, snake and food on a canvas using a grid of square cells
  • Move the snake one cell per tick in the current direction
  • Change direction with the arrow keys or WASD, and with swipes on phones
  • Ignore a turn straight back into the snake's own body
  • Place food at a random empty cell, and grow the snake when it eats
  • End the game when the snake hits a wall or itself, with a restart button
  • Show the score, speed up every 5 foods, and save the high score in localStorage
  • Pause and resume with the space bar

Build plan

  1. 1

    Set up the grid

    Choose a cell size and board size, and store the snake as an array of {x, y} cells with the head first.

  2. 2

    Write the game loop

    On each tick, work out the new head from the direction, add it to the front, and remove the tail unless food was eaten. Then redraw everything.

  3. 3

    Handle input

    Store the next direction in a variable when a key is pressed, and apply it at the next tick, so two quick presses cannot turn the snake into itself.

  4. 4

    Detect collisions

    Check the new head against the walls and against every body cell. Keep the body cells in a Set of 'x,y' strings for fast checks on long snakes.

  5. 5

    Food, score and speed

    Pick a random cell that is not on the snake, increase the score, and shorten the tick interval as the score grows.

Learn these first

Each part of the project is taught in one of these free lessons.

Make it stand out

  • Wrap-around walls as a game mode
  • Obstacles that appear on later levels
  • Sound effects with the Web Audio API
  • A two-player mode on one keyboard

Viva and interview questions

Try to answer each one aloud before you open it.

  • What is a game loop?

    A repeating cycle that reads input, updates the game state and draws the result. In this game it runs at a fixed speed, once per tick, using setInterval or requestAnimationFrame with a time check.

  • Why apply a direction change only on the next tick?

    If the player presses up and then left within one tick, applying both immediately could turn the snake back into its own body. Storing one queued direction and applying it at the tick keeps each move valid.

  • How do you place food so it never appears on the snake?

    Pick a random cell and try again if it is on the snake, or build a list of all empty cells and pick one at random. The second way never loops forever, even when the board is almost full.

  • requestAnimationFrame or setInterval — which is better?

    requestAnimationFrame runs in time with the screen's refresh and pauses in hidden tabs, so it is smoother and saves battery. Because the snake moves in steps, you still track elapsed time and only move when a tick's worth of time has passed.

What to put in your project report

  1. Abstract: the problem, your solution and the result in one paragraph.
  2. Introduction and problem statement: who has the problem and why it matters.
  3. Requirements: the features (functional) and qualities such as speed and security (non-functional).
  4. Design: architecture diagram, database tables or data flow, and screen layouts.
  5. Implementation: the main modules and the most interesting code, explained.
  6. Testing: the test cases you ran, with inputs, expected and actual results.
  7. Results: screenshots or numbers that show the project working.
  8. Conclusion and future scope: what you achieved and what you would add next.

Your college may give its own format. Follow that first, and use this list to check nothing is missing.

All project ideas →