Easy typing program

Im trying to create a program where a random letter appears on the screen and the user has to type it to gain 1 point and if they type it within a time frame they get bonus points (I haven’t wrote the point system yet because my current code in not working). The letters in my code fill up the canvas constantly instead of only appearing once.

void setup(){
  size(500,500);
}
void draw(){
  background(255); 
  String[] letter = { "A", "B", "C", "D" };
  int index = int(random(letter.length));
  textSize(25);
  fill(100,0,0);
  text((letter[index]),200,200);
}
1 Like

Good day helpme123, welcome to the forum.

Could you please edit your post and format the code? You can press the button that looks like < /> (Preformatted text) and place your Processing code in there. The outcome should look similar to this:

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

void draw() {
  // rest of your code
}

You have to take in account that void draw() works like a constant loop. Once your sketch runs, all the code inside of it gets repeated around 60 times a second (unless specified otherwise or if the sketch is too heavy to run it smoothly). That being said, do you understand why the letter keeps switching constantly?

I think it will be a good exercise for you to figure out the difference between local and global variables. If you make int index a global variable, you could –for now as a first step– use mouseReleased to switch the displayed letter.

1 Like

I often use these as a simple “timer” to control when I update data on screen:

I keep frameRate at default (60 fps).

Consider what this does:

  if (frameCount%60 == 0) 
    {
    // Update something
    }
   // Display something

This is also available to you:

:slight_smile:

1 Like

I wouldn’t use frameRate

In that scenario the Sketch would just wait until a key is pressed

To do

So this line

int index = int(random(letter.length));

becomes:

  • int index; before setup()

  • AND index = int(random(letter.length)); inside setup() and in a new function void keyPressed() { }

1 Like

I was able to implement this with my suggestion so that the random number selection was made every 2 sec and displayed with each frame.

The rest of the code is easily achievable so I will not assist further.

@helpme123
millis() may be helpful:

Keep track of time of events and take the difference.

How you right your code is up to you.

:slight_smile:

1 Like