xxxxxxxxxx
58
var balls = [];
function setup() {
createCanvas(600, 400);
for (i = 0; i < 2; i++) {
let x = random(width);
let y = random(height);
let speedx = random(-4, 4);
let speedy = random(-5, 5);
let r = random(25, 50);
let colR = random(180, 255);
let colG = random(150, 255);
let colB = random(180, 255);
balls[i] = new Ball(x, y, speedx, speedy, r, colR, colG, colB);
}
}
function draw() {
background(20);
for (i = 0; i < balls.length; i++) {
balls[i].move();
balls[i].bounce();
balls[i].display();
}
}
class Ball {
constructor(_x, _y, _speedx, _speedy, _r, _colR, _colG, _colB) {
this.x = _x;
this.y = _y;
this.speedx = _speedx;
this.speedy = _speedy;
this.r = _r;
this.colR = _colR;
this.colG = _colG;
this.colB = _colB;
}
move() {
this.x += this.speedx
this.y += this.speedy
}
bounce() {
if (this.x > width || this.x < 0 ) {
this.speedx = this.speedx * -1;
}
if (this.y > height || this.y < 0){
this.speedy = this.speedy * -1;
}
}
display() {
fill(this.colR, this.colG, this.colB);
ellipse(this.x, this.y, this.r);
}
}