/****
* 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.flapStrength = -10;
self.update = function () {
self.speedY += self.gravity;
self.y += self.speedY;
// Check for collision with top and bottom of the screen
if (self.y < 0) {
self.y = 0;
self.speedY = 0;
} else if (self.y > 2732) {
self.y = 2732;
self.speedY = 0;
}
};
self.flap = function () {
self.speedY = self.flapStrength;
};
});
// Wall class
var Wall = Container.expand(function () {
var self = Container.call(this);
var wallGraphics = self.attachAsset('wall', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = -5;
self.update = function () {
self.x += self.speedX;
// Remove wall if it goes off screen
if (self.x < -wallGraphics.width) {
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 / 2;
bird.y = 2732 / 2;
var walls = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function spawnWall() {
var wall = new Wall();
wall.x = 2048;
wall.y = Math.random() * 2732;
walls.push(wall);
game.addChild(wall);
}
game.down = function (x, y, obj) {
bird.flap();
};
game.update = function () {
bird.update();
for (var i = walls.length - 1; i >= 0; i--) {
walls[i].update();
// Check for collision with bird
if (bird.intersects(walls[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Remove wall if it goes off screen
if (walls[i].x < -walls[i].width) {
walls.splice(i, 1);
}
}
// Increase score and update score text
score += 1;
scoreTxt.setText(score);
// Spawn new wall every 100 ticks
if (LK.ticks % 100 == 0) {
spawnWall();
}
};