User prompt
if the ball hits the bottom of the screen without scoring any points, go to game over
User prompt
now, the ball needs to track the player's cursor. the ball however can only move horizontally, the y position always remains the same
User prompt
all subsequent balls need to remain stuck until the player taps to release, not just the first one
User prompt
only release the ball when the player taps the screen, before then the ball has to remain stuck
Initial prompt
Drop the Basketball in the hoop
/**** * Classes ****/ // Assets will be automatically created based on usage in the code. // Ball class var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.speedY = 0; self.gravity = 0.5; self.bounce = -0.7; self.update = function () { self.speedY += self.gravity; self.y += self.speedY; if (self.y > 2732 - self.height / 2) { self.speedY *= self.bounce; self.y = 2732 - self.height / 2; } }; }); // Hoop class var Hoop = Container.expand(function () { var self = Container.call(this); var hoopGraphics = self.attachAsset('hoop', { anchorX: 0.5, anchorY: 0.5 }); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Light blue background to simulate sky }); /**** * Game Code ****/ var ball; var hoop; var score = 0; var scoreTxt; function initGame() { ball = game.addChild(new Ball()); ball.x = 1024; // Center horizontally ball.y = 200; // Starting height hoop = game.addChild(new Hoop()); hoop.x = 1024; // Center horizontally hoop.y = 2732 - 300; // Position from bottom scoreTxt = new Text2(score.toString(), { size: 150, fill: "#ffffff" }); LK.gui.top.addChild(scoreTxt); game.on('down', function (obj) { var pos = obj.event.getLocalPosition(game); ball.x = pos.x; ball.y = pos.y; ball.speedY = 0; // Reset speed }); LK.on('tick', function () { ball.update(); checkScore(); }); } function checkScore() { if (ball.intersects(hoop)) { score += 1; scoreTxt.setText(score.toString()); // Reset ball position ball.x = 1024; ball.y = 200; ball.speedY = 0; } } initGame();
/****
* Classes
****/
// Assets will be automatically created based on usage in the code.
// Ball class
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedY = 0;
self.gravity = 0.5;
self.bounce = -0.7;
self.update = function () {
self.speedY += self.gravity;
self.y += self.speedY;
if (self.y > 2732 - self.height / 2) {
self.speedY *= self.bounce;
self.y = 2732 - self.height / 2;
}
};
});
// Hoop class
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background to simulate sky
});
/****
* Game Code
****/
var ball;
var hoop;
var score = 0;
var scoreTxt;
function initGame() {
ball = game.addChild(new Ball());
ball.x = 1024; // Center horizontally
ball.y = 200; // Starting height
hoop = game.addChild(new Hoop());
hoop.x = 1024; // Center horizontally
hoop.y = 2732 - 300; // Position from bottom
scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
ball.x = pos.x;
ball.y = pos.y;
ball.speedY = 0; // Reset speed
});
LK.on('tick', function () {
ball.update();
checkScore();
});
}
function checkScore() {
if (ball.intersects(hoop)) {
score += 1;
scoreTxt.setText(score.toString());
// Reset ball position
ball.x = 1024;
ball.y = 200;
ball.speedY = 0;
}
}
initGame();