xxxxxxxxxx
43
let listOfBalls = [];
function setup() {
createCanvas(500, 500);
}
function draw() {
background(100);
for (let i = 0; i < listOfBalls.length; i++) {
listOfBalls[i].drawBall();
listOfBalls[i].updateBall();
}
}
class Ball {
constructor(x, y) {
// constructor
this.ballSize = random(2, 10);
this.speed = random(2, 10);
this.ballColor = color(random(150, 255));
this.posX = x;
this.posY = y;
}
drawBall() {
noStroke();
fill(this.ballColor);
ellipse(this.posX, this.posY, this.ballSize, this.ballSize);
}
updateBall() {
if (this.posY < height) {
this.posY+= this.speed;
}
}
}
function mouseDragged() {
let ball = new Ball(mouseX, mouseY);
listOfBalls.push(ball);
}