/****
* 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 = -15;
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 (game.isStarted) {
game.isStarted = false;
LK.showGameOver();
}
}
if (self.y < birdGraphics.height / 2) {
self.y = birdGraphics.height / 2;
self.velocity = 0;
if (game.isStarted) {
game.isStarted = false;
LK.showGameOver();
}
}
};
self.flap = function () {
self.velocity = self.flapStrength;
LK.getSound('flap').play();
};
});
// Pillar class
var Pillar = Container.expand(function () {
var self = Container.call(this);
var pillarGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.x -= 10;
if (self.x < -pillarGraphics.width / 2) {
self.destroy();
}
if (self.intersects(bird)) {
game.isStarted = false;
LK.showGameOver();
}
};
});
// StartButton class
var StartButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('startButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.interactive = true;
self.buttonMode = true;
self.down = function (x, y, obj) {
startGame();
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0000FF // Init game with blue background
});
/****
* Game Code
****/
var walls = [];
var sky = LK.getAsset('sky', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(sky);
var cloud = LK.getAsset('cloud', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 4
});
game.addChild(cloud);
var sun = LK.getAsset('sun', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 - 150,
y: 150
});
game.addChild(sun);
var startButton = game.addChild(new StartButton());
startButton.x = 2048 / 2;
startButton.y = 2732 / 2;
var bird = new Bird();
bird.x = 2048 / 4;
bird.y = bird.height / 2;
var pipes = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.isStarted = false;
function startGame() {
game.isStarted = true;
score = 0;
scoreTxt.setText(score.toString());
game.addChild(bird);
game.down = function (x, y, obj) {
bird.flap();
};
game.update = function () {
if (game.isStarted) {
bird.update();
if (LK.ticks % 60 == 0) {
// Top pillar
var topPillar = new Pillar();
topPillar.x = 2048;
topPillar.y = Math.random() * (2732 / 3);
walls.push(topPillar);
game.addChild(topPillar);
// Ground pillar
var groundPillar = new Pillar();
groundPillar.x = 2048;
groundPillar.y = 2732 - Math.random() * (2732 / 3);
walls.push(groundPillar);
game.addChild(groundPillar);
// Middle pillar
var middlePillar = new Pillar();
middlePillar.x = 2048;
middlePillar.y = Math.random() * (2732 / 3) + 2732 / 3;
walls.push(middlePillar);
game.addChild(middlePillar);
}
for (var i = walls.length - 1; i >= 0; i--) {
walls[i].update();
if (walls[i].intersects(bird)) {
game.isStarted = false;
LK.showGameOver();
} else if (walls[i].x + walls[i].width / 2 < bird.x - bird.width / 2 && !walls[i].scored) {
score += 2;
scoreTxt.setText(score.toString());
walls[i].scored = true;
}
}
}
};
startButton.destroy();
}
game.update = function () {
bird.update();
scoreTxt.setText(score.toString());
};