/****
* Classes
****/
// The assets will be automatically created and loaded by the LK engine
// Create a class for the player
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.gravity = 0.5;
self.update = function () {
self.y += self.speed;
self.speed += self.gravity;
if (self.y > 2732) {
self.y = 2732;
self.speed = 0;
}
if (self.y < 0) {
self.y = 0;
self.speed = 0;
}
};
self.flap = function () {
self.speed = -12;
};
});
// Create a class for the walls
var Wall = Container.expand(function () {
var self = Container.call(this);
var wallGraphics = self.attachAsset('wall', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
if (self.x < -100) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
var player = game.addChild(new Player());
player.x = 500;
player.y = 1366;
var walls = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
game.update = function () {
if (LK.ticks % 120 == 0) {
var gapPosition = Math.random() * (2732 - 800) + 400;
var topWall = game.addChild(new Wall());
topWall.y = gapPosition - 600;
topWall.x = 2048;
var bottomWall = game.addChild(new Wall());
bottomWall.y = gapPosition + 600;
bottomWall.x = 2048;
walls.push(topWall);
walls.push(bottomWall);
score++;
scoreTxt.setText(score);
}
for (var i = walls.length - 1; i >= 0; i--) {
if (player.intersects(walls[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
if (walls[i].x < -100) {
walls[i].destroy();
walls.splice(i, 1);
}
}
};
game.down = function (x, y, obj) {
player.flap();
};