/****
* Classes
****/
//<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.speedY = 0;
self.gravity = 0.5;
self.lift = -10;
self.update = function () {
self.speedY += self.gravity;
self.y += self.speedY;
if (self.y > 2732 - birdGraphics.height / 2) {
self.y = 2732 - birdGraphics.height / 2;
self.speedY = 0;
}
if (self.y < birdGraphics.height / 2) {
self.y = birdGraphics.height / 2;
self.speedY = 0;
}
};
self.flap = function () {
self.speedY = self.lift;
};
});
// Spike class
var Spike = Container.expand(function () {
var self = Container.call(this);
var spikeGraphics = self.attachAsset('spike', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = -5;
self.update = function () {
self.x += self.speedX;
if (self.x < -spikeGraphics.width / 2) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Init game with sky blue background
});
/****
* Game Code
****/
var bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
var spikes = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function spawnSpike() {
var spike = new Spike();
spike.x = 2048 + spike.width / 2;
spike.y = Math.random() * (2732 - spike.height) + spike.height / 2;
spikes.push(spike);
game.addChild(spike);
}
function handleMove(x, y, obj) {
bird.flap();
}
game.down = handleMove;
game.update = function () {
bird.update();
for (var i = spikes.length - 1; i >= 0; i--) {
spikes[i].update();
if (bird.intersects(spikes[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
if (LK.ticks % 90 == 0) {
spawnSpike();
}
scoreTxt.setText(LK.getScore());
};
LK.setInterval(function () {
LK.setScore(LK.getScore() + 1);
}, 1000);