Eat the apples, grow longer, and never bite yourself
← Back to Fun TimeThis game is not one big lump of code. It is split into five small library files inside the funtime/lib/ folder, and every single function has a comment above it saying exactly what goes in, what comes out, and the algorithm in plain English.
That means you can delete the body of any function, read its comment, and write it yourself — the rest of the game keeps working as soon as yours is correct.
| File | What it does | Good functions to try re-writing |
|---|---|---|
| lib/snake-grid.js | Squares, positions and directions | samePosition, addDirection, containsPosition, randomEmptyCell |
| lib/snake-game.js | The rules: crawling, eating, growing, dying | moveSnake, stepGame, turnSnake, scoreForFood, stepIntervalForLevel |
| lib/snake-draw.js | Painting the black-and-white picture | drawSegment, drawHead, drawFood |
| lib/snake-input.js | Keyboard and touch buttons | actionForKey, directionForAction |
| lib/snake-main.js | The glue between the page and the rules | updateScoreboard, buildActions, gameLoop |
💡 The big idea: the snake is just a list of squares. Moving it means adding a head and dropping a tail; eating means adding a head and keeping the tail. That is the whole game, in moveSnake.
🛠️ Want to be walked through it? Build Snake Yourself takes you through twelve of these functions one step at a time, tests each one you write, and shows the game growing as you go.