xxxxxxxxxx
43
let snake;
let rez = 10;
let w;
let h;
let food;
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(10);
w = floor(width / rez);
h = floor(height / rez);
snake = new Snake();
foodLocation();
}
function foodLocation() {
let x = floor(random(w));
let y = floor(random(h));
food = createVector(x, y);
}
function keyPressed() {
if (keyCode == UP_ARROW) {
snake.direction(0, -1);
} else if (keyCode == DOWN_ARROW) {
snake.direction(0, 1);
} else if (keyCode == RIGHT_ARROW) {
snake.direction(1, 0);
} else if (keyCode == LEFT_ARROW) {
snake.direction(-1, 0);
}
}
function draw() {
scale(rez);
background(0);
snake.update();
snake.show();
fill(255, 0, 0);
noStroke();
rect(food.x, food.y, 1, 1);
}