xxxxxxxxxx
89
var x = 200;
var y = 200;
var gameOver = false;
var direction = 'w';
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));
}
function draw() {
if(gameOver == true){
return; //stops the program
}
if (notBlack(x, y)) {
fill(255);
textAlign(CENTER);
textSize(50);
text("Game Over!", 100, 100);
gameOver = true;
}
strokeWeight(2);
stroke(255);
point(x, y);
if(autoPilot == true){
selfDrive();
}
if(direction == 'd'){
x+=4;
}
if(direction == 'a'){
x-=4;
}
if(direction == 'w'){
y-=4;
}
if(direction == 's'){
y+=4;
}
}
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 == 'r'){
autoPilot = !autoPilot;
}
}
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';
}
}