/* 随机游走 */
var w;
function setup(){
createCanvas(600, 400);
w = new Walker();
background(0)
}
function draw(){
w.step()
w.render()
}
class Walker{
constructor(){
this.x = width / 2;
this.y = height / 2;
}
step(){
var choice =random(1);
if(choice < 0.4){
this.x++
} else if (choice < 0.6){
this.x--
}else if (choice < 0.8){
this.y++
}else{
this.y--
}
// constrain(n,low,high) 应该是压制在canvas内
this.x = constrain(this.x, 0, width-1);
this.y = constrain(this.y, 0, height-1);
}
render(){
stroke(255)
point(this.x, this.y)
// ellipse(this.x, this.y,20)
}
}
console