A13 – Snake

Create a snake game using files named snake.html, snake.css, and snake.js.

The rules of the game are pretty standard.  See this online version if you’re unfamiliar with the game.

  • The play area (or board) is a grid of squares, like a chess board, but larger.
  • A snake is placed at a random location on the board.  It has the same width as the grid squares, and travels along the grid squares.
  • Similarly, an element representing food is placed at random in one of the squares.
  • The snake travels either up, down, left or right and at a constant velocity throughout the game.
  • The user controls the direction of the snake using the arrow keys.

  • The game begins when the user presses a button.
  • When the game begins the snake begins moving.
  • If the snake travels through the square with the food, the snake grows in length by 1 and another food element is placed in a random square on the board.
  • If the snake hits the edges of the board or itself, the game is over and the number of food elements eaten is displayed on the page.

Note: there are a number of JavaScript solutions for this game on the web.  Don’t look at these solution’s source code – it’s not allowed and is considered cheating.  You can do this without resorting to that.  Talk to me about strategy if you’d like.

This assignment is due before class on February 23.

© 2020 – 2021, Eric. All rights reserved.