xxxxxxxxxx
269
let angele,eagle, dir;
let things = [];
let predators = [];
let bubbles = [];
let speed = 5;
let overallSpeed;
let score;
let gamestarted, gameover;
let btn = "";
let lastB;
let img = [];
let items = [];
let water;
let points= [];
function setup() {
createCanvas(800, 400);
bg = loadImage("images/bg2.jpg");
for (let i = 0; i < 12; i++) img[i] = loadImage("images/im" + i + ".png");
dir = 1;
gameover = false;
score = 0;
bubbles = [];
lastB = millis();
gamestarted = false;
gameInitialized = false;
overallSpeed = 0;
items = [1, 2, 3, 4, 5, 6, 7];
points= [5,10,5,20,20,50,-50];
water = 100;
hidden = 0;
}
function initializeGame() {
angele = new Turtle();
for (let i = 0; i < 2; i++) {
things[i] = new Thing(random(width), random(height / 2));
}
gameInitialized = true;
}
function draw() {
background(204);
imageMode(CENTER);
image(bg, width / 2, height / 2, 0, height);
image(img[8], width - height / 10, 9*height / 10,80,80);
//buttons
stroke(64);
strokeWeight(5);
// move buttons
fill(150, 175, 40);
ellipseMode(CENTER);
//ellipse(height / 10, height / 2, height / 7, height / 7);
//ellipse(width - height / 10, height / 2, height / 7, height / 7);
noStroke();
fill(90,220,255);
rect(width/3,height/5,(width/3)*(min(water,100))/100,20);
stroke(64);
strokeWeight(2);
noFill();
rect(width/3,height/5,width/3,20);
btn = "";
if (touches.length) {
let d = 50;
if (dist(touches[0].x, touches[0].y, height / 10, height / 2) < d) btn = "left";
else if (dist(touches[0].x, touches[0].y, width - height / 10, height / 2) < d) btn = "right";
//else if (dist(touches[0].x, touches[0].y, height / 10, height - 3 * height / 10) < d) btn = "air";
//else if (dist(touches[0].x, touches[0].y, width - height / 10, height - 3 * height / 10) < d) btn = "air";
else if (gameover && (dist(touches[0].x, touches[0].y, width / 2, 3 * height / 4) < 50)) btn = "restart";
if (btn != "") gamestarted = true;
}
if (btn === "restart") {
setup();
}
if (gamestarted && !gameInitialized) initializeGame();
if (!gameover) {
if (btn === "left") {
angele.move(-1 * speed);
dir = 1.0;
} else if (btn === "right") {
angele.move(speed);
dir = -1.0;
}
}
if (!gameover) {
if (keyCode === LEFT_ARROW) {
angele.move(-1 * speed);
dir = 1.0;
} else if (keyCode === RIGHT_ARROW) {
angele.move(speed);
dir = -1.0;
}
}
if (gameInitialized) {
if ((frameCount % 1200 == 0)&&!gameover) {
append(predators,new Eagle());
hidden=0;
}
for (let i = 0; i < predators.length; i++) {
predators[i].display();
if (predators[i].y<0) predators.splice(i, 1);
}
angele.display(gameover);
for (let i = 0; i < things.length; i++) {
things[i].move();
things[i].display();
}
textSize(30);
textAlign(RIGHT, CENTER);
if (!gameover) {
textSize(30);
stroke(64);
strokeWeight(4);
fill(255, 255, 0);
text("" + score, 19 * width / 20, height / 10);
}
if ((frameCount % 200 == 0)&&!gameover) {
append(things,new Thing(random(width), random(height / 20)));
if (random(1)<0.5) append(things,new Thing(random(width), random(height / 20)));
}
} else {
fill(255);
textAlign(CENTER, CENTER);
textSize(30);
text("Left/Right arrows to move Angèle", width / 2, height / 2);
textSize(20);
showChrs();
}
if (gameover) {
fill(255);
textAlign(CENTER, CENTER);
textSize(30);
text("Score: " + score, width / 2, height / 2);
textSize(20);
text("Restart", width / 2, 3 * height / 4);
showChrs();
return;
}
imageMode(CENTER);
//image(img[10], height / 10, 9*height / 10,80,80);
fill("#ff9900"); strokeWeight(1);
rect(0, 8.4*height / 10,100,25);
rect(2.2*height/10, 9*height / 10,25,100);
if (gameInitialized) {
water = max(0,water-0.05);
if (water==0) gameover=true;
}
}
function showChrs() {
push();
translate(width / 10, height / 10);
scale(-1.0, 1.0);
imageMode(CENTER);
image(img[9], 0, 0);
pop();
push();
translate(9 * width / 10, height / 10);
scale(1.0, 1.0);
imageMode(CENTER);
image(img[0], 0, 0);
pop();
}
class Turtle {
constructor() {
this.x = width / 2;
this.y = height - 30;
this.speed = 1;
}
move(d) {
this.x += d;
this.y = height - 30;
}
display(over) {
push();
translate(this.x, this.y);
scale(dir, 1.0 - 2 * over);
imageMode(CENTER);
image(img[0], 0, 0, 70, 70);
pop();
for (let i = things.length - 1; i >= 0; i--) {
if ((things[i].y > height - 20)) {
if (things[i].item!=7 ) score = score - points[things[i].item-1];
things.splice(i, 1);
} else if (dist(this.x, this.y, things[i].x, things[i].y) < 20) {
if (things[i].item==7) gameover=true;
score = score + points[things[i].item-1];
things.splice(i, 1);
}
}
if (this.x>width-height/10) water+=1;
water = min(100,water);
if (this.x<height/10) hidden=1;
}
}
class Eagle {
constructor() {
this.x = width*(random(1)>0.5);
this.y = 10;
this.speed = 1.5;
}
display() {
this.x += random(-this.speed, this.speed) + 1.5 * ((this.x - angele.x) < 0) - 1.5 * ((this.x - angele.x) > 0) ;
this.y += this.speed-3*hidden;
this.y = min(this.y, height - 20);
push();
translate(this.x, this.y);
scale(-1.0*(this.x<angele.x)+(this.x>=angele.x), 1.0);
imageMode(CENTER);
image(img[9], 0, 0);
pop();
if (dist(this.x, this.y, angele.x, angele.y) < 20) gameover=true;
}
}
class Thing {
constructor() {
this.x = 50 + random(width - 140);
this.y = random(height / 2);
this.speed = 1;
this.item = random(items);
}
move() {
this.x += random(-this.speed, this.speed);
this.y += random(-this.speed, this.speed) + max(0.5,score/500);
this.y = min(this.y, height - 10);
}
display() {
imageMode(CENTER);
if (this.y > height - 25)
image(img[11], this.x, this.y, 60, 60);
else image(img[this.item], this.x, this.y, 60, 60);
}
}
function keyPressed() {
gamestarted=true;
if ((gameover) && (key === 'r')) setup();
}
function keyReleased() {
keyCode = 0;
return false;
}