xxxxxxxxxx
144
var gameOver = false;
var x = 20;
var y = 200;
var direction = "d";
var xComp = 380;
var yComp = 200;
var dirComp = "a";
var dirOG = dirComp;
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
}
human();
computer();
if(notBlack(x,y) && notBlack(xComp, yComp)){
background(0, 144, 255);
fill(0);
stroke(0);
rect(5, 5, 390, 390);
fill(255);
textSize(50);
text("It's a tie!", 100, 200);
}
}
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() {
dirOG = dirComp;
if (dirComp == "w" && notBlack(xComp, yComp - 4)) {
while(notBlack(xComp, yComp - 4)){
dirComp = "d";
}
dirComp = dirOG;
}
if (dirComp == "d" && notBlack(xComp + 4, yComp)) {
while(notBlack(xComp + 4, yComp)){
dirComp = "s";
}
dirComp = dirOG;
}
if (dirComp == "s" && notBlack(xComp, yComp + 4)) {
while(notBlack(xComp, yComp+4)){
dirComp = "a";
}
dirComp = dirOG;
}
if (dirComp == "a" && notBlack(xComp - 4, yComp)) {
while(notBlack(xComp - 4, yComp)){
dirComp = "w";
}
dirComp = dirOG;
}
}
function human() {
if (notBlack(x, y)) {
fill(255);
textSize(50);
text("Game Over!\nComputer Wins!", 25, 200);
gameOver = true;
}
strokeWeight(2);
stroke(255);
point(x, y);
if (direction == "d") {
x += 2;
}
if (direction == "a") {
x -= 2;
}
if (direction == "w") {
y -= 2;
}
if (direction == "s") {
y += 2;
}
}
function computer() {
if (notBlack(xComp, yComp)) {
fill(255);
textSize(50);
text("Game Over!\nHuman Wins", 50, 200);
gameOver = true;
}
strokeWeight(2);
stroke(255, 0, 0);
point(xComp, yComp);
selfDrive();
if (dirComp == "d") {
xComp += 2;
}
if (dirComp == "a") {
xComp -= 2;
}
if (dirComp == "w") {
yComp -= 2;
}
if (dirComp == "s") {
yComp += 2;
}
}