xxxxxxxxxx
111
let x = 300;
let y = 100;
let gameOver = false;
let direction = 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);
textAlign(CENTER);
strokeWeight(2);
textSize(30);
frameRate(30);
}
function draw() {
if (gameOver === true) {
return;
//return just stops the whole program altogether
}
if (notBlack(x,y) === true) {
gameOver = true;
fill(255,0,0);
stroke(255,0,0);
//ellipse(x,y,10,10);
fill(255);
stroke(255);
text("GAME OVER", 200,180);
text("TIME PLAYED: " + frameCount, 200,220);
}
stroke(0,0,255);
fill(0,0,255);
rect(160,360,80,40);
stroke(255);
fill(255);
text(frameCount,200,390)
point(x,y);
//direction = 37;
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 selfDrive() {
if(direction == 39 && notBlack(x + 2, y) === true) {
direction = 40;
} else if (direction == 40 && notBlack(x, y + 2) === true) {
direction = 37;
} else if(direction == 37 && notBlack(x - 2, y) === true) {
direction = 38;
} else if(direction == 38 && notBlack(x, y - 2) === true) {
direction = 39;
}
}
function notBlack(x,y) {
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;
}
//problem in here somewhere
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 = 300;
y = 100;
gameOver = false;
direction = 37;
frameCount = 0;
}
}