xxxxxxxxxx
86
var x = 200;
var y = 200;
var gameOver = false;
var direction = 39;
function setup() {
createCanvas(800, 800);
background(0, 0, 255);
fill(0);
rect(3, 3, 793, 793);
fill(0,100,0);
rect(350,190,20,20);
textSize(40);
textAlign(CENTER);
frameRate(30);
strokeWeight(2);
}
function draw() {
if(gameOver === true)
return;
if (notBlack(x,y) === true) {
fill(255);
textSize(90);
text("Game Over!", 400, 100);
gameOver = true;
fill(255,0,0);
stroke(255,0,0);
ellipse(x,y,40,40);
}
fill(0,100,0);
rect(0,0,125,50);
textSize(40);
fill(255)
text(frameCount,65,40);
stroke(255);
point(x, y);
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;
}
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 (key === 'a'){
autopilot= !autopilot;
}
if (key === 'r'){
x = 200;
y = 200;
gameOver = false;
direction = int(random(37,41));
background(0,0,225);
fill(0);
stroke(0);
rect(3,3,793,793);
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;
}