xxxxxxxxxx
78
let x = 200;
let y = 200;
let gameOver = false;
let direction = 39;
function setup() {
createCanvas(400, 400);
background(0, 0, 255);
fill(0);
rect(10, 10, 380, 380);
textSize(24);
textAlign(CENTER);
}
function draw() {
if (gameOver === true) {
return;
}
if (notBlack() === true) {
fill(255);
text("GAME OVER", 200, 180);
gameOver = true;
fill(255, 0, 0);
stroke(255, 0, 0);
ellipse(x, y, 40, 40);
}
fill(0, 100, 0);
rect(0, 0, 75, 25);
fill(255);
text(frameCount, 37, 20);
stroke(255);
point(x, y);
if (direction === 37) {
x = x - 1;
}
if (direction === 38) {
y = y - 1;
}
if (direction === 39) {
x = x + 1;
}
if (direction === 40) {
y = y + 1;
}
}
function keyPressed() {
if (key === "r") {
x = 200;
y = 200;
gameOver = false;
direction = 39;
background(0, 0, 255);
fill(0);
rect(10, 10, 380, 380);
frameCount = 0;
}
if (keyCode === 37) {//left
direction = 37;
}
if (keyCode === 38) {//up
direction = 38;
}
if (keyCode === 39) {//right
direction = 39;
}
if (keyCode === 40) {//down
direction = 40;
}
}
function notBlack() {
let a = get(x, y);
if (a[0] !== 0) return true;
else if (a[1] !== 0) return true;
else if (a[2] !== 0) return true;
return false;
}