/**** * Classes ****/ // Background class var Background = Container.expand(function () { var self = Container.call(this); var bgGraphics = self.attachAsset('background', { anchorX: 0.0, anchorY: 0.0 }); self.speed = -2; self.update = function () { self.x += self.speed; if (self.x < -2048) { self.x = 2048; } }; }); //<Assets used in the game will automatically appear here> // Bird class var Bird = Container.expand(function () { var self = Container.call(this); var birdGraphics = self.attachAsset('bird', { anchorX: 0.5, anchorY: 0.5 }); self.gravity = 0.5; self.lift = -10; self.velocity = 0; self.update = function () { self.velocity += self.gravity; self.y += self.velocity; if (self.y > 2732 - birdGraphics.height / 2) { self.y = 2732 - birdGraphics.height / 2; self.velocity = 0; } if (self.y < birdGraphics.height / 2) { self.y = birdGraphics.height / 2; self.velocity = 0; } }; self.flap = function () { self.velocity = self.lift; }; }); // Pipe class var Pipe = Container.expand(function () { var self = Container.call(this); var pipeGraphics = self.attachAsset('pipe', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -pipeGraphics.width / 2) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB, // Init game with sky blue background title: "FLAPPY Aeroplane" }); /**** * Game Code ****/ var background1 = game.addChild(new Background()); background1.x = 0; background1.y = 0; var background2 = game.addChild(new Background()); background2.x = 2048; background2.y = 0; var bird = game.addChild(new Bird()); bird.x = 2048 / 4; bird.y = 2732 / 2; var pipes = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); function addPipe() { var gap = 400; var topPipe = new Pipe(); var bottomPipe = new Pipe(); topPipe.y = Math.random() * (2732 - gap) - gap / 2; bottomPipe.y = topPipe.y + gap + 2048; topPipe.x = bottomPipe.x = 2048; pipes.push(game.addChild(topPipe)); pipes.push(game.addChild(bottomPipe)); } game.down = function (x, y, obj) { bird.flap(); }; game.update = function () { background1.update(); background2.update(); bird.update(); if (LK.ticks % 90 == 0) { addPipe(); } for (var i = pipes.length - 1; i >= 0; i--) { pipes[i].update(); if (pipes[i].intersects(bird)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } if (pipes[i].x < bird.x && !pipes[i].scored) { score++; pipes[i].scored = true; scoreTxt.setText(score); } } };
/****
* Classes
****/
// Background class
var Background = Container.expand(function () {
var self = Container.call(this);
var bgGraphics = self.attachAsset('background', {
anchorX: 0.0,
anchorY: 0.0
});
self.speed = -2;
self.update = function () {
self.x += self.speed;
if (self.x < -2048) {
self.x = 2048;
}
};
});
//<Assets used in the game will automatically appear here>
// Bird class
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.gravity = 0.5;
self.lift = -10;
self.velocity = 0;
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
if (self.y > 2732 - birdGraphics.height / 2) {
self.y = 2732 - birdGraphics.height / 2;
self.velocity = 0;
}
if (self.y < birdGraphics.height / 2) {
self.y = birdGraphics.height / 2;
self.velocity = 0;
}
};
self.flap = function () {
self.velocity = self.lift;
};
});
// Pipe class
var Pipe = Container.expand(function () {
var self = Container.call(this);
var pipeGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
if (self.x < -pipeGraphics.width / 2) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB,
// Init game with sky blue background
title: "FLAPPY Aeroplane"
});
/****
* Game Code
****/
var background1 = game.addChild(new Background());
background1.x = 0;
background1.y = 0;
var background2 = game.addChild(new Background());
background2.x = 2048;
background2.y = 0;
var bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
var pipes = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function addPipe() {
var gap = 400;
var topPipe = new Pipe();
var bottomPipe = new Pipe();
topPipe.y = Math.random() * (2732 - gap) - gap / 2;
bottomPipe.y = topPipe.y + gap + 2048;
topPipe.x = bottomPipe.x = 2048;
pipes.push(game.addChild(topPipe));
pipes.push(game.addChild(bottomPipe));
}
game.down = function (x, y, obj) {
bird.flap();
};
game.update = function () {
background1.update();
background2.update();
bird.update();
if (LK.ticks % 90 == 0) {
addPipe();
}
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
if (pipes[i].intersects(bird)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
if (pipes[i].x < bird.x && !pipes[i].scored) {
score++;
pipes[i].scored = true;
scoreTxt.setText(score);
}
}
};