Eat apples, grow longer, and don't crash!
Reach the goal score by eating food, without running out of lives. Crashing into a wall or into your own tail costs a life, and the snake restarts.
You write these functions. The game calls them when something happens.
onEat()
Runs every time the snake eats food.
function onEat() {
score = score + 1;
}onCrash()
Runs when the snake hits a wall (unless wrap is on) or its own body.
function onCrash() {
lives = lives - 1;
}Variables at the top of your code are the game's settings. Change a value, press Play, and see what happens.
Each step shows code you can add to the end of your game's code. A function or variable written later replaces the earlier one, so you never have to delete anything.
Steer the snake to the food. Each apple runs onEat, and each crash runs onCrash.
function onEat() {
score = score + 1;
}Set wrap to true and the snake can no longer crash into the walls: it appears on the opposite side.
let wrap = true;Make every apple add 2 segments to the snake and 3 points to your score.
let growth = 2;
function onEat() {
score = score + 3;
}Every time your score is a multiple of 3, the snake gets faster (never above 5). The % sign gives the remainder after dividing.
function onEat() {
score = score + 1;
if (score % 3 === 0 && speed < 5) {
speed = speed + 1;
}
}A crash can change more than your lives. Here it costs a life and puts the speed back to 2.
function onCrash() {
lives = lives - 1;
speed = 2;
}// ๐ Snake!
let head = "๐"; // your snake's head
let snake = "๐ฉ"; // its body
let food = "๐"; // what it eats
let goal = 10; // reach this score to win
let lives = 3;
let speed = 2; // 1 (slow) to 5 (fast)
let wrap = false; // true: leave one side and come back on the other
let growth = 1; // how many segments one apple adds
function onEat() {
score = score + 1;
}
function onCrash() {
lives = lives - 1;
}