xxxxxxxxxx
45
class Walker {
constructor(x, y, color) {
this.x = x;
this.y = y;
this.color = color;
this.t = random(100); // Initialize time variable for Perlin noise
}
// Function to update the walker's position
update() {
let stepX = map(noise(this.t), 0, 1, -5, 5);
let stepY = map(noise(this.t + 10), 0, 1, -5, 5);
this.x += stepX;
this.y += stepY;
this.x = constrain(this.x, 0, width);
this.y = constrain(this.y, 0, height);
this.t += 0.02; // Increment the time variable
}
// Function to display the walker
display() {
fill(this.color);
ellipse(this.x, this.y, 20, 20);
}
}
let walkers = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 2; i++) {
let x = random(width);
let y = random(height);
let col = color(random(255), random(255), random(255));
walkers.push(new Walker(x, y, col));
}
background(220);
}
function draw() {
for (let i = 0; i < walkers.length; i++) {
walkers[i].update();
walkers[i].display();
}
}