KeyPressed cannot show a random position to a ellipse

Hello!

I have a question about a sketch. I am trying to draw a ellipse on a random position everytime a key is pressed in this case the key ‘a’. The situation is that it does draw the ellipse in a random position but it will draw it again in the same spot. I would like to know y i can make it draw in another spot evrytime the key ‘a’ is pressed. Here is my code:

tboolean a, q;
int x = int(random(20,2000));
int y = int(random(15, 1000));
void setup(){
size(2500, 1500);
colorMode(HSB, 360, 100,100);
background(0,0,0);
a = false;
q = false;

}

void draw(){
  if(q == true){
    
  for(int i = 0; i < 2501; i = i+250){
  
  strokeWeight(1);
 stroke(0, 0, 100, 5);
line(i, 0, i,1500 );

  for(int z = 0; z <1500; z= z+150){
    strokeWeight(1);
    stroke(0, 0, 100, 1);
  line(0, z, 2500,z );   
        
}
  
  }

  }

if(a == true){
  //int tono = int(random(299, 300));
  stroke(299, 99, 99, 30);
 strokeWeight(int(random(90,100)));
ellipse(x,y, 500, 500);


}
 

}

void keyPressed(){
if(key == 'a'){
  background(0,0,0);
  q = true;
a = true;
}

if(key=='q'){
  background(0,0,0);
  a = false;
q = true;

}

}

Thank you!

Where you are checking if ‘a’ is pressed you’ll want to generate new random numbers for x and y, they’re only getting generated as a global, Hope that helps

  if (key == 'a') {
    background(0, 0, 0);
    q = true;
    a = true;
    x = int(random(20, 2000)); // NEW RANDOM X
    y = int(random(20, 2000)); // NEW RANDOM Y
  }
2 Likes

Add a = false; here ?

Chrisir

Thank you! it did work for drawing ellipses on random positions on the screen, now my question continues for a one single ellipse that changes position every time the key ‘a’ is pressed.

1 Like

I have a solution for my last question. I wrote on the conditional of the keyPressed function

x = int(random(25,2500));
y = int(random(15,1500));

so it will look like this

if(key == 'a'){
  background(0,0,0);
  x = (int(random(20,2500)));
  y = (int(random(15, 1500)));
  q = true;
a = true;
}
1 Like

Thats the solution I gave you lol

1 Like