Skip to content
Ayotl

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

Coding Snake, part 2: the code

This is the video's code, as is: the one from part 1 turned into Daily Challenge's Snake, with the same names and the same logic. The map is text, with its walls; the randomness comes from a seed, today's date, so today every game starts the same (and tomorrow, another); every day three extra walls show up; every five fruits a bonus bug appears, and thirty seconds without eating end the game. When you crash, verifyRun replays the game with the very same keys, like the server, and writes the result to the browser's console.

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-2.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 →