Assistance Needed for Text Fade

I need some help with my code. I am creating an Anagrams project for school. I need help knowing where to start to get the fading text to work. Whenever you type something that is invalid, there is a feedback string that pops up and tells the player what they did wrong. However, this feedback stays on the screen throughout the game until the timer runs out. I need help fading out this text after a specific amount of time. How would I get this to work?

Here is my code:

PFont font;
int titleScreen = 0;
int gameScreen = 1;
int resultScreen = 2;
int gameState;
int countDownTimer;
int countUpTimer;
//Reminder: Add three zeroes to the end of desired time in Game Duration
int gameDuration = 60000;
String guess = "";
int wordLimit = 20;
int scoreCounter = 0;
String[] wordArray;
char[] alphabet = {'a','b', 'c', 'd','e', 'f', 'g', 'h','i', 'j', 'k', 'l', 'm', 'n','o', 'p', 'q', 'r', 's', 't','u', 'v','w', 'x', 'y', 'z',};
char[] vowels = {'a', 'e', 'i', 'o', 'u','y',};
char[] wheelOfFortune = {'r', 's', 't', 'l', 'n', 'e',};
String scrambled = "";
int maxRandomLetters = 7;
int maxRandomVowels = 2;
//WOF - Wheel Of Fortune
int maxRandomWOF = 3;
int results;
boolean charInRandom;
String feedback = "";




void setup(){
  background(#A73333);
  size(1280,640);
  wordArray = loadStrings("word_dictionary.txt");
  font = createFont("SF Slapstick Comic Bold.ttf", 60);
  textFont(font);
  

}
void draw(){
  if (gameState == titleScreen){
    titleScreen();
  }
  else if(gameState == gameScreen){
    gameScreen();
  }
  else if(gameState == resultScreen){
    resultScreen();
  }
} 
void keyPressed(){
  
  if (gameState == titleScreen && keyCode == ' '){
    gameState = gameScreen;
    countUpTimer = millis();
    scrambled();
  }
  if (gameState == resultScreen && keyCode == ' '){
    gameState = gameScreen;
    scoreCounter = 0;
    countUpTimer = millis();
    scrambled();
  }
  if (gameState == resultScreen && (key == 'm' || key == 'M')){
    gameState = titleScreen;
    scoreCounter = 0;
  }
  if (gameState == gameScreen && key >= 'a' && key <= 'z' ||
      gameState == gameScreen && key >= 'A' && key <= 'Z'){
    guess = guess + key;
    guess = guess.toLowerCase();
  }
  if (keyCode == BACKSPACE && guess.length() > 0) {
   guess = guess.substring(0, guess.length() -1);
  }
  if (keyCode == ENTER && guess.length() > 0){
    wordCheck();
    letterInScrambled();
    guess = "";
  }
  if (guess.length() > wordLimit){
   guess = guess.substring(0, wordLimit); 
  }

}

boolean letterInScrambled(){
  
  String letterFound = scrambled;
  for (int i = 0; i < guess.length(); i++){
    boolean charInRandom = false;
    for(int j = 0; j < letterFound.length(); j++){
      if (guess.charAt(i) == letterFound.charAt(j)){
        charInRandom = true;
        letterFound = letterFound.substring(0, j) + letterFound.substring(j+1, letterFound.length());
        break;
      }
    }
    if (!charInRandom){
      feedback = "Invalid Letter(s)";
      return false;
    }
  }
  return true;
}

// returns the valid word list that is found in 
// data/word_dictionary.text
String[] getWordArray()
{
  String[] wordArray = loadStrings("word_dictionary.txt");
  sort(wordArray);
  return wordArray;
}
  
// returns an array of characters that are valid for the player
// to type as parts of their anagram puzzle
char[] getValidCharArray()
{
  String[] tempCharArray = loadStrings("valid_keys.txt");
  char validCharArray[] = new char[tempCharArray.length];
  for (int i = 0; i < tempCharArray.length; i++)
  {
    validCharArray[i] = tempCharArray[i].charAt(0);
  }    
  return validCharArray;
}

void printWords()
{
  print("Printing all the words");
  String[] wordArray = getWordArray();
  for (int i = 0; i < wordArray.length; i++)
  {
    println(wordArray[i]);   
  }
}

void printValidChars()
{
  print("Printing all the valid characters");
  char[] validCharArray = getValidCharArray();
  for (int i = 0; i < validCharArray.length; i++)
  {
    println(validCharArray[i]); 
  }
}

//void setup()
//{
//  println("testing the code");
//  printWords();
//  printValidChars();
//}

void gameScreen(){
  
  background(#A73333);
  fill (#29C190);
  
  countDownTimer = 60 - (millis() - countUpTimer)/1000;
  if (countDownTimer <=0){
    gameState = resultScreen;
    guess = "";
    scrambled = "";
    feedback = "";
  }
  text (countDownTimer, width/2, height/2);
  text (guess, width/2, height/4);  
  text ("Score:" + scoreCounter, 100, 25);
  text (scrambled, width/2, height/4*3);
  text (feedback, width/2, height/6*5);

}

void resultScreen(){
  
  background(#A73333);
  
  results = scoreCounter;
  text("Results: " + results, width/2, height/2);
  text("Press |SPACEBAR| to go again", width/2, height/4);
  text ("Press |M| to go back to the main menu", width/2, height/4*3);
}

void scrambled(){
  for (int i = 0; i < maxRandomLetters; i++){
    scrambled += alphabet[(int) random(0, alphabet.length)]; 
  }
  for (int i = 0; i < maxRandomVowels; i++){
    scrambled += vowels[(int) random(0, vowels.length)];
  }
  for (int i = 0; i < maxRandomWOF; i++){
    scrambled += wheelOfFortune[(int) random(0, wheelOfFortune.length)];
  }
}

void titleScreen(){
  
  background(#A73333);
  strokeWeight(10);
  fill(#29C190);
  textSize(70);
  textAlign(CENTER,CENTER);
  
  text("WELCOME TO", width / 2, height / 4);
  text("ANAGRAMS", width / 2, height / 2 - 75 );
  textSize(50);
  textAlign(CENTER,CENTER);
  text("Press 'SPACEBAR' to Start", width / 2, height / 2 + 25);
  
}

boolean wordCheck(){
  
  if (letterInScrambled()){
    for (int i = 0; i < wordArray.length; i++){
    if (guess .equals (wordArray[i].toLowerCase())){
      scoreCounter++;
      feedback = "Great Job";
      return true;
      }
    }
  } 
  feedback = "Invalid Word";
  return false;
}

that is the right situation:
guess is reseted and player type again:

  if (gameState == gameScreen && key >= 'a' && key <= 'z' ||
    gameState == gameScreen && key >= 'A' && key <= 'Z') {
    if ( guess == "" ) feedback = "";                                // change KLL
    guess = guess + key;
    guess = guess.toLowerCase();
  }

but if you want play with a real fade out timer pls. do that in
extra 10 line test code.

there is a issue with the text positioning

  text (countDownTimer, width/2, height/2);
  text (guess, width/2, height/4);
  text ("Score:" + scoreCounter, 100, 25);
  text (scrambled, width/2, +100);//height/43);                 // change KLL
  text (feedback, width/2, height-100); //height/65);           // change KLL

First of all, please format your code correctly. You can do so in processing by pressing Str+T(Ctrl+T) and then paste it inside the space created created by the </> sign at the top of the Reply editor.

As for your question, i don’t know if this is the best method, but you could set up an extra PGraphics in which you draw your text and set the PGraphics in general to only be there for some amount of time and then stop rendering it, which would be a hard cut. But you can just fade the text inside the PGraphics by resetting the PGraphics each frame and when writing the text, do this with a stroke that has an alpha value closer to 0 than before. Once the time limit is up, that alpha value should have reached 0 too, to again avoid a (even just barely) visible hard cut.

I don’t know if you can actually set the alpha value of the whole PGraphics, but if thats possible, then just draw the text once on it, without resetting the PGraphics each frame and just change the overall alpha value of the PGraphics.

I aded this timer
to reset the feedback value after 2sec

long feedbackDuration = 2000;
long feedbackStart = 0;
...
if(millis()-feedbackStart> feedbackDuration)feedback="";

Here is the new code:

PFont font;
int titleScreen = 0;
int gameScreen = 1;
int resultScreen = 2;
int gameState;
int countDownTimer;
int countUpTimer;
//Reminder: Add three zeroes to the end of desired time in Game Duration
int gameDuration = 60000;
String guess = "";
int wordLimit = 20;
int scoreCounter = 0;
String[] wordArray;
char[] alphabet = {'a','b', 'c', 'd','e', 'f', 'g', 'h','i', 'j', 'k', 'l', 'm', 'n','o', 'p', 'q', 'r', 's', 't','u', 'v','w', 'x', 'y', 'z',};
char[] vowels = {'a', 'e', 'i', 'o', 'u','y',};
char[] wheelOfFortune = {'r', 's', 't', 'l', 'n', 'e',};
String scrambled = "";
int maxRandomLetters = 7;
int maxRandomVowels = 2;
//WOF - Wheel Of Fortune
int maxRandomWOF = 3;
int results;
boolean charInRandom;
String feedback = "";
long feedbackDuration = 2000;
long feedbackStart = 0;

void setup(){
background(#A73333);
size(1280,640);
wordArray = loadStrings("word_dictionary.txt");
font = createFont("SansSerif-48", 60);
textFont(font);

}
void draw(){
if (gameState == titleScreen){
titleScreen();
}
else if(gameState == gameScreen){
gameScreen();
}
else if(gameState == resultScreen){
resultScreen();
}
}


void keyPressed(){

if (gameState == titleScreen && keyCode == ' '){
gameState = gameScreen;
countUpTimer = millis();
scrambled();
}
if (gameState == resultScreen && keyCode == ' '){
gameState = gameScreen;
scoreCounter = 0;
countUpTimer = millis();
scrambled();
}
if (gameState == resultScreen && (key == 'm' || key == 'M')){
gameState = titleScreen;
scoreCounter = 0;
}
if (gameState == gameScreen && key >= 'a' && key <= 'z' ||
gameState == gameScreen && key >= 'A' && key <= 'Z'){
guess = guess + key;
guess = guess.toLowerCase();
}
if (keyCode == BACKSPACE && guess.length() > 0) {
guess = guess.substring(0, guess.length() -1);
}
if (keyCode == ENTER && guess.length() > 0){
wordCheck();
letterInScrambled();
guess = "";
}
if (guess.length() > wordLimit){
guess = guess.substring(0, wordLimit);
}

}

boolean letterInScrambled(){

String letterFound = scrambled;
for (int i = 0; i < guess.length(); i++){
boolean charInRandom = false;
for(int j = 0; j < letterFound.length(); j++){
if (guess.charAt(i) == letterFound.charAt(j)){
charInRandom = true;
letterFound = letterFound.substring(0, j) + letterFound.substring(j+1, letterFound.length());
break;
}
}
if (!charInRandom){
feedback = "Invalid Letter(s)";
return false;
}
}
return true;
}

// returns the valid word list that is found in
// data/word_dictionary.text
String[] getWordArray()
{
String[] wordArray = loadStrings("word_dictionary.txt");
sort(wordArray);
return wordArray;
}

// returns an array of characters that are valid for the player
// to type as parts of their anagram puzzle
char[] getValidCharArray()
{
String[] tempCharArray = loadStrings("valid_keys.txt");
char validCharArray[] = new char[tempCharArray.length];
for (int i = 0; i < tempCharArray.length; i++)
{
validCharArray[i] = tempCharArray[i].charAt(0);
}
return validCharArray;
}

void printWords()
{
print("Printing all the words");
String[] wordArray = getWordArray();
for (int i = 0; i < wordArray.length; i++)
{
println(wordArray[i]);
}
}

void printValidChars()
{
print("Printing all the valid characters");
char[] validCharArray = getValidCharArray();
for (int i = 0; i < validCharArray.length; i++)
{
println(validCharArray[i]);
}
}

//void setup()
//{
// println("testing the code");
// printWords();
// printValidChars();
//}

void gameScreen(){

background(#A73333);
fill (#29C190);

countDownTimer = 60 - (millis() - countUpTimer)/1000;
if (countDownTimer <=0){
gameState = resultScreen;
guess = "";
scrambled = "";
feedback = "";
}
text (countDownTimer, width/2, height/2);
text (guess, width/2, height/4);
text ("Score:" + scoreCounter, 100, 25);
text (scrambled, width/2, height/43);

if(millis()-feedbackStart> feedbackDuration)feedback="";
text (feedback, width/2, height/65);

}

void resultScreen(){

background(#A73333);

results = scoreCounter;
text("Results: " + results, width/2, height/2);
text("Press |SPACEBAR| to go again", width/2, height/4);
text ("Press |M| to go back to the main menu", width/2, height/4*3);
}

void scrambled(){
for (int i = 0; i < maxRandomLetters; i++){
scrambled += alphabet[(int) random(0, alphabet.length)];
}
for (int i = 0; i < maxRandomVowels; i++){
scrambled += vowels[(int) random(0, vowels.length)];
}
for (int i = 0; i < maxRandomWOF; i++){
scrambled += wheelOfFortune[(int) random(0, wheelOfFortune.length)];
}
}

void titleScreen(){

background(#A73333);
strokeWeight(10);
fill(#29C190);
textSize(70);
textAlign(CENTER,CENTER);

text("WELCOME TO", width / 2, height / 4);
text("ANAGRAMS", width / 2, height / 2 - 75 );
textSize(50);
textAlign(CENTER,CENTER);
text("Press 'SPACEBAR' to Start", width / 2, height / 2 + 25);

}

boolean wordCheck(){

if (letterInScrambled()){
for (int i = 0; i < wordArray.length; i++){
if (guess .equals (wordArray[i].toLowerCase())){
scoreCounter++;
feedback = "Great Job";
return true;
}
}
}
feedback = "Invalid Word";
feedbackStart=millis();
return false;
}

Thank you guys very much! These suggestions helped my problem a lot!