Help with resetting the random line function
in
Programming Questions
•
5 months ago
I am having trouble getting the entire program to reset and start on a new clean background any thoughts?
int num = 2000;
int range = 50;
float[] ax = new float[num];
float[] ay = new float[num];
void setup() {
size(800, 600);
background(0);
for(int i = 0; i < num; i++) {
ax[i] = width/2;
ay[i] = height/2;
}
frameRate(30);
}
void draw() {
float r = random(0, 800);
float a = random(0, 250);
float b = random(0, 250);
float c = random(0, 250);
if (keyPressed) {
if (key == 'a') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'b') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'c') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'd') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'e') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'f') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'g') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'h') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'i') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'j') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'k') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'l') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'm') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'n') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'o') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'p') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'q') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'r') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 's') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 't') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'u') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'v') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'w') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'x') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'y') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
if (key == 'z') {
fill(a, b, c);
rect(r, 300, 50, 50);
}
keyPressed=false;
}
// Shift all elements 1 place to the left
for(int i = 1; i < num; i++) {
ax[i-1] = ax[i];
ay[i-1] = ay[i];
}
// Put a new value at the end of the array
ax[num-1] += random(-range, range);
ay[num-1] += random(-range, range);
// Constrain all points to the screen
ax[num-1] = constrain(ax[num-1], 0, width);
ay[num-1] = constrain(ay[num-1], 0, height);
// Draw a line connecting the points
for(int i=1; i<num; i++) {
float val = float(i)/num * 204.0 + 51;
stroke(val);
line(ax[i-1], ay[i-1], ax[i], ay[i]);
}
if (mousePressed == true) {
background(0);
}
}
1