xxxxxxxxxx
121
var x = 200;
var y = 200;
var gameOver = false;
var direction = 39;
var autopilot = false;
var compX = 100;
var compY = 200;
var compDir = 37
function setup() {
createCanvas(400, 400);
background(0, 0, 255);
fill(0);
rect(10, 10, 380, 380);
fill(0, 100, 0);
rect(350, 190, 20, 20);
textSize(40);
textAlign(CENTER);
frameRate(30);
strokeWeight(2);
}
function draw() {
if (gameOver === true) return;
human();
computer();
}
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 keyPressed() {
if (keyCode >= 37 && keyCode <= 40) direction = keyCode;
if (key === "a") autopilot = !autopilot;
if (key === "r") {
x = 200;
y = 200;
gameOver = false;
direction = 39;
background(0, 0, 255);
fill(0);
stroke(0);
rect(10, 10, 380, 380);
fill(0, 100, 0);
rect(350, 190, 20, 20);
frameCount = 0;
}
}
function selfDrive() {
if (direction === 39 && notBlack(x+2, y) === true) {
direction = 40;
if (direction === 40 && notBlack(x, y+2) === true) direction = 37;
if (direction === 37 && notBlack(x-2, y) === true) direction = 38;
if (direction === 38 && notBlack(x, y-2) === true) direction = 39;
}
}
function computer(){
if(notBlack(compX, compY) === true){
fill(255);
text("GAMEOVER\nComputer Loses", 200, 180);
gameOver = true;
fill(0, 0, 255);
stroke(0,0,255);
ellipse(compX, compY, 40, 40);
}
stroke(255,0,0);
point(compX, compY);
//add code here for computer autoPilot
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 human(){
if (notBlack(x, y) === true) {
fill(255);
text("Game Over!", 200, 120);
gameOver = true;
}
//ellipse(x, y, 40, 40);
fill(0, 100, 0);
rect(5, 7, 85, 40);
fill(255);
text(frameCount, 40, 40);
stroke(203, 195, 227);
point(x, y);
if (autopilot === true) {
selfDrive();
}
if (direction === 37) x = x - 2;
if (direction === 38) y = y - 2;
if (direction === 39) x = x + 2;
if (direction === 40) y = y + 2;
}