/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define a Ball class for the basketball
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.initialX = 0;
self.initialY = 0;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
self.speedY += 0.1; // Decrease the gravity effect
};
});
// Define a Hoop class for the basketball hoop
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Define a Net class for the net of the basketball hoop
var Net = Container.expand(function () {
var self = Container.call(this);
var netGraphics = self.attachAsset('net', {
anchorX: 0.5,
anchorY: 0.5
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game elements
var ball = new Ball();
ball.x = 1024; // Center horizontally
ball.y = 2400; // Near the bottom of the screen
ball.initialX = ball.x;
ball.initialY = ball.y;
game.addChild(ball);
var hoop = new Hoop();
hoop.x = 1024; // Center horizontally
hoop.y = 500; // Near the top of the screen
game.addChild(hoop);
var net = new Net();
net.x = hoop.x; // Align with the hoop horizontally
net.y = hoop.y + hoop.height / 2; // Position at the bottom of the hoop
game.addChild(net);
var score = 0;
var scoreTxt = new Text2('Score: 0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var isDragging = false;
var startX, startY;
// Handle touch/mouse down event
game.down = function (x, y, obj) {
if (ball.intersects(obj)) {
isDragging = true;
startX = x;
startY = y;
}
};
// Handle touch/mouse move event
game.move = function (x, y, obj) {
if (isDragging) {
ball.x = x;
ball.y = y;
}
};
// Handle touch/mouse up event
game.up = function (x, y, obj) {
if (isDragging) {
isDragging = false;
ball.speedX = (x - startX) / 20;
ball.speedY = (y - startY) / 20;
}
};
// Update game logic
game.update = function () {
ball.update();
// Check if the ball intersects with the hoop
if (ball.intersects(hoop)) {
score += 1;
scoreTxt.setText('Score: ' + score);
ball.x = ball.initialX;
ball.y = ball.initialY;
ball.speedX = 0;
ball.speedY = -30; // Increase the height of the jump
}
// Reset ball if it goes off screen
if (ball.y > 2732) {
ball.x = ball.initialX;
ball.y = ball.initialY;
ball.speedX = 0;
ball.speedY = 0;
}
};