/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins 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.movement = 0;
self.gravity = 0.5;
self.update = function () {
self.movement += self.gravity;
self.y += self.movement;
};
self.flap = function () {
self.movement = -8;
};
});
// Pipe class
var Pipe = Container.expand(function () {
var self = Container.call(this);
var topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1.0
});
var bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.0
});
self.speed = 3;
self.setPosition = function (x, y) {
topPipe.x = x;
topPipe.y = y - 150;
bottomPipe.x = x;
bottomPipe.y = y + 150;
};
self.update = function () {
topPipe.x -= self.speed;
bottomPipe.x -= self.speed;
};
self.isOffScreen = function () {
return topPipe.x + topPipe.width < 0;
};
self.intersects = function (bird) {
return bird.intersects(topPipe) || bird.intersects(bottomPipe);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0096FF // Init game with blue background
});
/****
* Game Code
****/
var bird = game.addChild(new Bird());
bird.x = 100;
bird.y = 1366; // Centered vertically
var pipes = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function createPipe() {
var pipe = new Pipe();
var pipeY = Math.random() * (2732 - 300) + 150;
pipe.setPosition(2048, pipeY);
pipes.push(pipe);
game.addChild(pipe);
}
game.down = function (x, y, obj) {
bird.flap();
};
game.update = function () {
bird.update();
if (bird.y <= 0 || bird.y >= 2732) {
LK.showGameOver();
}
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
if (pipe.isOffScreen()) {
pipe.destroy();
pipes.splice(i, 1);
score++;
scoreTxt.setText(score);
}
if (pipe.intersects(bird)) {
LK.showGameOver();
}
}
if (LK.ticks % 90 === 0) {
createPipe();
}
};