xxxxxxxxxx
let bubbles = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 15; i++) {
let x = random(width);
let y = height;
let radius = random(20, 50);
let speedY = random(1, 3);
bubbles.push(new Bubble(x, y, radius, speedY));
}
}
function draw() {
background(0);
for (let bubble of bubbles) {
bubble.move();
bubble.display();
}
}
function mousePressed() {
// Check if mouse click is inside any bubble
for (let i = bubbles.length - 1; i >= 0; i--) {
if (bubbles[i].contains(mouseX, mouseY)) {
bubbles.splice(i, 1); // Remove the bubble
}
}
}
class Bubble {
constructor(x, y, radius, speedY) {
this.x = x;
this.y = y;
this.radius = radius;
this.speedY = speedY;
}
contains(px, py) {
let d = dist(px, py, this.x, this.y);
return d < this.radius;
}
move() {
this.y -= this.speedY;
if (this.y < -this.radius) {
this.y = height;
this.x = random(width);
}
}
display() {
stroke(0);
fill(224, 200);
ellipse(this.x, this.y, this.radius * 2);
}
}