xxxxxxxxxx
47
let dancers = [];
function setup() {
createCanvas(400, 400);
textAlign(CENTER);
rectMode(CENTER);
for (let i = 0; i < 10; i++) {
dancers.push(new Dance());
}
}
function draw() {
background(255);
text('A room full of bodies,',width/2,height/2);
for (let i = 0; i < dancers.length; i++) {
dancers[i].move();
dancers[i].display();
}
reproduce();
}
function reproduce(){
if (mouseIsPressed){
let newDancer = new Dance();
dancers.push(newDancer);
}
}
class Dance {
constructor() {
this.x = width/2;
this.y = height/2+20;
this.speed = 100;
}
move() {
if(mouseX>this.x && mouseX<this.x+textWidth('dancing') && mouseY>this.y && mouseY<this.y+20){
this.x += random(-this.speed, this.speed);
this.y += random(-this.speed, this.speed);
}
}
display() {
rect(this.x,this.y-3,textWidth('dancing')+15,20);
text('dancing',this.x, this.y);
}
}