xxxxxxxxxx
117
let basket;
let fallingObjects = [];
let stackedObjects = [];
let score = 0;
const targetScore = 15; // You can change this value to set the winning condition
const objectInterval = 1000;
const basketSpeed = 5;
const objectSpeedMin = 2;
const objectSpeedMax = 5;
let stackX = 20; // Initial X position for stacking balls
function setup() {
createCanvas(400, 400);
basket = new Basket();
setInterval(() => {
fallingObjects.push(new FallingObject());
}, objectInterval);
}
function draw() {
background(220);
basket.display();
basket.move();
for (let i = fallingObjects.length - 1; i >= 0; i--) {
fallingObjects[i].display();
fallingObjects[i].fall();
if (fallingObjects[i].y > height - fallingObjects[i].radius / 2) {
stackedObjects.push(fallingObjects[i]);
stackX += fallingObjects[i].radius * 1.5; // Adjust the spacing between stacked objects as needed
fallingObjects.splice(i, 1);
}
if (fallingObjects[i].hits(basket)) {
fallingObjects.splice(i, 1);
score++;
}
}
for (let i = 0; i < stackedObjects.length; i++) {
stackedObjects[i].displayStacked(height - 10, stackX - (i * 1.5 * stackedObjects[i].radius)); // Adjust the spacing between stacked objects as needed
}
fill(0);
textSize(24);
text(`Score: ${score}`, 10, 30);
if (score >= targetScore) {
textSize(32);
text("You won!", width / 2 - 50, height / 2);
noLoop();
} else if (stackedObjects.length >= width / 20) {
textSize(32);
text("Game Over!", width / 2 - 70, height / 2);
noLoop();
}
}
class Basket {
constructor() {
this.x = width / 2;
this.y = height - 60;
this.width = 60;
this.height = 10;
}
display() {
fill(255, 0, 0);
rect(this.x - this.width / 2, this.y - this.height / 2, this.width, this.height);
}
move() {
if (keyIsDown(LEFT_ARROW) && this.x > this.width / 2) {
this.x -= basketSpeed;
}
if (keyIsDown(RIGHT_ARROW) && this.x < width - this.width / 2) {
this.x += basketSpeed;
}
}
}
class FallingObject {
constructor() {
this.x = random(width);
this.y = 0;
this.radius = random(20, 40);
this.speed = random(objectSpeedMin, objectSpeedMax);
}
display() {
fill(0, 0, 255);
ellipse(this.x, this.y, this.radius);
}
fall() {
this.y += this.speed;
}
hits(basket) {
let halfBasketWidth = basket.width / 2;
if (this.x > basket.x - halfBasketWidth && this.x < basket.x + halfBasketWidth) {
if (this.y > basket.y - basket.height / 2) {
return true;
}
}
return false;
}
displayStacked(y, x) {
fill(0, 0, 255);
ellipse(x, y, this.radius);
}
}