Back to Snippets

Classic Retro Snake Game (HTML, CSS, JS)

Snake on a fixed grid. Arrow keys steer, food adds a segment, and hitting a wall or your own tail ends the run.

The snake is stored as an array of coordinates. Each tick pushes a new head onto the front and pops the tail off the back, unless food was just eaten. That one trick handles movement and growth together, without tracking segments separately.

Speed is a setInterval delay near the top of the script, so lower it for a harder game. Food placement retries until it lands on a square the snake is not already sitting on.

881
#javascript
#html
#css
#snake game
#retro game
#classic game
#arcade game
#2d game
#canvas game
#online game
#browser game
#simple game
#keyboard controls