/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Apple class representing falling apples
var Apple = Container.expand(function () {
var self = Container.call(this);
var appleGraphics = self.attachAsset('apple', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 15; // Increase the speed at which the apple falls
self.update = function () {
self.y += self.speed;
};
});
// Basket class representing the player's basket
var Basket = Container.expand(function () {
var self = Container.call(this);
var basketGraphics = self.attachAsset('basket', {
anchorX: 0.5,
anchorY: 1.0
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var apples = [];
var basket;
var score = 0;
var scoreTxt;
// Initialize game elements
function initGame() {
// Add tree as background
var tree = game.addChild(LK.getAsset('agac', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
scaleX: 2.048,
scaleY: 2.732
}));
basket = game.addChild(new Basket());
basket.x = 2048 / 2;
basket.y = 2500; // Position basket near the bottom of the screen
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
}
// Handle game updates
game.update = function () {
// Update apples
for (var i = apples.length - 1; i >= 0; i--) {
apples[i].update();
if (apples[i].intersects(basket)) {
score++;
// Increase apple fall speed every 10 score increment
if (score % 10 == 0) {
apples[i].speed += 1;
}
scoreTxt.setText('Score: ' + score);
apples[i].destroy();
apples.splice(i, 1);
} else if (apples[i].y > 2732) {
apples[i].destroy();
apples.splice(i, 1);
LK.showGameOver(); // Show game over when an apple is not caught by the basket
}
}
// Spawn new apples
if (LK.ticks % 30 == 0) {
var newApple = new Apple();
newApple.x = Math.random() * 2048;
newApple.y = 0;
apples.push(newApple);
game.addChild(newApple);
}
};
// Handle touch/mouse move events
game.move = function (x, y, obj) {
basket.x = x;
};
// Initialize the game
initGame();