xxxxxxxxxx
68
// THE NATURE OF CODE
// Exercise 0.3
// Create a random walker with dynamic probabilities. For example, can you give it a 50 percent chance of moving in the direction of the mouse?
// by Alexander Odden
function setup() {
class Walker {
constructor() {
this.x = width/2;
this.y = height/2;
}
step() {
let r = random(1);
if (r < 0.5) {
if (this.x < mouseX) {
this.x++;
} else if (this.x > mouseX) {
this.x--;
} // follow mouse x
if(this.y < mouseY) {
this.y++;
} else if (this.y > mouseY) {
this.y--
} // follow mouse y
// otherwise move randomly
} else if (r < .625) {
this.x++;
} else if (r<.75) {
this.x--;
} else if (r<.875) {
this.y++;
} else this.y--
} // step
show() {
stroke(
255 * abs(sin(this.x/70)), //R
255 * abs(cos(this.y/80)),//G
255 * abs(sin(this.y/60)) //B
);
let d = abs(dist(this.x, this.y, mouseX, mouseY));
strokeWeight(sqrt(d)*2);
point(this.x, this.y)
}
}
createCanvas(600, 600);
background(30);
walker = new Walker()
}
function draw() {
walker.step()
walker.show()
}