xxxxxxxxxx
67
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,i*10));
}
}
function draw() {
background(220);
for(let i=0; i<ball.length; i++){
ball[i].show();
ball[i].move();
ball[i].bounce();
}
// ball0.show();
// ball0.move();
// ball0.bounce();
// ball1.show();
// ball1.move();
// ball1.bounce();
// ball2.show();
// ball2.move();
// ball2.bounce();
// //Use this if statement to help with black hole project
// if(x> width || x < 0){
// xSpeed *= -1;
// }
// if(y> height || y < 0){
// ySpeed *= -1;
// }
}
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;
}
}
}