My text only appears for less than half a second

My text only appears as soon as I press the button of which it’s supposed to run, but it only appears for less than a second.

my code:

int userNumber;
float MyNumber = random (1, 10);
int Guesses = 0;

void setup() {
  size(500, 500);
  background(0);
  textSize(30);
}

void draw () {
  background(0);
  text ("Attempts", 250, 265);
  text ("Choose a number", 40, 60);
  text ("Your number: " +userNumber, 230, 200);
  text (Guesses, 400, 270);
}

void keyPressed() { 
  userNumber = key-48;
  Guesses += 1;
  if (userNumber > int(MyNumber)) {
    text("Wrong number. Smaller please.", 40, 400);    
  } else if (userNumber < int(MyNumber)) {
    text("Wrong number. Bigger please.", 40, 400);
  } else if (userNumber == int(MyNumber)) {
    text("CORRECT!", 40, 400);
    text("Left click to restart", 40, 460);
  }
}

void mouseClicked(){
  userNumber = 0;
  Guesses = 0;
  MyNumber = random (1, 10);
  text ("Choose a number", 40, 60);
}
1 Like

Hi @anklesatrisk
Please format your code above by clicking that pencil, and selecting your code. Then you click the format button.
What I already can see, is that you are drawing text in the mousePressed function. You must draw in the draw() loop with an if block. When you click you set a boolean in the mousePressed function to true, that will trigger an if block in draw().

1 Like