xxxxxxxxxx
122
let birdImage;
let Background;
let y = 100;
let pipes = [];
let cheerSound;
let booSound;
score = -5;
function preload() {
birdImage = loadImage('Bird.png');
Background = loadImage('Background.png');
cheerSound = loadSound('Cheer.mp3');
booSound = loadSound('Boo.mp3');
}
function setup() {
createCanvas(800, 600);
pipes.push(new Pipe());
bird = new Bird();
booSound.amp(0.3);
}
function Bird() {
this.y = 135;
this.x = 105;
this.show = function() {
push();
noFill();
noStroke();
ellipse(this.x, this.y, 43);
pop();
}
this.fly = function() {
this.y = this.y - 50;
}
this.fall = function() {
this.y = this.y + 2.5;
}
}
function Pipe() {
this.top = random(height / 2);
this.space = 150;
this.bottom = this.top + this.space;
this.x = width;
this.speed = 3;
this.gameover = false;
this.show = function() {
fill(0, 189, 5);
rect(this.x, 0, 50, this.top);
rect(this.x, this.bottom, 50, 500)
}
this.update = function() {
this.x = this.x - this.speed;
}
this.losing = function(Bird) {
if (this.x < 100 && this.x > 50) {
if (y < this.top || y > this.bottom) {
return true;
}
}
return false;
}
this.gettingScore = function(Bird) {
if (this.x < 100 && this.x > 50) {
if (y > this.top && y < this.bottom) {
return true;
}
}
return false;
}
}
this.out = function() {
if (this.x < 0) {
return true;
} else {
return false;
}
}
function draw() {
background(220);
image(Background, 0, 0);
Background.resize(800, 600);
image(birdImage, 50, y);
birdImage.resize(112, 80);
y = y + 2.5;
textSize(80);
if (score <= 0) {
cheerSound.stop();
}
if (frameCount % 80 == 0) {
pipes.push(new Pipe());
}
for (let i = 0; i < pipes.length; i++ ) {
pipes[i].show();
pipes[i].update();
if (pipes[i].losing(Bird)) {
cheerSound.stop();
booSound.play();
score = 0;
} else if (frameCount % 80 == 0) {
score = score + 1;
booSound.stop();
cheerSound.play();
}
}
bird.show();
bird.fall();
}
function keyPressed() {
y = y - 50;
bird.fly();
}