xxxxxxxxxx
59
let balls = [];
function setup() {
createCanvas(400, 400);
for (i = 0; i < 100; i++) {
balls[i] = new Ball(random(width), random(height), random(10, 50), random(-5, 5), random(-5, 5), random(50, 200), random(100, 255), random(100, 255))
}
}
function draw() {
background(20);
for (let b in balls) {
balls[b].show();
balls[b].move();
balls[b].bounce();
if (balls[b].isNear(mouseX, mouseY)) {
balls.splice(b, 1)
}
}
}
class Ball {
constructor(_x, _y, _r, _xSpeed, _ySpeed, _colr, _colg, _colb) {
this.x = _x;
this.y = _y;
this.r = _r;
this.xSpeed = _xSpeed;
this.ySpeed = _ySpeed;
this.colr = _colr;
this.colg = _colg;
this.colb = _colb;
}
show() {
noStroke();
fill(this.colr, this.colg, this.colb);
ellipse(this.x, this.y, this.r);
}
move() {
this.x += this.xSpeed;
this.y += this.ySpeed;
}
bounce() {
if (this.x < 0 || this.x > width) {
this.xSpeed *= -1
}
if (this.y < 0 || this.y > height) {
this.ySpeed *= -1
}
}
isNear(mx, my) {
return dist(mx, my, this.x, this.y) < this.r / 2;
}
}