/****
* Classes
****/
// Define the 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();
}
};
});
//<Assets used in the game will automatically appear here>
// Define the Sandal class
var Sandal = Container.expand(function () {
var self = Container.call(this);
var sandalGraphics = self.attachAsset('sandal', {
anchorX: 0.5,
anchorY: 0.5
});
self.gravity = 0.5;
self.lift = -10;
self.velocity = 0;
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
if (self.y > 2732 - sandalGraphics.height / 2) {
self.y = 2732 - sandalGraphics.height / 2;
self.velocity = 0;
}
if (self.y < sandalGraphics.height / 2) {
self.y = sandalGraphics.height / 2;
self.velocity = 0;
}
};
self.flap = function () {
self.velocity = self.lift;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
var sandal = game.addChild(new Sandal());
sandal.x = 2048 / 4;
sandal.y = 2732 / 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);
function spawnPipe() {
var pipe = new Pipe();
pipe.x = 2048 + pipe.width / 2;
pipe.y = Math.random() * (2732 - 400) + 200;
pipes.push(pipe);
game.addChild(pipe);
}
var pipeInterval = LK.setInterval(spawnPipe, 2000);
game.down = function (x, y, obj) {
sandal.flap();
};
game.update = function () {
sandal.update();
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
if (sandal.intersects(pipes[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
if (LK.ticks % 60 == 0) {
score++;
scoreTxt.setText(score);
}
};