Files
sunny9898/task8/_ref/noc-examples-processing-master/introduction/RandomWalkPVector/Walker.pde
louiscklaw 5637fbf94f update,
2025-02-01 02:07:58 +08:00

32 lines
564 B
Plaintext

// Daniel Shiffman
// The Nature of Code
// http://natureofcode.com
// A random walker class!
class Walker {
PVector pos;
Walker() {
pos = new PVector(width/2,height/2);
}
void render() {
stroke(0);
fill(175);
rectMode(CENTER);
rect(pos.x,pos.y,40,40);
}
// Randomly move up, down, left, right, or stay in one place
void walk() {
PVector vel = new PVector(random(-2,2),random(-2,2));
pos.add(vel);
// Stay on the screen
pos.x = constrain(pos.x,0,width-1);
pos.y = constrain(pos.y,0,height-1);
}
}