xxxxxxxxxx
52
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
let walkers = [];
function setup() {
createCanvas(400, 400);
walkers.push(new Walker());
background(0);
}
function draw() {
for (let i = walkers.length - 1; i >= 0; i--) {
let walker = walkers[i];
walker.step();
walker.show();
if (random(1) < 0.01) {
console.log('split');
let w = new Walker(walker.x, walker.y);
walkers.push(w);
}
}
}
class Walker {
constructor(x, y) {
this.col = color(random(255), random(255), random(255));
this.x = x || width / 2;
this.y = y || height / 2;
}
show() {
stroke(this.col);
point(this.x, this.y);
}
step() {
const choice = floor(random(4));
if (choice == 0) {
this.x++;
} else if (choice == 1) {
this.x--;
} else if (choice == 2) {
this.y++;
} else {
this.y--;
}
}
}