xxxxxxxxxx
63
let ball = [];
let numberOfBalls = 5;
function setup() {
createCanvas(400, 400);
for(let i=0; i<numberOfBalls; i++){
ball.push(new BouncyBall(i*20+40,i*22+40, i*10+30));
}
}
function draw() {
background(220,50);
for(let i=0; i<ball.length; i++){
ball[i].show();
ball[i].move();
ball[i].bounce();
}
}
function mousePressed(){
ball.reset(mouseX,mouseY);
}
function BouncyBall(X,Y,S){
this.x = X;
this.y = Y;
this.xSpeed = random(-5,5);
this.ySpeed = random(-5,5);
this.size = S;
this.show = function(){
ellipse(this.x,this.y,this.size);
}
this.move = function(){
this.x += this.xSpeed;
this.y += this.ySpeed;
}
this.bounce = function(){
if(this.x > width || this.x<0){
this.xSpeed *= -1;
}
if(this.y > height || this.y<0){
this.ySpeed *= -1;
}
}
this.reset = function(X,Y){
this.x = X;
this.y = Y;
}
}