xxxxxxxxxx
44
function setup() {
createCanvas(500, 500);
ball1 = new Ball(100, 200, 15);
ball2 = new Ball(300, 200, 30);
}
function draw() {
background(220);
ball1.bounce();
ball1.move();
ball1.position();
ball2.bounce();
ball2.move();
ball2.position();
}
class Ball {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.r = r
this.speedX = random(-10, 10);
this.speedY = random(-10, 10);
}
bounce() {
if (this.x > width || this.x < 0) {
this.speedX = -1 * this.speedX;
}
if (this.y > height || this.y < 0) {
this.speedY = -1 * this.speedY;
}
}
move() {
this.x += this.speedX
this.y += this.speedY
}
position() {
ellipse(this.x, this.y, this.r * 2);
}
}