xxxxxxxxxx
53
let s;
let scl;
let food;
let score;
function setup() {
createCanvas(800, 800);
s = new Snake();
scl = 20;
s.scl = scl;
pickLocation();
score = createDiv('');
}
function draw() {
background(0);
translate(width / 2, height / 2);
frameRate(8+(0.1*s.total));
s.death();
s.update();
s.show();
if (s.eat(food)) {
pickLocation();
}
fill(255, 0, 100);
rect(food.x, food.y, scl, scl);
score.html('Score: '+s.total+' / Speed: '+(8+(0.1*s.total))+' / Location: ('+s.x+','+s.y+')');
}
function pickLocation() {
let cols = floor(width / scl);
let rows = floor(height / scl);
food = createVector(floor(random(-cols / 2, cols / 2)), floor(random(-rows / 2, rows / 2)));
food.mult(scl);
}
function keyPressed() {
if (keyCode === UP_ARROW) {
s.dir(0, -1);
} else if (keyCode === DOWN_ARROW) {
s.dir(0, 1);
} else if (keyCode === RIGHT_ARROW) {
s.dir(1, 0);
} else if (keyCode === LEFT_ARROW) {
s.dir(-1, 0);
}
}
function mousePressed(){
s.total++;
}