/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Define the FlappyBird class
var FlappyBird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocity = 0;
self.gravity = 0.5;
self.flapStrength = -10;
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.flapStrength;
};
});
// Define the Wall class
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 < -wallGraphics.width / 2) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
var bird = game.addChild(new FlappyBird());
bird.x = 2048 / 4;
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 gapHeight = 400;
var gapPosition = Math.random() * (2732 - gapHeight) + gapHeight / 2;
var topWall = new Wall();
topWall.x = 2048 + 100;
topWall.y = gapPosition - gapHeight / 2 - topWall.height / 2;
walls.push(topWall);
game.addChild(topWall);
var bottomWall = new Wall();
bottomWall.x = 2048 + 100;
bottomWall.y = gapPosition + gapHeight / 2 + bottomWall.height / 2;
walls.push(bottomWall);
game.addChild(bottomWall);
}
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();
if (walls[i].intersects(bird)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
if (walls[i].x < bird.x && !walls[i].scored) {
score++;
scoreTxt.setText(score);
walls[i].scored = true;
}
}
if (LK.ticks % 90 == 0) {
spawnWall();
}
};