xxxxxxxxxx
192
let pause = false;
let gameOver = false;
let autoPilot = true;
let x = 20;
let y = 200;
let dir = 39;
let compX = 380;
let compY = 200;
let compDir = 37;
function setup() {
createCanvas(400, 400);
background("blue");
fill(0);
rect(10,10,380,380);
fill("blue");
}
function draw() {
if (gameOver === true) {
return;
}
textSize(18);
strokeWeight(1);
noStroke();
fill("blue");
rect(9,362,382,30);
stroke(0);
fill("white");
text("Move w/ Arrow Keys | Press 'a' for autopilot", 27,385);
fill("blue");
stroke("red");
rect(9,9,50,25);
textSize(22);
fill("white");
text(int((frameCount)/60), 28,30);
textSize(20);
if (autoPilot === true) {
stroke("red");
fill("blue");
rect(250,10,140,25);
fill("white");
text("Autopilot: On", 260,30);
} else {
stroke("red");
fill("blue");
rect(250,10,140,25);
fill("white");
text("Autopilot: Off", 260,30);
}
human();
computer();
}
function human() {
stroke("magenta");
point(x,y);
if (autoPilot === true) {
selfDrive();
}
if (dir === 37) {
x = x - 1;
}
if (dir === 38) {
y = y - 1;
}
if (dir === 39) {
x = x + 1;
}
if (dir === 40) {
y = y + 1;
}
if (notBlack(x,y) === true) {
noStroke();
fill("red");
ellipse(x,y,40,40);
fill("white");
textSize(30);
text("GAME OVER\nComputer Wins!\nScore: " + int((frameCount)/60), 120, 180);
gameOver = true;
textSize(50);
noStroke();
textSize(20);
}
}
function computer() {
stroke("orange");
point(compX,compY);
compDrive();
if (compDir === 37) {
compX--;
}
if (compDir === 38) {
compY--;
}
if (compDir === 39) {
compX++;
}
if (compDir === 40) {
compY++;
}
if (notBlack(compX,compY) === true) {
noStroke();
fill("red");
ellipse(compX,compY,40,40);
fill("white");
textSize(30);
text("GAME OVER\nPlayer Wins!\nScore: " + int((frameCount)/60), 120, 180);
gameOver = true;
textSize(50);
noStroke();
textSize(20);
}
}
function keyPressed() {
if (keyCode === 37) {
dir = 37;
}
if (keyCode === 38) {
dir = 38;
}
if (keyCode === 39) {
dir = 39;
}
if (keyCode === 40) {
dir = 40;
}
if (key === 'a') {
autoPilot = !autoPilot;
}
}
function notBlack(x,y) {
var 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 selfDrive() {
if(dir === 39 && notBlack(x+1,y) === true ) {
dir = 40;
}
if(dir === 40 && notBlack(x,y+1) === true ) {
dir = 37;
}
if(dir === 37 && notBlack(x-1,y) === true ) {
dir = 38;
}
if(dir === 38 && notBlack(x,y-1) === true ) {
dir = 39;
}
}
function compDrive() {
if(compDir === 39 && notBlack(compX+1,compY) === true ) {
compDir = 40;
}
if(compDir === 40 && notBlack(compX,compY+1) === true ) {
compDir = 37;
}
if(compDir === 37 && notBlack(compX-1,compY) === true ) {
compDir = 38;
}
if(compDir === 38 && notBlack(compX,compY-1) === true ) {
compDir = 39;
}
}