I need help on my number guessing game

Hi, fellow Coders. I recently got this assignment and I’m halfway through it. It’s a number guessing game.

void setup() {
  size(600,600);
  background(230,230,255);
  


}

int onum = int(random(0,9)); // onum = Originial Number
char gnum = char(onum+48); // Gnum = "Guess Number" and 48 is 0 char
char input; // Made char for user input

{
  print(gnum); // Made seperate part of code so it doesn't constantly rerun and make a new number
  
  
}


void draw() {




String Welcome = "Welcome to the Number Guessing Game!";
String Instruct = "The Computer will generate a random number (0 to 9) and It's your job to guess the number by using the number keys";
textSize(20);
fill(0);
text(Welcome, 50, 100, 280, 100);
text(Instruct, 50, 170, 400, 170);

}


String correct = "Congrats! You guessed correctly!";
String guess_lower = "Guess lower!";
String guess_higher = "Guess higher!";
void keyPressed() {
  input = key; // Putting input char in use, collecting user input now
    textSize(20); 
    fill(0);
if (keyPressed) {

input = key;

if (input == gnum) {
  text(correct, 190, 460, 270, 460);


}
}

if (input > gnum) {
  // Guess Lower
     text(guess_lower, 230, 460, 320 ,460);
String Welcome = "Welcome to the Number Guessing Game!";
String Instruct = "The Computer will generate a random number (0 to 9) and It's your job to guess the number by using the number keys";
textSize(20);
fill(0);
text(Welcome, 50, 100, 280, 100);
text(Instruct, 50, 170, 400, 170);
}
  if (input < gnum) {
  // Guess Higher
    text(guess_higher, 230, 460, 320 ,460);

}

}

The thing I need help on is, making it so when it displays “Guess lower” or “Guess higher” the text disappears. My idea was to draw it over with a rectangle but when I do that it draws over it instantly and I’ve tried messing around with the delay or millis code but no success. If anyone could help me, much appreciated and also If you are willing to help could you please explain it dumb’d down so I can understand. Also, the last part is inviting the user to play and I have an idea on how I would do that, using Y or N to reset the game.

TL;DR

Need help on school assignment;

Need “Guess Higher” or “Guess Lower” text to disappear.

1 Like

please do some house cleaning:

A Sketch has

  • first the variables
  • then setup()
  • then draw()

Remember to hit ctrl-t to get auto-format

here is your result:

String Welcome = "Welcome to the Number Guessing Game!";
String Instruct = "The Computer will generate a random number (0 to 9) and it's your job to guess the number by using the number keys.";

int onum = int(random(0, 9)); // onum = Originial Number
char gnum = char(onum+48); // Gnum = "Guess Number" and 48 is 0 char
char input; // Made char for user input

String correct = "Congrats! You guessed correctly!";
String guess_lower = "Guess lower!";
String guess_higher = "Guess higher!";

// ------------------------------------------------------------------------------------

void setup() {
  size(600, 600);
  background(230, 230, 255);
}

void draw() {
  textSize(20);
  fill(0);
  text(Welcome, 50, 100, 280, 100);
  text(Instruct, 50, 170, 400, 170);
}

// ------------------------------------------------------------------------------------

void keyPressed() {
  input = key; // Putting input char in use, collecting user input now

  if (input == gnum) {
    text(correct, 190, 460, 270, 460);
  }
  if (input > gnum) {
    // Guess Lower
    text(guess_lower, 230, 460, 320, 460);
  }
  if (input < gnum) {
    // Guess Higher
    text(guess_higher, 230, 460, 320, 460);
  }
}
//

As you can see, the text from keyPressed disappears since keyPressed is called only once when a key is pressed, not throughout.

Solution

Therefore we need all text() commands in draw(). How to do this?

So this might be a line you want to have in draw() so it is shown throughout:
text(feedbackText, 190, 460, 270, 460);

Instead of using text() command in keyPressed() you need to set feedbackText to the right text in keyPressed() (and use text() in draw()).
For example feedbackText=guess_lower;
Since feedbackText ist displayed throughout in draw() the screen changes automatically.

Can you do these changes for me?

Remarks

Now it makes sense to use background() at start of draw() !!! The text does look more crisp now.

(Before setup() you need
String feedbackText= ""; to make it a global variable)

Please show your entire code when you made the changes.
Then we can proceed.

(to let the text disappear make a timer and when it’s over set the feedback variable to “”)

Warm regards,

Chrisir

5 Likes

Just got one thing to say, with onum = int(random(0,9)); you will almost never get a nine. The result of random() is a float and the maximum value it can have is 9, but the chances for this are very very low, as there are many digits in floats. You should instead use onum = int(random(0, 10));, because the chances of getting a 10 are EXTREMELY low (in muliple millions of attemps there have been none).

2 Likes

Hello @nanto2016,

Good catch!

You will never get a 10.

The reference will elaborate on this:
https://processing.org/reference/random_.html

:)

1 Like

Hi, Thank you for your help! I’ve done it. After the player guesses correctly, it prompts them for Y to play again and N to exit. After they press Y how would you reset the char? Aka make a new number for them to guess.

String Welcome = "Welcome to the Number Guessing Game!";
String Instruct = "The Computer will generate a random number (0 to 9) and it's your job to guess the number by using the number keys.";

int onum = int(random(0, 9)); // onum = Originial Number
char gnum = char(onum+48); // Gnum = "Guess Number" and 48 is 0 char
char input; // Made char for user input

String correct = "Congrats! You guessed correctly!";
String guess_lower = "Guess lower!";
String guess_higher = "Guess higher!";
String feedbackText = "";
String End = "";
String End1 = "Congrats, you guessed the number correctly!, Press Y to play again or N to exit";

// ------------------------------------------------------------------------------------

void setup() {
  size(600, 600);
  background(230, 230, 255);
  println(gnum);
}

void draw() {
  background(230,230,255);
  textSize(20);
  fill(0);
  text(Welcome, 50, 100, 280, 100);
  text(Instruct, 50, 170, 400, 170);
  text(feedbackText, 190, 460, 270, 460);
  text(End, 150, 350, 270, 350);
}

// ------------------------------------------------------------------------------------

void keyPressed() {
  input = key; // Putting input char in use, collecting user input now


      
      
  if (input == gnum) {
    feedbackText = correct;
    End = End1;
  }
  if (input > gnum) {
    // Guess Lower
    feedbackText = guess_lower;
  }
  if (input < gnum) {
    // Guess Higher
    feedbackText = guess_higher;
  }
              if (key == 'y' || key == 'Y') {
        feedbackText = "";
        End = "";
        
        
              }
        
      
}

Note: The old number should not be able to be guessed anymore.

I think you should mark Chrisir’s reply as Answer, there’s a big green button for that I think.

1 Like

OP has continued the discussion in a new thread