xxxxxxxxxx
164
let x = 20;
let y = 200;
let gameOver = false;
let direction = 39;
var compX = 380;
var compY = 200;
var compDir = 37
function setup() {
createCanvas(400, 400);
background(0,0,255);
fill(0);
rect(10,10,380,380);
textAlign(CENTER);
strokeWeight(2);
textSize(30);
frameRate(30);
}
function draw() {
if (gameOver === true) {
return;
//return just stops the whole program altogether
}
human();
computer();
}
function human() {
if (notBlack(x,y) === true && notBlack(compX,compY) === false) {
gameOver = true;
fill(255,0,0);
stroke(255,0,0);
ellipse(x,y,30,30);
fill(255);
stroke(0);
text("GAME OVER: YOU LOSE", 200,180);
text("Time: " + frameCount, 200,220);
return;
}
stroke(0,0,255);
fill(0,0,255);
rect(160,360,80,40);
stroke(255);
fill(255);
text(frameCount,200,390)
point(x,y);
stroke(255,100,255)
fill(255,100,255);
//direction = 37;
if (direction === 37) {
x = x - 2;
}
if (direction === 38) {
y = y - 2;
}
if (direction === 39) {
x = x + 2;
}
if (direction === 40) {
y = y + 2;
}
}
function computer() {
if (notBlack(compX,compY) === true && notBlack(x,y) === false) {
gameOver = true;
fill(255,0,0);
stroke(255,0,0);
ellipse(compX,compY,30,30);
fill(255);
stroke(0);
text("GAME OVER: YOU WIN", 200,180);
text("Time: " + frameCount, 200,220);
return;
}
stroke(0,0,255);
fill(0,0,255);
rect(160,360,80,40);
stroke(255);
fill(255);
text(frameCount,200,390)
stroke(255,100,255)
fill(255,100,255);
point(compX,compY);
//direction = 37;
compDrive();
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 compDrive() {
if(compDir == 39 && notBlack(compX + 2, compY) === true) {
compDir = 40
}
if(compDir == 40 && notBlack(compX, compY + 2) === true) {
compDir = 37;
}
if(compDir == 37 && notBlack(compX - 2, compY) === true) {
compDir = 38;
}
if(compDir == 38 && notBlack(compX, compY - 2) === true) {
compDir = 39;
}
}
function notBlack(x,y) {
let a = get(x,y);
//let b = get(compX,compY)
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) {
direction = 37;
}
if (keyCode === 38) {
direction = 38;
}
if (keyCode === 39) {
direction = 39;
}
if (keyCode === 40) {
direction = 40;
}
if (key === 'r') {
background(0,0,255);
fill(0);
stroke(0);
rect(10,10,380,380);
x = 20;
y = 200;
compX = 380
compY = 200
gameOver = false;
direction = 39;
compDir = 37;
frameCount = 0;
}
}