xxxxxxxxxx
229
let GROUND;
let PIPE = [];
let ding, explode, explosionimg, gameoverscreen, sky, pipetop, pipebot,dirt,flabby,textborder;
let playerspeed = 7;
let gamespeed = 5;
let start = false;
let lose = false;
let score = 0;
function setup() {
var cnv = createCanvas(500, 600);
var x = (windowWidth - width) / 2;
var y = (windowHeight - height) / 2;
cnv.position(x, y);
flabby.play();
// buttonFS = new Clickable();
// buttonFS.locate(370,500);
// buttonFS.text = "FULLSCREEN";
// buttonFS.onPress = function(){
// FS();
// }
// buttonFS.onOutside = function(){
// this.color = 'White';
// }
// buttonFS.onHover = function(){
// this.color = 'Green';
// }
buttonRE = new Clickable();
buttonRE.locate(370,550);
buttonRE.text = "RESTART";
buttonRE.onOutside = function(){
this.color = 'White';
}
buttonRE.onPress = function(){
START();
}
buttonRE.onHover = function(){
this.color = 'Green';
}
GROUND = new ground();
START();
}
function preload(){
ding = createAudio('assets/pass.wav');
crash = loadSound('assets/crash.mp3');
explosion = loadImage('assets/explosion.png');
gameoverscreen = loadImage('assets/gameover.png');
sky = loadImage('assets/Sky.jpg');
pipebot = loadImage('assets/pipebot.png');
pipetop = loadImage('assets/pipetop.png');
dirt = loadImage('assets/dirt.png');
flabby = loadImage('assets/flabby.gif');
textborder = loadImage('assets/textborder.png');
}
function draw(){
image(sky, 0, 0, 500, 500);
image(dirt, 0, 500, width, 100);
PLAYER.fly();
PLAYER.fall();
GROUND.collision(PLAYER);
for( let i = 0; i < PIPE.length; i++){
PIPE[i].move();
PIPE[i].collision(PLAYER);
if (PIPE[i].gone()){
ding.volume(0.2);
ding.play();
score++;
gamespeed += 0.01;
PIPE[i] = new enemy(width + 200, gamespeed);
}
}
fill(0);
textAlign(CENTER);
textSize(32);
text(score, 30, 45);
fill(255);
// buttonFS.draw();
buttonRE.draw();
if (start == false) {
noLoop();
image(textborder, 15, 10, 475, 150);
fill(0);
textStyle(BOLD);
textAlign(CENTER);
textSize(20);
text('NOT FLAPPY BIRD - by Minh Nguyen', width /2, 135);
textSize(28);
// text('CLICK FOR FULLSCREEN', width / 2, 50);
text('HOLD SPACE TO FLY!', width / 2, 100);
}
if (lose == true) noLoop();
}
function BIRD(size, speed) {
this.sz = size;
this.x = size / 2;
this.y = 0;
this.speed = speed;
this.g = 0.5;
this.fall = function() {
this.speed += this.g;
this.y += this.speed;
}
this.fly = function() {
if (keyIsDown(32)) {
this.speed = speed;
this.y -= this.speed * 2;
//flap.play();
}
if (mouseIsPressed) {
this.speed = speed;
this.y -= this.speed * 2;
}
if (this.y < this.sz / 2) this.y = this.sz / 2;
image(flabby, this.x, this.y, this.sz, this.sz);
}
}
function ground(x, y, w, h) {
this.hit = false;
this.collision = function(obj) {
this.hit = collideRectCircle(0, 500, width, 100, obj.x, obj.y, obj.sz - 10);
if (this.hit) GAMEOVER();
}
}
function enemy(x, speed) {
this.gap = 200;
this.top = random(0, height - this.gap - 100); //y
this.bot = height - (this.gap + this.top);
this.x = x;
this.w = 100;
this.speed = speed;
this.hit = false;
this.collision = function(obj) {
this.hit = collideRectCircle(this.x, 0, this.w, this.top, obj.x, obj.y, obj.sz - 10);
if (this.hit) GAMEOVER();
this.hit = collideRectCircle(this.x, height - this.bot, this.w, this.bot, obj.x, obj.y, obj.sz - 10);
if (this.hit) GAMEOVER();
}
this.move = function() { //speedup
this.x -= speed;
image(pipetop, this.x, 0, this.w, this.top);
image(pipebot, this.x, height - this.bot, this.w, this.bot);
}
this.gone = function() {
if (this.x < -this.w) {
return true;
} else {
return false;
}
}
}
function START() {
start = false;
lose = false;
score = 0;
flabby.play();
PLAYER = new BIRD(50, playerspeed);
PIPE[0] = new enemy(width, gamespeed);
PIPE[1] = new enemy(width + 400, gamespeed);
} //startup
function GAMEOVER() {
crash.play();
image(explosion, PLAYER.x, PLAYER.y, 75, 75);
image(gameoverscreen, 0, 0, 500, 600);
textAlign(CENTER);
fill(0);
text('HIGH SCORE:' + score, width / 2, 450);
text('PRESS R TO RESTART', width / 2, 500);
lose = true;
noLoop();
}
function keyPressed() {
if (lose == false) {
start = true;
loop();
}
if (key == 'f' || key == 'F') {
FS();
} //fullscreen
if (key == 'r' || key == 'R') {
START();
}
}
function mousePressed() {
if (lose == false) {
start = true;
loop();
}
}
// function FS(){
// let fs = fullscreen();
// fullscreen(!fs);
// }