Game with typing random characters

Urgent please help with assignment this class is making my gpa suffer!

My assignment is to create a program that generates a random char value from 97 to 122 and then stores the number in a char variable. The screen will display that character to the screen for 100 draw calls. We will use this letter and others to play a game.

Rules for Practice Game:

  • If the user presses the displayed key during the time frame they will get 1 point.
  • If the user does not press the key or presses another key they will get one skull and the letter will change to a new random letter for 100 draw calls.
  • The game ends when the user collects 3 skulls and program will stop generating new letters and updating the score.
  • ‘?’ Will restart the game.

So far I have the char to generate random values. I do not understand how create everything after that step. Any help is appreciated.

char c=(char)int(random(97,122));
void setup()
{
  size(400,400);
}


void draw()
{ 
  background(0);
  
  PFont t=createFont("arial",70);
  PFont f=createFont("arial",15);
  PImage skull=loadImage("Skull.png");

  textFont(t);
  drawText();
  
  textFont(f);
  text("Skulls:",30,20);
  text("Score:",300,20);
  image(skull,90,10);
}

void drawText()
{
   text(c,200,200);
   c++;
}

STOP.

Before we do anything else, there is something we need to fix IMMEDIATELY.

Look at this code:

void setup() {
  size(400,400);
}

void draw() { 
  background(0);  
  PImage skull=loadImage("Skull.png");
}

This code is VERY BAD. This is VERY BAD because you are RELOADING that image 60 times EVERY SINGLE SECOND. You do not need to RELOAD it at all. You only need to load it ONCE.

To do something only ONCE, you put it in setup(). But because we want to be able to use this image in draw too, we need to make the PImage variable global.

This code is MUCH BETTER:

PImage skull;

void setup() {
  size(400,400);
  skull=loadImage("Skull.png");
}

void draw() { 
  background(0);  
}

Do you see the difference? LOOK AT THE ABOVE CODE AGAIN UNTIL YOU SEE THE DIFFERENCES. UNDERSTAND THAT YOU ONLY NEED TO LOAD THINGS ONCE, IN SETUP().

You should also make this change for your fonts. Declare the variables for your fonts globally, create them only once in setup(), and then use them as many times as you need in draw().

Now go back to your own code and FIX THIS FIRST. Once you have fixed this, repost it and then we can talk about getting your assignment working.

2 Likes

@ari_y in addition to what @TfGuy44 has said, I don’t think c++ is what you really want.
Let’s say c is initialized as 120, after two loops it’s 122, and the next one will be 123 which is beyond your wanted range.
Also the sketch runs at 60Hz standard, which means the letters will be displayed for 0,0166 seconds, or 16 milliseconds. I don’t think anyone can type that fast :stuck_out_tongue: so make sure that changes.
Lastly, create variables that store the score and the amount of times a skull must be displayed and create if() statements for when a user types a correct or incorrect character. Of course I would recommend the void keyPressed() {} function for any kind of keystrokes.

1 Like

I suggest working on things one step at a time, and asking for help one step at a time. Your next step was probably to display those random values on the screen. Were you able to do that?

If so, when you ask for help, describe the step you are currently stuck on, what you are trying to do, and what isn’t working / what you don’t understand.