xxxxxxxxxx
166
let x = 300;
let y = 100;
let gameOver = false;
let direction = 37;
let otherDirection = 39;
let otherX = 100;
let otherY = 100;
let anotherX = 0;
let anotherY = 0;
function setup() {
createCanvas(400, 400);
background(0,0,255);
fill(0);
rect(10,10,380,380);
textAlign(CENTER);
strokeWeight(2);
textSize(25);
frameRate(30);
}
function draw() {
if (gameOver === true) {
return;
//return just stops the whole program altogether
}
if (notBlack() === true && otherNotBlack() !== true) {
gameOver = true;
fill(255,0,0);
stroke(255,0,0);
ellipse(x,y,30,30);
fill(255);
stroke(0);
text("GAME OVER: PINK WINS", 200,180);
text("TIME PLAYED: " + (frameCount - 1), 200,220);
return;
}
if (otherNotBlack() === true && notBlack() !== true) {
gameOver = true;
fill(255,0,0);
stroke(255,0,0);
ellipse(otherX,otherY,30,30);
fill(255);
stroke(0);
text("GAME OVER: ORANGE WINS", 200,180);
text("TIME PLAYED: " + (frameCount - 1), 200,220);
return;
}
if (notBlack() === true && otherNotBlack() === true) {
gameOver = true;
fill(255,0,0);
stroke(255,0,0);
ellipse(otherX,otherY,30,30);
ellipse(x,y,30,30);
fill(255);
stroke(0);
text("GAME OVER: TIE", 200,180);
text("TIME PLAYED: " + (frameCount - 1), 200,220);
return;
}
stroke(0,0,255);
fill(0,0,255);
rect(160,360,80,40);
stroke(255);
fill(255);
text(frameCount,200,390)
stroke(255,100,0)
point(x,y);
stroke(255,100,255)
fill(255,100,255);
point(otherX,otherY);
//direction = 37;
if (direction === 37) {
x = x - 2;
}
if (direction === 38) {
y = y - 2;
}
if (direction === 39) {
x = x + 2;
}
if (direction === 40) {
y = y + 2;
}
if (otherDirection === 37) {
otherX = otherX - 2;
}
if (otherDirection === 38) {
otherY = otherY - 2;
}
if (otherDirection === 39) {
otherX = otherX + 2;
}
if (otherDirection === 40) {
otherY = otherY + 2;
}
}
function notBlack() {
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 otherNotBlack () {
let b = get(otherX,otherY);
if (b[0] !== 0) {
return true;
} else if (b[1] !== 0) {
return true;
} else if (b[2] !== 0) {
return true;
}
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 === 'w') {
otherDirection = 38;
}
if (key === 'a') {
otherDirection = 37;
}
if (key === 's') {
otherDirection = 40;
}
if (key === 'd') {
otherDirection = 39;
}
if (key === 'r') {
background(0,0,255);
fill(0);
stroke(0);
rect(10,10,380,380);
x = 300;
y = 100;
otherX = 100;
otherY = 100;
gameOver = false;
direction = 37;
otherDirection = 39;
frameCount = 0;
}
}