xxxxxxxxxx
94
let snake;
let res = 15;
let w;
let h;
let c;
function setup() {
createCanvas(600, 400);
w = floor(width/res);
h = floor(height/res);
frameRate(10);
snake = new Snake();
foodLocation();
c = color(random(100,250),random(0,125),random(100,175));
startGame();
}
function foodLocation(){
let x = floor(random(w));
let y = floor(random(h));
food = createVector(x, y);
}
function keyPressed(){
if (keyCode === LEFT_ARROW){
snake.setDir(-1,0);
} else if (keyCode === RIGHT_ARROW){
snake.setDir(1,0);
} else if (keyCode === DOWN_ARROW){
snake.setDir(0,1);
} else if (keyCode === UP_ARROW){
snake.setDir(0,-1);
}
}
function draw() {
scale(res);
background(220);
if (snake.eat(food)){
foodLocation();
}
snake.update();
noStroke();
fill(85);
textSize(1);
text("food",food.x, food.y);
snake.show();
if (snake.endGame()){
background(c);
textSize(5);
fill(255);
text("END GAME",7,15);
}
}
function mousePressed(){
startGame();
}
function startGame(){
snake = new Snake();
foodLocation();
}