/****
* Classes
****/
// Basket class
var Basket = Container.expand(function () {
var self = Container.call(this);
self.basketGraphics = self.attachAsset('basket', {
anchorX: 0.5,
anchorY: 0.5
});
self.basketGraphics.rotationX = 0.1;
self.basketGraphics.rotationY = 0.1;
self.vx = 2; // Initial horizontal velocity
self.vy = 2; // Initial vertical velocity
});
//<Assets used in the game will automatically appear here>
// Basketball class
var Basketball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('basketball', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Basket movement logic
self.x += self.vx;
self.y += self.vy;
// Reverse direction if basket hits screen edges
if (self.x > 2048 - self.basketGraphics.width / 2 || self.x < self.basketGraphics.width / 2) {
self.vx *= -1;
}
if (self.y > 2732 - self.basketGraphics.height / 2 || self.y < self.basketGraphics.height / 2) {
self.vy *= -1;
}
// Ball physics and movement logic
self.y += self.vy;
self.x += self.vx;
// self.vy += self.gravity;
ballGraphics.rotationX += 0.01;
ballGraphics.rotationY += 0.01;
if (self.y > 2732 - ballGraphics.height / 2) {
self.y = 2732 - ballGraphics.height / 2;
self.vy = 0;
}
if (self.x > 2048 - ballGraphics.width / 2) {
self.x = 2048 - ballGraphics.width / 2;
self.vx = 0;
}
if (self.x < ballGraphics.width / 2) {
self.x = ballGraphics.width / 2;
self.vx = 0;
}
if (self.x > 2048 - ballGraphics.width / 2) {
self.x = 2048 - ballGraphics.width / 2;
self.vx = 0;
}
if (self.x < ballGraphics.width / 2) {
self.x = ballGraphics.width / 2;
self.vx = 0;
}
};
self.vx = 0;
self.vy = 0;
self.gravity = 0;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game elements
var basketball = game.addChild(new Basketball());
basketball.x = 1024;
basketball.y = 2000;
var basket = game.addChild(new Basket());
basket.x = 1024;
basket.y = 500;
// Event listeners for dragging the basketball
// Event listener for 'A' key press to move the basketball
document.addEventListener('keydown', function (event) {
if (event.key === 'A' || event.key === 'a') {
basketball.vx = 5; // Set horizontal velocity to move the ball
}
});
var dragNode = null;
game.down = function (x, y, obj) {
if (basketball.intersects(obj)) {
dragNode = basketball;
dragNode.vx = 0;
dragNode.vy = 0;
}
};
game.move = function (x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
};
game.up = function (x, y, obj) {
if (dragNode) {
dragNode.vx = (x - dragNode.x) / 10;
dragNode.vy = (y - dragNode.y) / 10;
dragNode = null;
}
};
// Update game logic
game.update = function () {
basketball.update();
// Move the basketball when 'A' key is pressed
if (basketball.vx !== 0) {
basketball.x += basketball.vx;
// Reset velocity after moving
basketball.vx = 0;
}
basket.update();
if (basketball.intersects(basket)) {
// Score logic
LK.setScore(LK.getScore() + 1);
basketball.x = 1024;
basketball.y = 2000;
basketball.vx = 0;
basketball.vy = 0;
// basketball.gravity = 0;
}
};
// Display score
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.setText(LK.getScore());
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);