/****
* Classes
****/
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var topObstacle = self.attachAsset('obstacleTop', {
anchorX: 0.5,
anchorY: 1,
scaleY: 2732 / 60
});
var bottomObstacle = self.attachAsset('obstacleBottom', {
anchorX: 0.5,
anchorY: 0,
scaleY: 2732 / 60
});
self.gap = 500;
bottomObstacle.y = topObstacle.y + topObstacle.height + self.gap;
self.move = function () {
self.x -= 4;
};
});
// Assets will be automatically generated based on usage in the code.
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
self.velocityY = 0;
self.gravity = 0.25;
self.lift = -8;
self.flap = function () {
self.velocityY = self.lift;
// Add a bounce animation to the player when it jumps
var bounce = 0.5;
var bounceInterval = LK.setInterval(function () {
bounce *= -1;
self.y += bounce;
if (bounce < 0.05 && bounce > -0.05) {
LK.clearInterval(bounceInterval);
}
}, 50);
// Add a scale animation to make the bounce more visible
var scale = 1;
var scaleInterval = LK.setInterval(function () {
scale *= -1;
self.scaleX += scale * 0.1;
self.scaleY += scale * 0.1;
if (scale < 0.05 && scale > -0.05) {
LK.clearInterval(scaleInterval);
self.scaleX = 1;
self.scaleY = 1;
}
}, 50);
// Change the player asset to 'player2' when it jumps
self.attachAsset('player2', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
// Change the player asset back to 'player' after 0.5 seconds
LK.setTimeout(function () {
self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2
});
}, 500);
};
self.update = function () {
self.velocityY += self.gravity;
self.y += self.velocityY * 2;
if (self.y > 2732 || self.y < 0) {
self.y = self.y > 2732 ? 2732 : 0;
self.velocityY = 0;
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background to simulate sky
});
/****
* Game Code
****/
var player = game.addChild(new Player());
player.x = 2048 / 4;
player.y = 2732 / 2;
var obstacles = [];
var score = 0;
var scoreBar = LK.gui.top.addChild(new Container());
scoreBar.width = 2048;
scoreBar.height = 100;
scoreBar.backgroundColor = 0x000000;
var scoreTxt = scoreBar.addChild(new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
}));
scoreTxt.anchor.set(0.5, 0.5);
scoreTxt.x = scoreBar.width / 2;
scoreTxt.y = scoreBar.height / 2;
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048;
obstacle.y = Math.floor(Math.random() * (2732 - obstacle.gap));
obstacles.push(obstacle);
game.addChild(obstacle);
}
LK.on('tick', function () {
player.update();
if (LK.ticks % 300 == 0) {
spawnObstacle();
}
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].move();
if (obstacles[i].x + obstacles[i].width < 0) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
if (obstacles[i].x + obstacles[i].width < player.x && !obstacles[i].scored) {
score += 1;
scoreTxt.setText(score.toString());
obstacles[i].scored = true;
}
if (player.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
});
game.on('down', function () {
player.flap();
});