xxxxxxxxxx
42
let bubbles = [];
function setup() {
createCanvas(600, 400);
}
function draw() {
background(0);
for (let i = 0; i < bubbles.length; i++) {
bubbles[i].move();
bubbles[i].bounce();
bubbles[i].show();
bubbles[i].checkCloseBubbles(bubbles);
}
}
function mousePressed() {
let index = findClosestBubble(mouseX, mouseY);
if (index !== -1) {
bubbles.splice(index, 1); // Remove the closest bubble instead of adding
}
}
function keyPressed() {
if (key === ' ') { // Use spacebar to clear all bubbles
bubbles = [];
}
}
function findClosestBubble(x, y) {
let recordDistance = Infinity;
let closestIndex = -1;
for (let i = 0; i < bubbles.length; i++) {
let d = dist(x, y, bubbles[i].x, bubbles[i].y);
if (d < recordDistance) {
recordDistance = d;
closestIndex = i;
}
}
return closestIndex;
}