/****
* 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.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;
};
});
// 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
});
/****
* Game Code
****/
var bird;
var pipes = [];
var score = 0;
var scoreTxt;
var pipeInterval;
var gameOver = false;
// Initialize bird
bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
// Initialize score text
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to create pipes
function createPipes() {
if (gameOver) return;
var gap = 400;
var pipeHeight = 800;
var pipeY = Math.random() * (2732 - gap - pipeHeight) + pipeHeight / 2;
var topPipe = new Pipe();
topPipe.x = 2048 + topPipe.width / 2;
topPipe.y = pipeY - gap / 2 - pipeHeight;
pipes.push(topPipe);
game.addChild(topPipe);
var bottomPipe = new Pipe();
bottomPipe.x = 2048 + bottomPipe.width / 2;
bottomPipe.y = pipeY + gap / 2 + pipeHeight;
pipes.push(bottomPipe);
game.addChild(bottomPipe);
}
// Function to handle game over
function handleGameOver() {
gameOver = true;
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Game update function
game.update = function () {
if (gameOver) return;
bird.update();
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
if (pipes[i].intersects(bird)) {
handleGameOver();
}
if (pipes[i].x < bird.x && !pipes[i].scored) {
score++;
pipes[i].scored = true;
scoreTxt.setText(score);
}
}
};
// Handle touch events
game.down = function (x, y, obj) {
if (!gameOver) {
bird.flap();
}
};
// Start pipe creation interval
pipeInterval = LK.setInterval(createPipes, 2000);