/****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Basketball class representing the ball in the game
var Basketball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('basketball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
// Apply gravity
self.speedY += 0.5;
// Check for out of bounds
if (self.y > 2732) {
self.reset();
}
};
self.reset = function () {
self.x = 2048 / 2;
self.y = 2732 - 200;
self.speedX = 0;
self.speedY = 0;
};
});
// Hoop class representing the basketball hoop
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = 2048 / 2;
self.y = 300;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var basketball = game.addChild(new Basketball());
basketball.reset();
var hoop = game.addChild(new Hoop());
var score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
game.down = function (x, y, obj) {
var localPos = game.toLocal(obj.global);
basketball.speedX = (localPos.x - basketball.x) / 10;
basketball.speedY = (localPos.y - basketball.y) / 10;
};
game.update = function () {
basketball.update();
if (basketball.intersects(hoop)) {
score += 1;
scoreTxt.setText('Score:' + score);
basketball.reset();
// Placeholder for setting a limit on the score, if needed
// Example: if (score > 100) { score = 10; }
}
};