xxxxxxxxxx
42
function setup() {
createCanvas(400, 400);
for(let b = 0; b< 10; b++){
balls.push(newBall(random(width),random(height),random(-5,5),random(-1,-5)));
}
}
function draw() {
background(220);
for (let ball of balls){
ball.move();
ball.bounce();
ball.display();
}
}
//Define a class called Ball -- the idea of it
class Ball {
constructor(x,y,xspeed,yspeed){
this.x = x
this.y = y
this.xspeed = xspeed
this.yspeed = yspeed
}
move(){
x += xspeed;
y += yspeed;
}
bounce(){
if( x<0 || x>width) {
xspeed *= -1
}
if (y<0 || y>height){
yspeed *= -1
}
}
display(){
ellipse(x,y,50,50)
}
}