This example takes the previous Random Walker example and uses an ArrayList
instead of an array.
ArrayList<RandomWalker> randomWalkers = new ArrayList<RandomWalker>();
void setup() {
size(200, 200);
background(200);
noSmooth();
frameRate(1000);
}
void draw() {
for (RandomWalker rw : randomWalkers) {
rw.step();
rw.draw();
}
}
void mouseDragged() {
randomWalkers.add(new RandomWalker());
}
class RandomWalker {
float x = mouseX;
float y = mouseY;
float r = random(256);
float g = random(256);
float b = random(256);
void step() {
x += random(-1, 1);
y += random(-1, 1);
if (x < 0) {
x = width;
}
if (x > width) {
x = 0;
}
if (y < 0) {
y = height;
}
if (y > height) {
y = 0;
}
}
void draw() {
stroke(r, g, b);
point(x, y);
}
}
See the Pen by Happy Coding (@KevinWorkman) on CodePen.
ArrayList
.This example uses random walkers to create scribble art!
Happy Coding is a community of folks just like you learning about coding.
Do you have a comment or question? Post it here!
Comments are powered by the Happy Coding forum. This page has a corresponding forum post, and replies to that post show up as comments here. Click the button above to go to the forum to post a comment!