/****
* Classes
****/
// Baby class
var Baby = Container.expand(function () {
var self = Container.call(this);
var babyGraphics = self.attachAsset('baby', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Baby update logic if needed
};
});
// Assets will be automatically created and loaded during gameplay
// Coin class
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5; // Move coin downwards
if (self.y > 2732) {
self.destroy();
}
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
self.y += 5; // Move obstacle downwards
if (self.y > 2732) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Init game with sky blue background
});
/****
* Game Code
****/
// Initialize arrays and variables
var coins = [];
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create baby instance
var baby = game.addChild(new Baby());
baby.x = 2048 / 2;
baby.y = 2732 - 200;
// Handle game move events
game.move = function (x, y, obj) {
baby.x = x;
};
// Update game logic
game.update = function () {
// Spawn coins and obstacles
if (LK.ticks % 60 == 0) {
var newCoin = new Coin();
newCoin.x = Math.random() * 2048;
newCoin.y = -50;
coins.push(newCoin);
game.addChild(newCoin);
var newObstacle = new Obstacle();
newObstacle.x = Math.random() * 2048;
newObstacle.y = -50;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
// Update coins and check for collisions
for (var i = coins.length - 1; i >= 0; i--) {
if (coins[i].intersects(baby)) {
score += 1;
scoreTxt.setText(score);
coins[i].destroy();
coins.splice(i, 1);
}
}
// Update obstacles and check for collisions
for (var j = obstacles.length - 1; j >= 0; j--) {
if (obstacles[j].intersects(baby)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
};