xxxxxxxxxx
45
let WID = 400;
let HIG = 400;
let balls = [];
let n = 60;
let i;
function setup() {
createCanvas(WID,HIG);
for (i=0; i<n;i++){
balls[i] = new ball(i * 3);
}
print(width);
}
function draw() {
background(220);
for (i=0; i<balls.length;i++){
balls[i].show();
balls[i].move();
}
}
class ball {
constructor(shade){
this.x = WID/2;
this.y = WID/2;
this.d = 23;
this.s = shade;
}
move() {
this.x = this.x + random(-2,2);
this.y = this.y + random(-2,2);
if (this.x > WID - 2 * this.d) {this.x = WID - 2 * this.d;}
if (this.x < 2 * this.d) {this.x = 2 * this.d;}
if (this.y > HIG -2 * this.d) {this.y = HIG - 2 * this.d;}
if (this.y < 2 * this.d) {this.x = 2 * this.d;}
}
show() {
stroke(0);
strokeWeight(2);
fill(this.s);
ellipse(this.x, this.y, this.d);
}
}