xxxxxxxxxx
49
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
var walker;
var graphics;
function setup() {
createCanvas(400, 400);
graphics = createGraphics(400, 400);
walker = new Walker();
//graphics.background(127,0,200);
graphics.clear();
}
function draw() {
background(127);
walker.step();
walker.render();
fill(0,255,0);
ellipse(mouseX, mouseY, 24, 24);
image(graphics,0,0);
}
function Walker() {
this.x = width / 2;
this.y = height / 2;
this.render = function() {
graphics.stroke(0);
graphics.point(this.x, this.y);
};
this.step = function() {
var choice = floor(random(4));
if (choice === 0) {
this.x++;
} else if (choice == 1) {
this.x--;
} else if (choice == 2) {
this.y++;
} else {
this.y--;
}
this.x = constrain(this.x, 0, width - 1);
this.y = constrain(this.y, 0, height - 1);
};
}