xxxxxxxxxx
39
class Box{
constructor(){
this.x = random(width-100);
this.y = random(height-100);
}
draw(_num){
rectMode(CENTER);
rect(this.x, this.y, 100,100);
textAlign(CENTER, CENTER);
text(str(_num), this.x, this.y);
}
}
var boxes = [];
function setup() {
createCanvas(windowWidth, windowHeight);
for( let i = 0; i < 10; i++ ){
boxes.push(new Box());
}
}
function draw() {
background(220);
for( let i = 0; i < boxes.length; i++ ){
boxes[i].draw(i);
}
}
function keyPressed()
{
if( key == 'p' ){ // push
boxes.push(new Box());
}
else if( key == 's' ){ // splice
boxes.splice(random(boxes.length), 1);
}
}