p5js/5.醉汉模拟.html

63 lines
1.9 KiB
HTML
Raw Permalink Normal View History

2022-12-13 13:26:05 +00:00
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="/css/style.css" />
<title>醉汉模拟</title>
2022-12-30 10:50:06 +00:00
<script src="/lib/p5/p5.min.js"></script>
<script src="/lib/p5/addons/p5.sound.min.js"></script>
2022-12-13 13:26:05 +00:00
<script src="/lib/utils.js"></script>
<script>
let walker;
function setup() {
createCanvas(600, 600);
angleMode(DEGREES);
2022-12-13 17:09:46 +00:00
blendMode(ADD);
2022-12-13 13:26:05 +00:00
cursor(CROSS);
frameRate(60);
2022-12-13 17:09:46 +00:00
stroke(255, 128, 192, 1);
strokeWeight(4);
2022-12-13 13:26:05 +00:00
background(0);
2022-12-13 17:09:46 +00:00
walker = new Walker(width / 2, height / 2);
2022-12-13 13:26:05 +00:00
}
function draw() {
2022-12-13 17:09:46 +00:00
for (let i = 0; i < 1000; i++) {
2022-12-13 13:26:05 +00:00
walker.move();
walker.show();
}
textInfo("醉汉模拟", "2022-12-10");
}
2022-12-13 17:09:46 +00:00
class Walker extends p5.Vector {
2022-12-13 13:26:05 +00:00
constructor(x, y) {
2022-12-13 17:09:46 +00:00
super(x, y);
2022-12-13 13:26:05 +00:00
}
move() {
2022-12-13 17:09:46 +00:00
this.count++;
2022-12-13 13:26:05 +00:00
let step = p5.Vector.random2D().mult(2);
2022-12-13 17:09:46 +00:00
this.add(step);
if (
this.x < 0 ||
this.x > width ||
this.y < 0 ||
this.y > height
) {
this.x = width / 2;
this.y = height / 2;
2022-12-13 13:26:05 +00:00
}
}
show() {
2022-12-13 17:09:46 +00:00
point(this.x, this.y);
2022-12-13 13:26:05 +00:00
}
}
</script>
</head>
<body>
<main></main>
</body>
</html>