xxxxxxxxxx
102
let x = 200;
let y = 200;
let gameOver = false;
let direction = 39;
function setup() {
createCanvas(400, 400);
background(0, 0, 255);
fill(0);
rect(10, 10, 380, 380);
textSize(22);
textAlign(CENTER);
frameRate(30);
strokeWeight(2);
frameCount=0
}
function draw() {
selfDrive()
text("framCount")
if (gameOver === true) {
return;
}
if (notBlack(x, y) === true) {
gameOver=true
fill(0);
text("GAME OVER", 200, 180);
fill(255, 0, 0);
ellipse(x, y, 10, 10);
var time=second()
text('your time was '+time+' seconds', 20,250,300,50)
}
stroke(255);
point(x, y);
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) {
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;
return false;
}
function keyPressed() {
if (key === "r") {
background(0, 0, 255);
fill(0);
noStroke()
rect(10, 10, 380, 380);
textSize(22);
textAlign(CENTER);
frameRate(30);
strokeWeight(2);
x = 200;
y = 200;
x=x+2
gameOver = false;
let direction = 39;
}
if (key === "a") {
direction = 37;
}
if (key === "w") {
direction = 38;
}
if (key === "d") {
direction = 39;
}
if (key === "s") {
direction = 40;
}
}
function selfDrive(){
if(direction===37 && notBlack(x-2,y)===true){
direction=38
}
if(direction===38 && notBlack(x,y-2)===true){
direction=39
}
if(direction===39 && notBlack(x+2,y)===true){
direction=40
}
if(direction===40 && notBlack(x,y+2)===true){
direction=37
}
}