User prompt
add a hopptrigger in the hoop class
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: Cannot read properties of null (reading 'update')' in or related to this line: 'ball.update();' Line Number: 266
Code edit (1 edits merged)
Please save this source code
User prompt
Réinitialise le compteur de rebond à chaque fois que l'utilisateur touche l'écran.
User prompt
compte le nombre de rebonds entre le moment du tir et l'arrivée dans le panier.
Code edit (2 edits merged)
Please save this source code
User prompt
enregistre le score dans l'outil de la plateforme.
User prompt
Utilise LK7SCORE pour le score.
User prompt
ajoute un compteur de temps en haut à droite
User prompt
Remonte la position initiale du ballon de 100 pixels
User prompt
Change la couleur du score en vert foncé
User prompt
Change la couleur du score en vert.
Code edit (1 edits merged)
Please save this source code
User prompt
Réduis la quantité de rebonds lorsque la balle touche le bas de l'écran.
User prompt
Réduis la quantité de rebonds lorsque la balle touche le bas de l'écran.
User prompt
Change la couleur du score en noir
Code edit (2 edits merged)
Please save this source code
User prompt
déplace le code qui gère le fait de marquer un panier dans une fonction globale
User prompt
Quand un palier est marqué, laisse un délai avant de réinitialiser et de bouger le palier.
Code edit (3 edits merged)
Please save this source code
User prompt
La balle doit arrêter de tourner lorsque la vitesse X ou Y est à zéro.
Code edit (5 edits merged)
Please save this source code
User prompt
dans game.on('up' limite la vitesse à maxSpeed
Code edit (1 edits merged)
Please save this source code
/****
* Classes
****/
// Assets will be automatically generated based on usage in the code.
// Ball class for handling the basketball's behavior
var Ball = 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.isMoving = false;
self.launch = function (speedX, speedY) {
self.speedX = speedX;
self.speedY = speedY;
self.isMoving = true;
};
self.update = function () {
if (self.isMoving) {
self.x += self.speedX;
self.y += self.speedY;
// Make the basketball spin
ballGraphics.rotation += 0.1;
// Gradually reduce horizontal speed
// Apply friction to horizontal speed and limit to max speed
self.speedX *= 0.99;
self.speedX = Math.max(Math.min(self.speedX, 15), -15); // Max speed limit
// Enhanced gravity effect with gradual vertical speed reduction and limit to max speed
self.speedY += 1.2;
self.speedY = Math.max(Math.min(self.speedY, 150), -150); // Max speed limit
// Check for boundary collisions, reverse speed, and lose points
if (self.x <= 0 || self.x >= game.width) {
self.speedX *= -1;
//score -= 1; // Deduct points when hitting horizontal boundaries
scoreTxt.setText(score.toString()); // Update score display
}
if (self.y <= 0) {
self.speedY *= -1;
//score -= 1; // Deduct points when hitting the top boundary
scoreTxt.setText(score.toString()); // Update score display
}
// Check if the ball has passed above the hoop and set ballPassedAboveHoop to true
if (self.y < hoop.y - hoop.height / 2 - 100 && !ballPassedAboveHoop) {
ballPassedAboveHoop = true;
}
// Set ballPassedAboveHoop to false when the ball passes under the hoop (+ its height)
if (self.y > hoop.y + hoop.height / 2) {
ballPassedAboveHoop = false;
}
// Reset ball when its speed is very low
if (Math.abs(self.speedX) < 0.5 && Math.abs(self.speedY) < 0.5) {
self.reset();
}
// Allow the ball to fall off the bottom without reversing speed or losing points
// If the ball touches the bottom boundary and score is 0, trigger game over
// Check if the ball is coming from below the hoop and make it bounce
if (self.y >= game.height) {
// || self.intersects(hoop) && self.speedY > 0) {
self.speedY *= -1;
}
}
};
self.reset = function () {
ballPassedAboveHoop = false;
self.x = game.width / 2;
self.y = game.height - 200;
self.speedX = 0;
self.speedY = 0;
self.isMoving = false;
};
});
// Confetti class for creating a confetti effect
var Confetti = Container.expand(function () {
var self = Container.call(this);
var confettiColors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF];
var confettiPieces = [];
// Generate multiple confetti pieces
for (var i = 0; i < 100; i++) {
var color = confettiColors[Math.floor(Math.random() * confettiColors.length)];
var confettiPiece = self.attachAsset('butter', {
anchorX: 0.5,
anchorY: 0.5,
tint: color
});
confettiPiece.x = Math.random() * game.width * 2; // Spread across entire screen width
confettiPiece.y = Math.random() * game.height * 2 - game.height; // Spread across entire screen height
confettiPiece.scaleX = confettiPiece.scaleY = Math.random() * 0.5 + 0.5; // Random scale
confettiPieces.push(confettiPiece);
}
// Animate confetti pieces
self.animate = function () {
confettiPieces.forEach(function (piece) {
piece.y += Math.random() * 20 + 10; // Further increased fall speed
piece.rotation += Math.random() * 0.2 - 0.1; // Random rotation
// Remove piece if it goes off-screen
if (piece.y > game.height + 50) {
piece.destroy();
confettiPieces.splice(confettiPieces.indexOf(piece), 1);
}
});
// Stop animation and destroy confetti container if all pieces are gone
if (confettiPieces.length === 0) {
self.destroy();
}
};
});
// Hoop class for handling the basketball hoop's behavior
var Hoop = Container.expand(function () {
var self = Container.call(this);
var hoopGraphics = self.attachAsset('hoop', {
anchorX: 0.5,
anchorY: 0.5
});
self.setPosition = function (x, y) {
self.x = x;
self.y = y;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({});
/****
* Game Code
****/
// Global variable to track if the ball has passed above the hoop
var ballPassedAboveHoop = false;
// Display the background asset
var background = LK.getAsset('background', {
anchorX: 0.0,
anchorY: 0.0,
x: 0,
y: 0
});
game.addChild(background);
var ball = game.addChild(new Ball());
ball.reset();
var hoop = game.addChild(new Hoop());
hoop.setPosition(game.width / 2, 1024); // Position the hoop at the top center
var score = 0;
var scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var startPosition = null;
game.on('down', function (obj) {
var pos = obj.event.getLocalPosition(game);
startPosition = pos;
});
game.on('up', function (obj) {
if (startPosition) {
var endPosition = obj.event.getLocalPosition(game);
var speedX = (endPosition.x - startPosition.x) * 0.1;
var speedY = (endPosition.y - startPosition.y) * 0.1;
ball.launch(speedX, speedY);
startPosition = null;
}
});
LK.on('tick', function () {
ball.update();
// Check for scoring
if (ball.intersects(hoop)) {
if (ballPassedAboveHoop) {
score += 1;
scoreTxt.setText(score.toString());
ball.reset();
ballPassedAboveHoop = false; // Reset the condition after scoring
// Create and add confetti effect to the game
var confetti = game.addChild(new Confetti());
confetti.x = 0; // Position confetti at the hoop's position
confetti.y = 0;
LK.on('tick', function () {
confetti.animate(); // Animate confetti
});
// Initiate gradual movement of the hoop to a new random position within the game boundaries
var targetX = Math.random() * (game.width - hoop.width) + hoop.width / 2;
var targetY = Math.max(Math.random() * (game.height / 2) + 100, 500); // Ensure hoop's Y position does not go below 500
var moveHoopInterval = LK.setInterval(function () {
hoop.x += (targetX - hoop.x) * 0.05; // Move 5% of the distance per tick
hoop.y += (targetY - hoop.y) * 0.05; // Move 5% of the distance per tick
// Check if the hoop is close enough to the target position to stop
if (Math.abs(hoop.x - targetX) < 1 && Math.abs(hoop.y - targetY) < 1) {
hoop.setPosition(targetX, targetY); // Ensure hoop is exactly at target position
LK.clearInterval(moveHoopInterval); // Stop the interval
}
}, 16); // Run every 16ms (~60FPS)
} else if (ball.speedY > 0) {
ball.speedY *= -1;
}
}
// Reset ball if it goes off-screen
if (ball.y > game.height + 50 || ball.x < -50 || ball.x > game.width + 50) {
ball.reset();
}
});