xxxxxxxxxx
86
let x = 200;
let y = 200;
let gameOver = false;
var dir = 39;
let compX = 380;
let compY = 200;
let compDir = 37;
function setup() {
createCanvas(400, 400);
background(0, 0, 255);
fill(0);
//stroke(0);
rect(5, 5, 390, 390);
stroke(255);
fill(255);
textSize(50);
textAlign(CENTER);
frameRate(30);
strokeWeight(2);
}
function draw() {
if (gameOver === true) {
return;
}
human();
computer();
}
function human() {
if (notBlack() === true) {
text("Game Over", 200, 120);
gameOver = true;
}
point(x, y);
if (dir === 37) {
x = x - 2;
}
if (dir === 38) {
y = y - 2;
}
if (dir === 39) {
x = x + 2;
}
if (dir === 40) {
y = y + 2;
}
}
function computer() {
if (notBlack(compX, compY) === true) {
text("Game Over", 200, 120);
gameOver = true;
}
point(compX, compY);
if (compDir === 37) {
compX = compX - 2;
}
if (compDir === 38) {
compY = compY - 2;
}
if (compDir === 39) {
compX = compX + 2;
}
if (compDir === 40) {
compY = compY + 2;
}
}
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;
}
function keyPressed() {
if (keyCode >= 37 && keyCode <= 40) dir = keyCode;
if (key === "r") {
x = 200;
y = 200;
gameOver = false;
direction = 39;
background(0, 0, 255);
fill(0);
}
}