xxxxxxxxxx
53
// let balls=[];
let ball1,ball2;
function setup() {
createCanvas(400, 400);
// for(let i=0;i<20;i++){
// balls[i]=new Ball(random(width),random(height),random(100))
// }
ball1=new Ball(width/2,height/2);
ball2=new Ball(width/2-50,height/2);
}
function draw() {
background(0);
// for(let i=0;i<balls.length;i++){
// balls[i].display();
// balls[i].bounce();
ball1.display();
ball2.display();
ball1.bounce();
ball2.bounce();
// }
}
class Ball{
constructor(tempX,tempY,tempD=50,tempXvel=1,tempYvel=2, tempCol=color(255,255,255)){
this.x=tempX;
this.y=tempY;
this.d=tempD;
this.xVel=tempXvel;
this.yVel=tempYvel;
this.col=tempCol;
}
display(){
fill(this.col);
noStroke();
circle(this.x,this.y,this.d);
}
bounce(){
this.x+=this.xVel;
this.y+=this.yVel;
if(this.x<this.d/2||this.x>width-this.d/2){
this.xVel*=-1;
}
if(this.y<this.d/2||this.y>height-this.d/2){
this.yVel*=-1;
}
}
collide(other){
let distance=dist(this.x,this.y,other.x,other.y);
return (distance<this.d);
}
}