xxxxxxxxxx
53
let balls = [];
function setup() {
createCanvas(400, 400);
}
function draw() {
background(20);
for (let b in balls) {
balls[b].show();
balls[b].move();
balls[b].bounce();
}
}
function mousePressed() {
let bb = new Ball(mouseX, mouseY, random(10, 50), random(-5, 5), random(-5, 5), random(50, 200), random(100, 255), random(100, 255));
balls.push(bb);
}
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
}
}
}