Code edit (2 edits merged)
Please save this source code
User prompt
met ballPassedAboveHoop à vrai lorsque le ballon passe au dessus du panier
Code edit (3 edits merged)
Please save this source code
User prompt
On ne doit marquer des points que si on passe par au-dessus du panier.
User prompt
Quand le ballon arrive par en dessous du panier, il doit rebondir.
User prompt
Pour marquer, il faut que le ballon soit d'abord passé au-dessus du panier.
User prompt
Ajoute une variable globale qui indique que le ballon est passé au-dessus du panier.
User prompt
Réinitialise le ballon lorsque sa vitesse est très faible.
User prompt
Réinitialise le ballon lorsque sa vitesse est faible.
User prompt
Retire le Game Over
User prompt
Le ballon doit tourner sur lui-même lorsqu'il est lancé.
User prompt
Le Y du palier ne doit pas passer en dessous de 500.
User prompt
Le Y du panier ne doit pas passer en dessous de 16 ans.
User prompt
Après un lancé, la vitesse du ballon doit se réduire au fur et à mesure.
User prompt
Plus vite !
User prompt
J'ai tombé les confettis plus vite.
User prompt
Les confettis doivent être affichés sur toute la hauteur de l'écran aussi.
Code edit (1 edits merged)
Please save this source code
User prompt
Les confettis doivent être affichés sur toute la largeur de l'écran.
User prompt
Quand on marque un panier, ça fait des confettis.
User prompt
Le réserve des cours, quoi faire ? Cache le joueur !
User prompt
Casse le joueur !
User prompt
Améliore le jeu.
Code edit (1 edits merged)
Please save this source code
User prompt
Cache le joueur !
/****
* 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;
self.speedY += 1.2; // Enhanced gravity effect
// 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
}
// 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
if (self.y >= game.height) {
if (score <= 0) {
LK.showGameOver();
} else {
self.speedY *= -1;
}
}
}
};
self.reset = function () {
self.x = game.width / 2;
self.y = game.height - 200;
self.speedX = 0;
self.speedY = 0;
self.isMoving = false;
};
});
// 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;
};
});
// Player class for handling the player's behavior
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.0,
// Anchor to the left
anchorY: 1.0 // Anchor to the bottom
});
self.setPosition(0, game.height); // Position at bottom left
self.setPosition = function (x, y) {
self.x = x;
self.y = y;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({});
/****
* Game Code
****/
// Display the background asset
var background = LK.getAsset('background', {
anchorX: 0.0,
anchorY: 0.0,
x: 0,
y: 0
});
game.addChild(background);
// var player = game.addChild(new Player());
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)) {
score += 1;
scoreTxt.setText(score.toString());
ball.reset();
// 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.random() * (game.height / 2) + 100; // Keep the hoop in the upper half of the screen
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)
}
// Reset ball if it goes off-screen
if (ball.y > game.height + 50 || ball.x < -50 || ball.x > game.width + 50) {
ball.reset();
}
});