study/p5js/5.醉汉模拟.js

28 lines
478 B
JavaScript
Raw Permalink Normal View History

2022-12-18 07:56:32 +00:00
const width = 400;
const height = 400;
const d = 1;
let x, y;
function setup() {
createCanvas(width, height);
background(255);
x = width / 2;
y = height / 2;
stroke('#AA00FF');
strokeWeight(d);
}
function draw() {
point(x, y);
let r = ceil(random(0, 4));
if (r == 1) y -= d;
else if (r == 2) y += d;
else if (r == 3) x -= d;
else x += d;
}
function mousePressed() {
background(255);
x = width / 2;
y = height / 2;
}