xxxxxxxxxx
let trees = [];
let age = 1;
function setup() {
createCanvas(windowWidth, windowHeight);
background(255);
}
function draw() {
for(var t of trees){
if(t.age < age){
t.grow();
t.show();
}
}
}
function mousePressed() {
let t = new Tree(mouseX,mouseY, 50, 5);
trees.push(t);
}
class Tree {
constructor(x, y, r, f) {
this.x = x;
this.y = y;
this.f = f;
this.r = r;
this.b = random(0,255);
this.age = 0;
}
grow() {
let choice = random(0,101);
if(choice < 25){
this.x += this.f;
}
else if(choice < 50){
this.x -= this.f;
}
else if(choice < 51){
if(this.r - 1 > 0){
let t = new Tree(this.x,this.y, this.r - 1, this.f);
trees.push(t);
this.age++;
}
else{
this.age++;
}
}
else if(choice < 76){
this.y -= this.f;
}
else if(choice < 101){
this.y += this.f;
}
this.x = constrain(this.x,0, width);
this.y = constrain(this.y,0, height);
}
show() {
noStroke();
fill(0,random(0,255),this.b);
ellipse(this.x,this.y,this.r);
}
}