xxxxxxxxxx
237
var gameOver = false;
var x = 100;
var y = 200;
var direction = 39;
var autopilot = false;
var h = false;
var compX = 700;
var compY = 200;
var compDir = 37;
var powerX = 100;
var powerY = 300;
function setup() {
createCanvas(800, 800);
background(0, 0, 255);
fill(0);
rect(3, 3, 793, 793);
fill(255,100,0);
rect(350,190,30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
textSize(40);
textAlign(CENTER);
frameRate(30);
strokeWeight(2);
}
function draw() {
if(gameOver === true)
return;
human();
computer();
//text(get(x,y),200,30)
}
function human(){
if (powerX > x-15.2 && powerX < x+15.2 && powerY > y-15.2 && powerY < y+15.2){
fill(0);
noStroke();
ellipse(powerX,powerY,31,31);
powerX = random(30,770);
powerY = random(30,770);
fill(0,255,0);
ellipse(powerX,powerY,30,30);
x = random (30,770);
y = random (30,770);
}
//if(get(x,y)[1]===255 && get(x,y)[0] === 0){
// fill(0);
// stroke(0);
// ellipse(powerX,powerY,30,30);
// powerX = random(30,370);
// powerY = random(30,370);
// stroke(0,255,0);
// fill(0,255,0);
// ellipse(powerX,powerY,30,30);
// stroke(0);
//}
else if (notBlack(x,y) === true) {
stroke(255);
fill(255);
textSize(65);
text("Game Over!\nComputer Wins!", 400, 700);
gameOver = true;
fill(255,0,0);
stroke(255,0,0);
ellipse(x,y,30,30);
}
fill(0,100,0);
rect(0,0,125,50);
textSize(35);
fill(255)
text(frameCount,65,40);
stroke(0,255,0);
point(x, y);
if(autopilot === true)
selfDrive();
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 computer(){
if (powerX > compX-15.1 && powerX < compX+15.1 && powerY > compY-15.1 && powerY < compY+15.1){
fill(0);
noStroke();
ellipse(powerX,powerY,31,31);
powerX = random(30,770);
powerY = random(30,770);
fill(0,255,0);
}
if (notBlack(compX,compY) === true) {
fill(255);
textSize(65);
text("Game Over!\nHuman Wins!", 400, 50);
gameOver = true;
fill(255,0,0);
stroke(255,0,0);
ellipse(compX,compY,30,30);
}
fill(0,100,0);
rect(0,0,125,50);
textSize(35);
fill(255)
text(frameCount,65,40);
stroke(200,0,0);
point(compX, compY);
compDrive();
if(compDir === 37)
compX = compX - 2;
if(compDir === 38)
compY = compY - 2;
if(compDir === 39)
compX = compX + 2;
if(compDir === 40)
compY = compY + 2;
}
function notBlack(x,y) {
var a = get(x, y);
if (a[0] !== 0) return true;
else if (a[1] !== 0 || a[1] === 255) return true;
else if (a[2] !== 0) return true;
return false;
}
function keyPressed(){
if(keyCode >= 37 && keyCode<=40){
if(direction === 37 && keyCode!==39){
direction = keyCode;
}
if(direction === 39 && keyCode!==37){
direction = keyCode;
}
if(direction === 38 && keyCode!==40){
direction = keyCode;
}
if(direction === 40 && keyCode!==38){
direction = keyCode;
}
}
if (key === 'a'){
autopilot= !autopilot;
}
if (key === 'h'){
strokeWeight(2);
fill (0,255,0);
noStroke();
ellipse(powerX,powerY,30,30);
h = true;
}
if (key === 'r'){
x = 200;
y = 200;
compX = 700;
compY = 200;
compDir = 37;
gameOver = false;
direction = int(random(37,41));
background(0,0,225);
fill(0);
stroke(0);
rect(3,3,793,793);
fill(255,100,0);
rect(350,190,30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
rect(random(30,770),random(30,770),30,30);
frameCount = 0;
fill(0,255,0);
powerX = random(30,770);
powerY = random(30,770);
if (h === true){
ellipse(powerX,powerY,30,30);
}
}
}
function compDrive(){
if (compDir === 37 && notBlack(compX-2, compY) === true)
compDir = 38;
if (compDir === 38 && notBlack(compX, compY-2) === true)
compDir = 39;
if (compDir === 39 && notBlack(compX+2, compY) === true)
compDir = 40;
if (compDir === 40 && notBlack(compX, compY+2) === true)
compDir = 37;
}
function selfDrive(){
if(direction===39 && notBlack(x+2,y)===true)
direction = 40;
if(direction===40 && notBlack(x,y+2)===true)
direction = 37;
if(direction===37 && notBlack(x-2,y)===true)
direction = 38;
if(direction===38 && notBlack(x,y-2)===true)
direction=39;
}