xxxxxxxxxx
67
// let xPos,yPos,xSpeed,ySpeed;
// function setup() {
// createCanvas(400, 400);
// xPos=width/2;
// yPos=random(100,300);
// xSpeed=4;
// ySpeed=7;
// }
// function draw(){
// background(240,0,0)
// xPos+=xSpeed
// yPos+=ySpeed
// if(xPos<=15 || yPos>=width-15){
// xSpeed=-xSpeed;
// }
// if(yPos<=15 || yPos>=width-15){
// ySpeed=-ySpeed;
// }
// ellipse(xPos,yPos,30)
// }
let ball=[];
function setup(){
createCanvas(400,400)
for(let i=0;i<10;i++){
ball[i]=new BouncingBall(i,4);
}
}
function draw(){
background(240)
for(let i=0;i<10;i++){
ball[i].move();
ball[i].checkForCollisions(15);
ball[i].draw();
}
}
class BouncingBall {
constructor(x,y) {
this.xPos = width / 2;
this.yPos=random(100,300);
this.xSpeed=x;
this.ySpeed=y;
}
move(){
this.xPos+=this.xSpeed;
this.yPos+=this.ySpeed;
}
checkForCollisions(){
if(this.xPos<=0 || this.xPos>=width){
this.xSpeed=-this.xSpeed;
}
if(this.yPos<=0 || this.yPos>=height){
this.ySpeed=-this.ySpeed;
}
}
draw(){
circle(this.xPos,this.yPos,30);
}
}