complete exercise 1.1

This commit is contained in:
Rokas Puzonas 2024-02-07 00:09:12 +02:00
parent 5dfb825315
commit 5ddad8d145
2 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,15 @@
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/p5@1.9.0/lib/p5.js"></script>
<script src="sketch.js"></script>
<style>
body {
background: #212121;
}
</style>
</head>
<body>
<main>
</main>
</body>
</html>

View File

@ -0,0 +1,40 @@
function clamp(x, min_x, max_x) {
return min(max(x, min_x), max_x)
}
class Walker {
constructor(x = 0, y = 0) {
this.pos = createVector(x, y)
}
step() {
let vel = createVector(0, 0)
if (random(1) < 0.5) {
vel.x = clamp(mouseX - this.pos.x, -1, 1)
vel.y = clamp(mouseY - this.pos.y, -1, 1)
} else {
vel.x = floor(random(3)) - 1
vel.y = floor(random(3)) - 1
}
this.pos.add(vel)
}
draw() {
stroke(0)
point(this.pos)
}
}
let walker
function setup() {
createCanvas(640, 240)
background(255)
walker = new Walker(320, 120)
}
function draw() {
walker.step();
walker.draw();
}