xxxxxxxxxx
54
let buggers = [];
let numBuggers = 5000;
let explosionRadius = 2;
function setup() {
createCanvas(600, 400);
for (let i = 0; i < numBuggers; i++) {
let x = random(width);
let y = random(height);
buggers.push(new Bugger(x, y));
}
}
function draw() {
background(220);
for (let bugger of buggers) {
bugger.display();
}
}
function mouseClicked() {
for (let i = buggers.length - 1; i >= 0; i--) {
if (dist(mouseX, mouseY, buggers[i].x, buggers[i].y) < explosionRadius) {
triggerChainReaction(buggers[i]);
buggers.splice(i, 1);
}
}
if (buggers.length === 0) {
console.log("Congratulations! You've exterminated all the buggers!");
}
}
function triggerChainReaction(bugger) {
for (let other of buggers) {
if (dist(bugger.x, bugger.y, other.x, other.y) < explosionRadius) {
buggers.splice(buggers.indexOf(other), 1);
triggerChainReaction(other);
}
}
}
class Bugger {
constructor(x, y) {
this.x = x;
this.y = y;
this.diameter = 10;
}
display() {
fill(255, 0, 0);
ellipse(this.x, this.y, this.diameter);
}
}