xxxxxxxxxx
58
let bubbles = [];
function setup() {
createCanvas(600, 600);
}
function mouseDragged() {
let s = "blue";
let c = "yellow";
let r = 15;
let b = new Bubble(mouseX, mouseY, r, c, s);
bubbles.push(b);
}
function mousePressed() {
let s = "lightgreen";
let c = "red";
let r = 40;
let b = new Bubble(mouseX, mouseY, r, c, s);
bubbles.push(b);
}
function draw() {
background(200, 20);
for (let bubble of bubbles) {
bubble.move();
bubble.show();
}
for (let i = 0; i < bubbles.length; i++) {
bubbles[i].move();
bubbles[i].show();
}
}
class Bubble {
constructor(x, y, r, c, s) {
this.x = x;
this.y = y;
this.r = r;
this.c = c;
this.s = s;
}
move() {
this.x = this.x + random(-2, 2); //controls x movement
this.y = this.y + random(-2, 2); //controls y movement
}
show() {
strokeWeight(3);
fill(this.c);
stroke(this.s);
ellipse(this.x, this.y, this.r * 2);
}
}