xxxxxxxxxx
141
var x = 200;
var y = 200;
var gameOver = false;
var direction = "w";
var resetKey = false;
var autoPilot = false;
function setup() {
createCanvas(400, 400);
background(0, 144, 255);
fill(0);
stroke(0);
rect(5, 5, 390, 390);
print(get(2, 2));
textAlign(CENTER);
textSize(50);
//obstacles
fill(194, 279, 2);
stroke(234, 239, 123);
for (var i = 0; i < 10; i++) {
rect(random(20, 180), random(20, 180), 20, 20);
rect(random(210, 380), random(210, 380), 20, 20);
rect(random(20, 180), random(210, 380), 20, 20);
rect(random(210, 380), random(20, 380), 20, 20);
}
}
function draw() {
if (gameOver == true) {
if (resetKey == true) {
print("reset");
reset();
print("resetDone");
}
return; //stops the program
}
if (notBlack(x, y)) {
fill(224, 289, 235);
stroke(0);
strokeWeight(5);
text("Game Over! \n Click 'r' to restart", 200, 180);
gameOver = true;
}
strokeWeight(2);
stroke(255);
point(x, y);
if (autoPilot == true) {
selfDrive();
}
if (direction == "d") {
x += 2;
}
if (direction == "a") {
x -= 2;
}
if (direction == "w") {
y -= 2;
}
if (direction == "s") {
y += 2;
}
}
function notBlack(x, y) {
var a = get(x, y); //get() returns an array for the rgb and opacity of that position
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 == "s") {
direction = "s";
}
if (key == "a") {
direction = "a";
}
if (key == "d") {
direction = "d";
}
if (key == "w") {
direction = "w";
}
if (key == "e") {
autoPilot = !autoPilot;
}
if (key == "r") {
resetKey = true;
print("key");
}
}
function selfDrive() {
if (direction == "w" && notBlack(x, y - 4)) {
direction = "d";
}
if (direction == "d" && notBlack(x + 4, y)) {
direction = "s";
}
if (direction == "s" && notBlack(x, y + 4)) {
direction = "a";
}
if (direction == "a" && notBlack(x - 4, y)) {
direction = "w";
}
}
function reset() {
x = 200;
y = 200;
gameOver = false;
direction = "w";
resetKey = false;
autoPilot = false;
background(0, 144, 255);
fill(0);
stroke(0);
rect(5, 5, 390, 390);
print(get(2, 2));
textAlign(CENTER);
textSize(50);
//obstacles
fill(194, 279, 2);
stroke(234, 239, 123);
for (var i = 0; i < 10; i++) {
rect(random(20, 180), random(20, 180), 20, 20);
rect(random(210, 380), random(210, 380), 20, 20);
rect(random(20, 180), random(210, 380), 20, 20);
rect(random(210, 380), random(20, 380), 20, 20);
}
}