/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Diamond class to represent collectible diamonds
var Diamond = Container.expand(function () {
var self = Container.call(this);
var diamondGraphics = self.attachAsset('diamond', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Diamonds do not need to update their position
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var diamonds = [];
var score = 0;
// Create score display
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to spawn a diamond at a random position
function spawnDiamond() {
var diamond = new Diamond();
diamond.x = Math.random() * 2048;
diamond.y = Math.random() * 2732;
diamonds.push(diamond);
game.addChild(diamond);
}
// Function to update score
function updateScore() {
scoreTxt.setText('Score: ' + score);
}
// Handle tap events to collect diamonds
game.down = function (x, y, obj) {
for (var i = diamonds.length - 1; i >= 0; i--) {
if (diamonds[i].containsPoint({
x: x,
y: y
})) {
diamonds[i].destroy();
diamonds.splice(i, 1);
score += 1;
updateScore();
}
}
};
// Game update loop
game.update = function () {
// Spawn a new diamond every 60 frames (1 second)
if (LK.ticks % 60 === 0) {
spawnDiamond();
}
};