xxxxxxxxxx
49
let painterBugs = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 10; i++) {
painterBugs[i] = new PainterBug(random(width), random(height), random(10, 30));
}
}
function draw() {
for (let bug of painterBugs) {
bug.move();
bug.display();
}
}
class PainterBug {
constructor(startX, startY, size) {
this.x = startX;
this.y = startY;
this.size = size;
this.xSpeed = random(1, 3);
this.ySpeed = random(1, 3);
this.color = color(random(255), random(255), random(255));
}
display() {
fill(this.color);
noStroke();
ellipse(this.x, this.y, this.size, this.size);
}
move() {
this.x += this.xSpeed;
this.y += this.ySpeed;
if (this.x > width || this.x < 0) {
this.xSpeed *= -1;
}
if (this.y > height || this.y < 0) {
this.ySpeed *= -1;
}
}
}