Skip to content
Ayotl

← How a game is made: Daily Challenge's Snake

Coding Snake, part 1: the code

This is the video's code, as is: a playable Snake in JavaScript, with no engine or libraries. The names and the logic are those of Daily Challenge's Snake. What's missing (the maps with walls, the bonus bug, the hunger, the sound and the server) is part 2.

The video

Watch on YouTube →

Play it here

With the keyboard's arrows or with the buttons. It's the same code as below.

Copy it

Each tab is one of the video's files. All in one is the same program in a single file.

<!doctype html>
<style>
  canvas {
    width: 480px;
    image-rendering: pixelated;
  }
</style>
<canvas></canvas>
<script type="module" src="main.js"></script>

$ npx serve

→ http://localhost:3000

How to run it on your computer

  • The easy way: copy "All in one", save it as snake.html and double-click it.
  • As in the video: save the files, each with its own name, in one folder. Modules don't open with a double click: in that folder run npx serve (or python3 -m http.server) and open the address it gives you (with Python it's http://localhost:8000).

The course, piece by piece and with exercises →