xxxxxxxxxx
47
let movers = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 5; i++) {
movers.push(new Mover(random(width), random(height), random(-4, 4), random(-4, 4)));
}
}
function draw() {
background(220);
for (let mover of movers) {
mover.update();
mover.checkEdges();
mover.display();
}
}
class Mover {
constructor(x, y, dx, dy) {
this.position = createVector(x, y);
this.velocity = createVector(dx, dy);
}
update() {
this.position.add(this.velocity);
}
checkEdges() {
if (this.position.y > height || this.position.y < 0) {
this.velocity.y *= -1;
}
if (this.position.x > width || this.position.x < 0) {
this.velocity.x *= -1;
}
}
display() {
ellipse(this.position.x, this.position.y, 20, 20);
}
}