xxxxxxxxxx
46
let walker;
function setup() {
createCanvas(400, 400);
background(220);
walker= new Walker();
}
function draw() {
walker.step();
walker.render();
}
class Walker {
construction (){
this.x = width/2;
this.y = height/2;
}
render(){
stroke(0);
point(this.x,this.y);
}
step() {
let choice = floor(random(4));
if (choice == 0) {
this.x++;
} else if (choice == 1) {
this.x--;
} else if (choice == 2) {
this.y++;
} else {
this.y--;
}
//to avoid our walker moving out of our canvas
this.x = constrain(this.x, 0, width - 1);
this.y = constrain(this.y, 0, height - 1);
}
}