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;
}