keyPressed doesn't work

So I’m working on a display full of triangles. I want to use keyPressed to get random colors for each triangle after I press a key, but unfortunately it doesn’t work (not even when I put it in the void draw). Does anyone have a solution?

My code:

void setup(){
size(800,800);
background(0,0,0);
smooth();
}


int x = 25;
int y = -150;
int count =0;
int value = 0;

void draw(){
  for (int i=0; i <= 16; i++) {
    y += 100;
    x = 25;
    if(count % 2 != 0){
      for (int j = 0; j <= 16; j++) {
        fill(random(0,250), random(0,250), random(0,250));
        triangle(x-25, y, x, y+50, x+25, y);
        fill(random(0,250), random(0,250), random(0,250));
        triangle(x-25, y, x, y-50, x+25, y);
        x += 50;
      }
      count++;
    }
    x = 0;
    if(count % 2 == 0){
      y += 50;
      for (int j = 0; j <= 16; j++) {
        fill(random(0,250), random(0,250), random(0,250));
        triangle(x-25, y, x, y+50, x+25, y);
        fill(random(0,250), random(0,250), random(0,250));
        triangle(x-25, y, x, y-50, x+25, y);
        x += 50;
      }
      count++;
      y -= 50;
    }
   }
}

void keyPressed(){
  if (keyPressed == true) {
    fill(random(0,250), random(0,250), random(0,250));
  }
}

NOTE: This Processing code belongs to R. Latify, 1st year Industrial Design at TU/e.

1 Like

there are a few problems with your code:

  • after the first frame your x, y, and count variables have already been changed, making your script draw far away from the intended area.
    this can be fixed by resetting your variables at the end of every frame
    you can do this by simply adding the following to the end of your void draw()

    code
    x = 25;
    y = -150;
    count =0;
    value = 0;
    
  • after implementing the above, all triangles are assigned a new color every frame, no matter what our keypressed status is.
    this can be fixed by adding an if statement to your void draw that makes it only draw a new frame when a key is pressed

    implementation
    void draw() {
      if (keyPressed) {
        //all of your original void draw() code
        //my above stated refresh of variables
      }
    }
    

    with this you don’t even need the void keyPressed() so feel free to delete that

1 Like

Thanks! I want to see a display full of triangles when I run the code, and to change colors when I press a key. But at the moment, when I run the code, I first get a black background. How do I solve this?

You could take all the code out of the if (keypressed) statement and put it into its own method.
For example:

void thing() {
   //put old if statement contents here
} 

Then you would call thing(); in both the if (keypressed) statement and void setup()

2 Likes

Thank you so much for the solution!!

1 Like

I’m not sure if you still need this but here’s how i solves it

void setup(){
size(800,800);
background(0,0,0);
smooth();
}

int x = 25;
int y = -150;
int count =0;
int value = 0;

// i added more variables that are colors
// the [1000] means there are 1000 variables
// in red
float[] red = new float[1000];
float[] green = new float[1000];
float[] blue = new float[1000];


int repeat = 0;

void draw(){
// you forgot to reset your variables...
// it happens to the best of us.
x = 25;
y = -150;
count = 0;
value = 0;

for (int i=0; i <= 16; i++) {
y += 100;
x = 25;
if(count % 2 != 0){
for (int j = 0; j <= 16; j++) {

// this repeats over and over resetting the colors
// every time
//fill(random(0,250), random(0,250), random(0,250));


// instead of above we fill with the red green and blue
// variables from above
fill(color(red[j],green[j],blue[j]));
triangle(x-25, y, x, y+50, x+25, y);
//fill(random(0,250), random(0,250), random(0,250));
// the +16 is just for randomness
fill(color(red[j+16],green[j+16],blue[j+16]));
triangle(x-25, y, x, y-50, x+25, y);
x += 50;
}
count++;
}
x = 0;
if(count % 2 == 0){
y += 50;
for (int j = 0; j <= 16; j++) {
//fill(random(0,250), random(0,250), random(0,250));
fill(color(red[j+32],green[j+32],blue[j+32]));
triangle(x-25, y, x, y+50, x+25, y);
//fill(random(0,250), random(0,250), random(0,250));
fill(color(red[j+48],green[j+48],blue[j+48]));
triangle(x-25, y, x, y-50, x+25, y);
x += 50;
}
count++;
y -= 50;
}
}
}

void keyPressed(){

// this fills every triangle the same color
//fill(random(0,250), random(0,250), random(0,250));

repeat = 0;

// will repeat 1000 times assigning all the
// random colors to the variables
while(repeat < 1000){
red[repeat] = random(0,255);
green[repeat] = random(0,255);
blue[repeat] = random(0,255);
repeat = repeat + 1;
}
}
3 Likes