xxxxxxxxxx
36
// https://discourse.processing.org/t/building-a-walker-in-p5/7336
var walk;
function setup() {
createCanvas(400, 400);
walk = new Walker();
fill(0, 200, 0);
stroke(200,0,0);
background(200,200,0);
}
function draw() {
walk.step();
}
function Walker() {
this.x = 200;
this.y = 200;
this.step = function() {
var choice = int(random(0, 4));
if (choice == 0) this.x++;
else if (choice == 1) this.x--;
else if (choice == 2) this.y++;
else if (choice == 3) this.y--;
if ( this.x < 0 ) this.x = 0;
if ( this.y < 0 ) this.y = 0;
if ( this.x > width ) this.x = width;
if ( this.y > height) this.y = height;
// or use constrain() // https://p5js.org/reference/#/p5/constrain
//print('choice= ' + choice);
ellipse(this.x, this.y, 4, 4);
}
}