Help with a project (game to guess words)

sorry for deleting the post earlier but i dont really understand how to use the forum. Also im quite new to processing too.
Problem: the full word wont print and i have tried using if(keyPressed) text(“s”,x,y) to screen but they wont stay on, is there any way i can get a hint for this? Also is there a way to reset my timer variable so that i dont have to name new timer variables to use in my levle functions, thanks.

code down below:

import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioPlayer song; //https://freemusicarchive.org/genre/Christmas(Dab Yan-key alpine bells(jingle bells);

int timer= 6000; // referenced from Hangman Final by Anthony Yu- openproccessing.org/sketch/61942/*
int timer1=5000;
int timer2=4000;

String type= "";



PImage xmas1,xmas2,xmas5,xmas12;
PFont font; // referenced from Meri Engel Youtube video "Adding and using fonts in processing"
// idea for font sourced from Susan Alden

// either have delay(500) at the start of game or have Mousepressed to start game
int gameLevel; // game level is implemented

 void setup() 
 {
 size(626,417,P2D);
 minim = new Minim(this);
 song= minim.loadFile("song.mp3");
 song.loop();
 
 
 font = loadFont("thinkQuick.vlw");
 textFont(font);
 xmas1 = loadImage("xmas1.jpg");
 xmas2 = loadImage("xmas2.png");// load image and data into scene data structure( from lab 3 with Professor Stephen Brown)
 xmas5 = loadImage("xmas5.jpg");//font = createFont("ArialMT-48", 225);
 xmas12 = loadImage("xmas12.jpg");


}

void draw() 
{
background(xmas1);
switch(gameLevel)
{
  case 0: 
  startMenu();
  break;
  
  case 1:
  level1();
  break;
  
  case 2:
  level2();
  break;
  
  case 3:
  level3();
  break;
   
}
  if(key == '1')
  {
  gameLevel=1;
  level1();
  }
  
  else if( key == '2')
  {
    gameLevel=2;
    level2();
  }
  
else if( key == '3')
  {
    gameLevel=3;
    level3();
  }
  else
  {
    gameLevel=0;
    startMenu();
  }
}

void startMenu()
{
background(xmas1);

textFont(font,40);
fill(255,0,128);
text("Welcome to Think Quik!", 100,100);
textFont(font,40);
fill(255,0,128);
text("Choose >1< for level 1", 100,200);
textFont(font,40);
fill(255,0,128);
text("Choose >2< for level 2", 100,300);
textFont(font,40);
fill(255,0,128);
text("Choose >3< for level 3", 100,400);
}

void level1()
{
 background(xmas12); 
 fill(0);
 rect(80,50,400,100);
 fill(255);
 textSize(25);
 text("Hint:Usually falls in the Winter", 100,100); // hint for the player to guess the word
 
String word = "snow";
String guess = "";

for(int i = 0; i < word.length();i++)
{
if(keyPressed)
{
  if(key == word.charAt(i)) // if the character's key equals any letter in the word prints word to screen
{
  type += key;
}
else if(key=='\n')
{
  type="";
}
}

for(int j=0; j <word.length(); j++)//for loop creates underlines for the word depending on how many
{
  int underScore= 60*j;//referenced from Hangman Final by Anthony Yu- openproccessing.org/sketch/61942/*
  int underScore1=200;//referenced from Hangman Final by Anthony Yu- openproccessing.org/sketch/61942/*
  line(underScore+50,underScore1,underScore+25,underScore1);//referenced from Hangman Final by Anthony Yu- openproccessing.org/sketch/61942/*
}
}
guess += key+ type;
guess = guess.toUpperCase(); // letters stay on screen but only print one  and if line 129-132 is removed multiple letters get printed 

if(guess.length() > 1)
{
  type="";
}
fill(0);
text(guess,0,200); 
 
  for(int a =0; a < guess.length();a++)
  {
    if(guess.length() == word.length())
    {
      gameLevel=2; // if the word is guessed then player moves to next level
    }
      
  }

  
timer--;
fill(0);
textSize(25);
text(timer,500,50);

if(timer == 0)
{
  exit();
}

}
 

void level2()
{
  background(xmas5);
  
  if(keyPressed)
  {
    if(key == 'x')
    {
      text("x",160,200);
    }
  }
  
  
 timer1--;
fill(0);
textSize(25);
text(timer1,490,50);
if(timer1 == 0)
{
  exit();
}
}

void level3()
{
  background(xmas2);
  timer2--;
fill(0);
textSize(25);
text(timer2,490,50);

String s1= "winter";

 if(keyPressed) // keypressed object to print letters to screen except theyre not staying on the screen
 {
   if(key == s1.charAt(0))
   {
     text( "w",170,200);
   }
 }

if(timer2 == 0)
{
  exit();
}
}

keypressed command to print letters to screen except they’re not staying on the screen

the text() command is within the if(keypressed) section, so the text is only displayed during key is pressed and not through out.

You can instead use text() outside the if(keypressed) section. When you want to have the text not be displayed initially: set a global boolean variable to false and in the if(keypressed) section set it to true.

you can say timer=6000; to reset

Remark

I looked at your level 1:

Can you explain what you want to achieve?

for example, when the word is snow and I type “o” do you want to be displayed

_ _ o _ and then when next key I type w
_ _ o w ?

Thanks!

Warm regards,

Chrisr

yeah this is what i want to achieve, would i use string variable= string.replace(“_”, text)?

Also thanks so i would have to globalise a boolean variable as well? will that let me print the whole word and thanks so much again.

idea…


int timer=6000;
int i = 0;

String type= "____";

PImage xmas1, xmas9, xmas8, xmas12;
PFont font; // referenced from Meri Engel Youtube video “Adding and using fonts in processing”
// idea for font sourced from Susan Alden

// either have delay(500) at the start of game or have Mousepressed to start game
int gameLevel; // game level is implemented

void setup()
{
  size(626, 417, P2D);

  font = createFont("thinkQuick.vlw", 18);
  textFont(font);
  xmas1 = loadImage("xmas1.jpg");
  xmas9 = loadImage("xmas9.jpg");// load image and data into scene data structure( from lab 3 with Professor Stephen Brown)
  xmas8 = loadImage("xmas8.jpg");//font = createFont(“ArialMT-48”, 225);
  xmas12 = loadImage("xmas12.jpg");
}

void draw()
{
  background(0);
  switch(gameLevel)
  {
  case 0:
    startMenu();

    if (key == '1')
    {
      gameLevel=1;
      // level1(); // NO
    } else if ( key == '2')
    {
      gameLevel=2;
      //level2();
    } else if ( key == '3')
    {
      gameLevel=3;
      //level3();
    }
    break;

  case 1:
    level1();
    //println(type);
    fill(255);
    text(type, 
      111, 211); 
    break;

  case 2:
    level2();
    break;

  case 3:
    level3();
    break;
  }
}

void startMenu()
{
  background(0);

  textFont(font, 40);
  fill(255, 0, 128);
  text("Welcome to Think Quik!", 100, 100);
  textFont(font, 40);
  fill(255, 0, 128);
  text("Choose >1< for level 1", 100, 200);
  textFont(font, 40);
  fill(255, 0, 128);
  text("Choose >2< for level 2", 100, 300);
  textFont(font, 40);
  fill(255, 0, 128);
  text("Choose >3< for level 3", 100, 400);
}

void level1()
{
  background(0);
  fill(0);
  rect(80, 50, 400, 100);
  fill(255);
  textSize(25);
  text("Hint: Usually falls in the Winter", 100, 100); // hint for the player to guess the word

  String word = "snow";
  String guess = "";

  //  for (int i = 0; i < word.length(); i++)  {
  if (keyPressed)
  {
    if (i<word.length() && key == word.charAt(i)) // if the character’s key equals any letter in the word prints word to screen
    {
      int i_3=0;
      String t_=""; 
      for (char c : word.toCharArray()) { 
        if (i_3!=i) 
          t_ +=  type.charAt(i_3)  ;
        else 
        t_ +=c;
        i_3++;
      }//for 

      type = t_;    // key;
      println(type) ;
      i++;
    } else if (key=='\n')
    {
      type="";
    }
  }

  for (int j=0; j <word.length(); j++)//for loop creates underlines for the word depending on how many
  {
    int underScore= 60*j;//referenced from Hangman Final by Anthony Yu- openproccessing.org/sketch/61942/
    int underScore1=200;//referenced from Hangman Final by Anthony Yu- openproccessing.org/sketch/61942/*
    line(underScore+50, underScore1, underScore+25, underScore1);//referenced from Hangman Final by Anthony Yu- openproccessing.org/sketch/61942/*
  }
  // }
  guess += key + type;
  guess = guess.toUpperCase(); // letters stay on screen but only print one and if line 129-132 is removed multiple letters get printed

  if (guess.length() > 1)
  {
    // type="";
  }
  fill(0);
  text(guess, 0, 200);

  for (int a =0; a < guess.length(); a++)
  {
    if (word.length() == type.length() && 
      word.charAt(0)==type.charAt(0)
      && word.charAt(1)==type.charAt(1)
      && word.charAt(2)==type.charAt(2)
      && word.charAt(3)==type.charAt(3))
    {
      gameLevel=2; // if the word is guessed then player moves to next level
      timer=5000;
    }
  }

  timer--;
  fill(0);
  textSize(25);
  text(timer, 500, 50);

  if (timer == 0)
  {
    //  exit();
  }
}

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

void level2()
{
  background(0);

  fill(220);
  text("Level 2", 111, 211);

  if (gameLevel != 1 && gameLevel !=3)
  {
    // timer--;
  }

  // timer--;
  fill(0);
  textSize(25);
  text(timer, 490, 50);
}

void level3()
{
  background(0);

  timer = 4000;

  timer--;
  fill(0);
  textSize(25);
  text(timer, 490, 50);

  String s1= "winter";

  if (keyPressed) // keypressed object to print letters to screen except theyre not staying on the screen
  {
    if (key == s1.charAt(0))
    {
      text( "w", 170, 200);
    }
  }
}

thank you so much chrisir, i was seriously struggling and i also wasnt sure if i had enough work done to ask for help , i wanna cite your help, how would you want me to address you?

No, thanks

Not necessary

ok and thanks your idea did the trick

1 Like

your idea worked for level 1 but on level 2 im having the same problem any hints?

import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioPlayer song; //https://freemusicarchive.org/genre/Christmas(Dab Yan-key alpine bells(jingle bells);

int timer= 6000; // timer for game
//int timer1=5000;
//int timer2=4000;

String type= "__________";
int i=0;

PImage xmas1,xmas2,xmas5,xmas12;
PFont font; // referenced from Meri Engel Youtube video "Adding and using fonts in processing"
// idea for font sourced from Susan Alden

// either have delay(500) at the start of game or have Mousepressed to start game
int gameLevel; // game level is implemented

void setup()
{
  size(626, 417, P2D);

  font = createFont("thinkQuick.vlw", 18);
  textFont(font);
  xmas1 = loadImage("xmas1.jpg");
  xmas5 = loadImage("xmas5.jpg");// load image and data into scene data structure( from lab 3 with Professor Stephen Brown)  xmas8 = loadImage("xmas8.jpg");//font = createFont(“ArialMT-48”, 225);
 xmas12 = loadImage("xmas12.jpg");
 xmas2 = loadImage("xmas2.png");
}

void draw()
{
  background(0);
  switch(gameLevel)
  {
  case 0:
    startMenu();

    if (key == '1')
    {
      gameLevel=1;
      // level1(); // NO
    } else if ( key == '2')
    {
      gameLevel=2;
      //level2();
    } else if ( key == '3')
    {
      gameLevel=3;
      //level3();
    }
    break;

  case 1:
    level1();
    //println(type);
   
    break;

  case 2:
    level2();
     fill(255);
    textSize(40);
    text(type, 
      160, 400); 
    break;

  case 3:
    level3();
    break;
  }
}

void startMenu()
{
  background(xmas1);

  textFont(font, 40);
  fill(255, 0, 128);
  text("Welcome to Think Quik!", 100, 100);
  textFont(font, 40);
  fill(255, 0, 128);
  text("Choose >1< for level 1", 100, 200);
  textFont(font, 40);
  fill(255, 0, 128);
  text("Choose >2< for level 2", 100, 300);
  textFont(font, 40);
  fill(255, 0, 128);
  text("Choose >3< for level 3", 100, 400);
}

void level1()
{
  background(xmas12);
  fill(0);
  rect(80, 50, 400, 100);
  fill(255);
  textSize(25);
  text("Hint: Usually falls in Winter", 100, 100); // hint for the player to guess the word

  String word = "snow";
  String guess = "";

  //  for (int i = 0; i < word.length(); i++)  {
  if (keyPressed)
  {
    if (i<word.length() && key == word.charAt(i)) // if the character’s key equals any letter in the word prints word to screen
    {
      int i_3=0;
      String t_=""; 
      for (char c : word.toCharArray()) { 
        if (i_3!=i) 
          t_ +=  type.charAt(i_3)  ;
        else 
        t_ +=c;
        i_3++;
      }//for 

      type = t_;    // key;
      println(type) ;
      i++;
    } else if (key=='\n')
    {
      type="";
    } // hitting enter results in the null pointer exception
  }


  // }
  guess += key + type;
  guess = guess.toUpperCase(); // letters stay on screen but only print one and if line 129-132 is removed multiple letters get printed

  fill(0);
  text(guess, 160, 200);

  for (int a =0; a < guess.length(); a++)
  {
    if (word.length() == type.length() && 
      word.charAt(0)==type.charAt(0)
      && word.charAt(1)==type.charAt(1)
      && word.charAt(2)==type.charAt(2)
      && word.charAt(3)==type.charAt(3))
    {
      gameLevel=2; // if the word is guessed then player moves to next level
      timer=5000;
      type="";
    }
  }

  timer--;
  fill(255);
  textSize(25);
  text(timer, 500, 50);

  if (timer == 0)
  {
     exit();
  }
}

void level2()
{
  background(xmas5);

 fill(0);
  rect(80, 50, 400, 100);
  fill(255);
  textSize(25);
  text("Hint: Christmas Tree decoration", 100, 100); // hint for the player to guess the word

  String word = "baubles";
  String guess = ""; 

  if (keyPressed)
  {
    if (i<word.length() && key == word.charAt(i)) // if the character’s key equals any letter in the word prints word to screen
    {
      int i_3=0;
      String t_=""; 
      for (char c : word.toCharArray()) { 
        if (i_3!=i) 
          t_ +=  type.charAt(i_3);
          type="";
              
        else 
        t_ +=c;
      
        i_3++;
      }//for 

      type = t_;    // key;
      println(type) ;
      i++;
    } else if (key=='\n')
    {
      type="";
    }
  }

  for (int j=0; j <word.length(); j++)//for loop creates underlines for the word depending on how many
  {
    int underScore= 60*j;//referenced from Hangman Final by Anthony Yu- openproccessing.org/sketch/61942/
    int underScore1=200;//referenced from Hangman Final by Anthony Yu- openproccessing.org/sketch/61942/*
    line(underScore+50, underScore1, underScore+25, underScore1);//referenced from Hangman Final by Anthony Yu- openproccessing.org/sketch/61942/*
  }
  // }
  guess += key + type;
  guess = guess.toUpperCase(); // letters stay on screen but only print one and if line 129-132 is removed multiple letters get printed

  
  fill(0);
  text(guess, 0, 200);

  for (int a =0; a < guess.length(); a++)
  {
    if (word.length() == type.length() && 
      word.charAt(0)==type.charAt(0)
      && word.charAt(1)==type.charAt(1)
      && word.charAt(2)==type.charAt(2)
      && word.charAt(3)==type.charAt(3)
      && word.charAt(4)==type.charAt(4)
      && word.charAt(5)==type.charAt(5)
       && word.charAt(5)==type.charAt(5)
     &&word.charAt(6)==type.charAt(6) )
    {
      gameLevel=3; // if the word is guessed then player moves to next level
      timer=4000;
    }
  }
  timer--;
  fill(255);
  textSize(25);
  text(timer, 500, 50);
}

void level3()
{
  background(xmas2);

  

  timer--;
  fill(0);
  textSize(25);
  text(timer, 490, 50);



 
}

In level 2 you need to do the same as in level 1

i had tried that before, like i copied and pasted the level one code into level 2 and i got an out of bounds string error but its fixed now. i have a restart for the game but every time it goes to level 1, the word from level 3 appears, ive tried clearing type and i in the restart but it led to null



import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioPlayer song; //https://freemusicarchive.org/genre/Christmas(Dab Yan-key alpine bells(jingle bells);

int timer= 6000; // timer for game
//int timer1=5000;
//int timer2=4000;

String type= "__________";
int i=0;

PImage xmas1, xmas2, xmas5, xmas12;
PFont font; // referenced from Meri Engel Youtube video "Adding and using fonts in processing"
// idea for font sourced from Susan Alden

// either have delay(500) at the start of game or have Mousepressed to start game
int gameLevel; // game level is implemented

void setup()
{
  size(626, 417, P2D);

  font = createFont("thinkQuick.vlw", 18);
  textFont(font);
  xmas1 = loadImage("xmas1.jpg");
  xmas5 = loadImage("xmas5.jpg");// load image and data into scene data structure( from lab 3 with Professor Stephen Brown)  xmas8 = loadImage("xmas8.jpg");//font = createFont(“ArialMT-48”, 225);
  xmas12 = loadImage("xmas12.jpg");
  xmas2 = loadImage("xmas2.png");
}

void draw()
{
  background(0);
  switch(gameLevel)
  {
  case 0:
    startMenu();

    if (key == '1')
    {
      type="____";
      gameLevel=1;
      // level1(); // NO
    } else if ( key == '2')
    {
      type = "_______";//for word baubles
      i=0; 
      gameLevel=2;
      //level2();
    } else if ( key == '3')
    {
      gameLevel=3;
      //level3();
    }
    break;

  case 1:
    level1();
    //println(type);
    break;

  case 2:
    level2();
    fill(255);
    textSize(40);
    text(type, 
      160, 400); 
    break;

  case 3:
    level3();
    break;
  }
}

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

void startMenu()
{
  background(0);

  textFont(font, 40);
  fill(255, 0, 128);
  text("Welcome to Think Quick!", 100, 100);
  textFont(font, 40);
  fill(255, 0, 128);
  text("Choose '1' for level 1", 100, 200);
  textFont(font, 40);
  fill(255, 0, 128);
  text("Choose >2< for level 2", 100, 300);
  textFont(font, 40);
  fill(255, 0, 128);
  text("Choose >3< for level 3", 100, 400);
}

void level1()
{
  background(0);
  fill(0);
  rect(80, 50, 400, 100);
  fill(255);
  textSize(25);
  text("Hint: Usually falls in Winter", 100, 100); // hint for the player to guess the word

  String word = "snow";
  String guess = "";

  text(type, 111, 211); 

  //  for (int i = 0; i < word.length(); i++)  {
  if (keyPressed)
  {
    if (i<word.length() && key == word.charAt(i)) // if the character’s key equals any letter in the word prints word to screen
    {
      int i_3=0;
      String t_=""; 
      for (char c : word.toCharArray()) { 
        if (i_3!=i) 
          t_ +=  type.charAt(i_3)  ;
        else 
        t_ +=c;
        i_3++;
      }//for 

      type = t_;    // key;
      println(type) ;
      i++;
    } else if (key=='\n')
    {
      type="";
    } // hitting enter results in the null pointer exception
  }


  // }
  guess += key + type;
  guess = guess.toUpperCase(); // letters stay on screen but only print one and if line 129-132 is removed multiple letters get printed

  fill(0);
  text(guess, 160, 200);

  //for (int a =0; a < guess.length(); a++) {
  if (word.length() == type.length() 
    && word.charAt(0)==type.charAt(0)
    && word.charAt(1)==type.charAt(1)
    && word.charAt(2)==type.charAt(2)
    && word.charAt(3)==type.charAt(3))
  {
    fill(255, 0, 0); 
    text("YOU WON                Hit Space Bar", 111, 311); 
    fill(255); 
    if (keyPressed&&key==' ') {
      // if the word is guessed then player moves to next level - reset data  
      gameLevel=2; 
      timer=5000;
      //type="";
      type = "_______"; // for word = "baubles";
      i=0; // !!!!!!!!!
      key=0;
    }
  }
  //}

  timer--;
  fill(255);
  textSize(25);
  text(timer, 500, 50);

  if (timer == 0)
  {
    exit();
  }
}

void level2()
{
  background(0);

  fill(0);
  rect(80, 50, 400, 100);
  fill(255);
  textSize(25);
  text("Hint: Christmas Tree decoration", 100, 100); // hint for the player to guess the word

  String word = "baubles";
  String guess = ""; 

  fill(255); 
  text(type, 111, 211); 


  //  for (int i = 0; i < word.length(); i++)  {
  if (keyPressed)
  {
    if (i<word.length() && key == word.charAt(i)) // if the character’s key equals any letter in the word prints word to screen
    {
      int i_3=0;
      String t_=""; 
      for (char c : word.toCharArray()) { 
        if (i_3!=i) 
          t_ +=  type.charAt(i_3)  ;
        else 
        t_ +=c;
        i_3++;
      }//for 

      type = t_;    // key;
      println(type) ;
      i++;
    } else if (key=='\n')
    {
      // type="";
    } // hitting enter results in the null pointer exception
  }


  // }
  guess += key + type;
  guess = guess.toUpperCase(); // letters stay on screen but only print one and if line 129-132 is removed multiple letters get printed

  fill(0);
  text(guess, 160, 200);

  int count = 0; 
  for (int a = 0; a < word.length(); a++) {
    if (word.charAt(a) == type.charAt(a)) {
      count ++;
    }//if
  }
  if (count==word.length())  
  {
    fill(255, 0, 0); 
    text("YOU WON                Hit Space Bar", 111, 311); 
    fill(255); 
    if (keyPressed&&key==' ') {
      gameLevel=3; // if the word is guessed then player moves to next level
      timer=5000;
      type="";
    }
  }

  timer--;
  fill(255);
  textSize(25);
  text(timer, 500, 50);

  if (timer == 0)
  {
    exit();
  }
}

void level3()
{
  background(0);



  timer--;
  fill(0);
  textSize(25);
  text(timer, 490, 50);
}
//

here is my version.

It shows a better “You WON” screen

to be honest, for example for the word snow, it shouldn’t matter whether he presses o first or w, right? The letter could just appear at the right spot in type?

So the user could type s - w - n - o and solved, no?

Request

To get help with your restart, post your entire code please

thanks this was my version:``


int timer= 6000; // timer for game
//int timer1=5000;
//int timer2=4000;

String type= "________";
int i=0;


PImage xmas1,xmas2,xmas5,xmas12,xmas9;
PFont font; // referenced from Meri Engel Youtube video "Adding and using fonts in processing"
// idea for font sourced from Susan Alden

// either have delay(500) at the start of game or have Mousepressed to start game
int gameLevel; // game level is implemented

void setup()
{
  size(626, 417, P2D);

  minim = new Minim(this);
  song = minim.loadFile("song.mp3");
  song.loop();
  font = loadFont("font2.vlw");
  textFont(font);
  xmas1 = loadImage("xmas1.jpg");
  xmas5 = loadImage("xmas5.jpg");// load image and data into scene data structure( from lab 3 with Professor Stephen Brown)  xmas8 = loadImage("xmas8.jpg");//font = createFont(“ArialMT-48”, 225);
 xmas12 = loadImage("xmas12.jpg");
 xmas2 = loadImage("xmas2.png");
 xmas9 = loadImage("xmas9.jpg");
}

void draw()
{
  background(0);
  switch(gameLevel) // switch cases for startMenu, levels and the end
  {
  case 0:
    startMenu();

    if (key == 's')
    {
      gameLevel=1;
      // level1(); // NO
    }
    else
    {
      gameLevel=0;
    }
    break;

  case 1:
    level1();
    //println(type);
      fill(255);
    textSize(40); 
    text(type,  
      160, 400); //shows the letters the player guesses 
    break;

  case 2:
    level2();
     fill(255);
    textSize(40);
    text(type, 
      160, 400); 
    break;

  case 3:
    level3();
       fill(255);
    textSize(40);
    text(type, 
      160, 400); 
    break;
    
    case 4:
    restart();
    break;
   
  }
}

void startMenu()
{
  background(xmas1);

  textFont(font, 30);
 fill(128,128,255);//lilac
  text("Welcome to Think Quik!", 100, 50);
  
  
  text("Press >s< to start the game", 100, 100);
  text("Instructions: Use the Keyboard to type letters", 0, 200);
  text("for your guesses and good luck!", 0, 230);
  text("Rules: Guess the word before time runs out", 0, 280);
  text("Hints are given for the word on each level",0,310);
}

void level1()
{
  background(xmas12);
  fill(0);
  rect(80, 50, 400, 100);
  fill(255);
  textFont(font);
  textSize(25);
  text("Hint: Usually falls in Winter", 100, 100); // hint for the player to guess the word

  String word = "snow";
  String guess = "";

  //for (int i = 0; i < word.length(); i++)  {
  if (keyPressed)
  {
    if (i<word.length() && key == word.charAt(i)) // if the character’s key equals any letter in the word prints word to screen
    {
     
      int i_3=0;
      String t_=""; 
      for (char c : word.toCharArray()) { 
        if (i_3!=i) 
          t_ +=  type.charAt(i_3);
        else 
        t_ +=c;
        i_3++;
   
      }//for 

      type = t_;    // key;
     // println(type) ;
      i++;
    } else if (key=='\n')
    {
      type="";
    } // hitting enter results in the null pointer exception
  }

// }
  guess += key + type;
  guess = guess.toUpperCase(); // letters stay on screen but only print one and if line 129-132 is removed multiple letters get printed

  fill(0);
  textFont(font);
  textSize(40);
  text(guess, 160, 250);

  for (int a =0; a < guess.length(); a++)
  {
    if (word.length() == type.length() && 
      word.charAt(0)==type.charAt(0)
      && word.charAt(1)==type.charAt(1)
      && word.charAt(2)==type.charAt(2)
      && word.charAt(3)==type.charAt(3))
    {
      gameLevel=2; // if the word is guessed then player moves to next level
      timer=5000;
      type="";
      i=0;
      delay(500);
    }
  }

  timer--;
  fill(255);
  textSize(25);
  text(timer, 500, 50);
}

void level2()
{
  background(xmas5);

 fill(0);
  rect(80, 50, 400, 100);
  fill(255);
  textSize(25);
  text("Hint: Christmas Tree decoration", 100, 100); // hint for the player to guess the word

  String word = "baubles";
  String guess = ""; 
int len= word.length();
  //for (int i = 0; i < word.length(); i++)  {
  if (keyPressed)
  {
    if (i<word.length() && key == word.charAt(i)) // if the character’s key equals any letter in the word prints word to screen
    {
     
      int i_3=0;
      String t_=""; 
      for (char c : word.toCharArray()) { 
        if (i_3!=i)
          len= len-i_3; // int length change implemented by Cian Burke.

        else 
        t_ +=c;
        i_3++;
   len = len-i_3;
      }//for 

      type += t_;    // key;
   //println(type);
      i++;
    }
  }//println(type);
  

// }
  guess += key + type;
  guess = guess.toUpperCase(); 
  for (int a =0; a < guess.length(); a++)
  {
    if (word.length() == type.length() && 
      word.charAt(0)==type.charAt(0)
      && word.charAt(1)==type.charAt(1)
      && word.charAt(2)==type.charAt(2)
      && word.charAt(3)==type.charAt(3)
      && word.charAt(4)==type.charAt(4)
      && word.charAt(5)==type.charAt(5)
      && word.charAt(6)==type.charAt(6))
    {
      gameLevel=3; // if the word is guessed then player moves to next level
      timer=4000;
      type="";
      i=0;
       delay(500);
    }
  }
 fill(0);
  textSize(40);
  text(guess, 160, 250);

  if(timer == 0)
  {
    exit(); //if the timer runs out before player
  }

  timer--;
  fill(255);
  textSize(25);
  text(timer, 500, 50);
}

void level3()
{
  background(xmas2);
  String word = "reindeer";
  String guess = ""; 
int len= word.length();
fill(0);
  rect(80, 50, 400, 100);
  fill(255);
  textSize(25);
  text("Hint: Santa's pets", 160, 100); // hint for the player to guess the word

  //for (int i = 0; i < word.length(); i++)  {
  if (keyPressed)
  {
    if (i<word.length() && key == word.charAt(i)) // if the character’s key equals any letter in the word prints word to screen and if its the same length
    {
     
      int i_3=0;
      String t_=""; 
      for (char c : word.toCharArray()) { 
        if (i_3!=i)
          len= len-i_3;

        else 
        t_ +=c;
        i_3++;
   len = len-i_3;
      }//for 

      type += t_;    // key;
   //println(type);
      i++;
    }
  }//println(type);
  

// }
  guess += key + type;
  guess = guess.toUpperCase(); // letters stay on screen but only print one and if line 129-132 is removed multiple letters get printed
  for (int a =0; a < guess.length(); a++) // verifies to see if every guessed letter is correct
  {
    if (word.length() == type.length() && 
      word.charAt(0)==type.charAt(0)
      && word.charAt(1)==type.charAt(1)
      && word.charAt(2)==type.charAt(2)
      && word.charAt(3)==type.charAt(3)
      && word.charAt(4)==type.charAt(4) 
      && word.charAt(5)==type.charAt(5)
      && word.charAt(6)==type.charAt(6)
      && word.charAt(7)==type.charAt(7))
    {
      gameLevel=4; // player moves to restart()
      i=0;
    }
  }
 fill(0);
  textSize(40);
  text(guess, 160, 250);
  if(timer == 0)
 {
   exit();
 }
    
  timer--;
  fill(0);
  textSize(25);
  text(timer, 490, 50);
  
  
  }

void restart() // when the game is won, screen pops up to prompt the player if they wih to play again.
{
  background(xmas9);
   fill(0,255,0);
  textSize(50);
  text("Press >y< to play again", 50, 150);
  text("Press >n< to leave game", 50, 250);
  text("Congrats on winning", 50, 350);
  if(key == 'y')
  {
    gameLevel=1; //player returns to startMenu() to play again.
    delay(500);
  }
  else if(key == 'n')
  {
    exit(); // exits the game if they dont want to play again.
  }
}
 

Think Quick with c?

restart

      timer=4000;
      type="____";
      i=0;

This works:



int timer= 6000; // timer for game
//int timer1=5000;
//int timer2=4000;

String type= "________";
int i=0;


PImage xmas1, xmas2, xmas5, xmas12, xmas9;
PFont font; // referenced from Meri Engel Youtube video "Adding and using fonts in processing"
// idea for font sourced from Susan Alden

// either have delay(500) at the start of game or have Mousepressed to start game
int gameLevel; // game level is implemented

void setup()
{
  size(626, 417, P2D);

  //minim = new Minim(this);
  //song = minim.loadFile("song.mp3");
  //song.loop();
  font = createFont("font2.vlw", 16);
  // textFont(font);
  xmas1 = loadImage("xmas1.jpg");
  xmas5 = loadImage("xmas5.jpg");// load image and data into scene data structure( from lab 3 with Professor Stephen Brown)  xmas8 = loadImage("xmas8.jpg");//font = createFont(“ArialMT-48”, 225);
  xmas12 = loadImage("xmas12.jpg");
  xmas2 = loadImage("xmas2.png");
  xmas9 = loadImage("xmas9.jpg");
}

void draw()
{
  background(0);
  switch(gameLevel) // switch cases for startMenu, levels and the end
  {
  case 0:
    startMenu();

    if (key == 's')
    {
      gameLevel=1;
      // level1(); // NO
    } else
    {
      gameLevel=0;
    }
    break;

  case 1:
    level1();
    //println(type);
    fill(255);
    textSize(40); 
    text(type, 
      160, 400); //shows the letters the player guesses 
    break;

  case 2:
    level2();
    fill(255);
    textSize(40);
    text(type, 
      160, 400); 
    break;

  case 3:
    level3();
    fill(255);
    textSize(40);
    text(type, 
      160, 400); 
    break;

  case 4:
    restart();
    break;
  }
}

void startMenu()
{
  background(0);

  // textFont(font, 30);
  fill(128, 128, 255);//lilac
  text("Welcome to Think Quik!", 100, 50);


  text("Press >s< to start the game", 100, 100);
  text("Instructions: Use the Keyboard to type letters", 0, 200);
  text("for your guesses and good luck!", 0, 230);
  text("Rules: Guess the word before time runs out", 0, 280);
  text("Hints are given for the word on each level", 0, 310);
}

void level1()
{
  background(0);
  fill(0);
  rect(80, 50, 400, 100);
  fill(255);
  textFont(font);
  textSize(25);
  text("Hint: Usually falls in Winter", 100, 100); // hint for the player to guess the word

  String word = "snow";
  String guess = "";

  //for (int i = 0; i < word.length(); i++)  {
  if (keyPressed)
  {
    if (i<word.length() && key == word.charAt(i)) // if the character’s key equals any letter in the word prints word to screen
    {

      int i_3=0;
      String t_=""; 
      for (char c : word.toCharArray()) { 
        if (i_3!=i) 
          t_ +=  type.charAt(i_3);
        else 
        t_ +=c;
        i_3++;
      }//for 

      type = t_;    // key;
      // println(type) ;
      i++;
    } else if (key=='\n')
    {
      type="";
    } // hitting enter results in the null pointer exception
  }

  // }
  guess += key + type;
  guess = guess.toUpperCase(); // letters stay on screen but only print one and if line 129-132 is removed multiple letters get printed

  fill(0);
  textFont(font);
  textSize(40);
  text(guess, 160, 250);

  for (int a =0; a < guess.length(); a++)
  {
    if (word.length() == type.length() && 
      word.charAt(0)==type.charAt(0)
      && word.charAt(1)==type.charAt(1)
      && word.charAt(2)==type.charAt(2)
      && word.charAt(3)==type.charAt(3))
    {
      gameLevel=2; // if the word is guessed then player moves to next level
      timer=5000;
      type="";
      i=0;
      delay(500);
    }
  }

  timer--;
  fill(255);
  textSize(25);
  text(timer, 500, 50);
}

void level2()
{
  background(0);

  fill(0);
  rect(80, 50, 400, 100);
  fill(255);
  textSize(25);
  text("Hint: Christmas Tree decoration", 100, 100); // hint for the player to guess the word

  String word = "baubles";
  String guess = ""; 
  int len= word.length();
  //for (int i = 0; i < word.length(); i++)  {
  if (keyPressed)
  {
    if (i<word.length() && key == word.charAt(i)) // if the character’s key equals any letter in the word prints word to screen
    {

      int i_3=0;
      String t_=""; 
      for (char c : word.toCharArray()) { 
        if (i_3!=i)
          len= len-i_3; // int length change implemented by Cian Burke.

        else 
        t_ +=c;
        i_3++;
        len = len-i_3;
      }//for 

      type += t_;    // key;
      //println(type);
      i++;
    }
  }//println(type);


  // }
  guess += key + type;
  guess = guess.toUpperCase(); 
  for (int a =0; a < guess.length(); a++)
  {
    if (word.length() == type.length() && 
      word.charAt(0)==type.charAt(0)
      && word.charAt(1)==type.charAt(1)
      && word.charAt(2)==type.charAt(2)
      && word.charAt(3)==type.charAt(3)
      && word.charAt(4)==type.charAt(4)
      && word.charAt(5)==type.charAt(5)
      && word.charAt(6)==type.charAt(6))
    {
      gameLevel=3; // if the word is guessed then player moves to next level
      timer=4000;
      type="";
      i=0;
      delay(500);
    }
  }
  fill(0);
  textSize(40);
  text(guess, 160, 250);

  if (timer == 0)
  {
    exit(); //if the timer runs out before player
  }

  timer--;
  fill(255);
  textSize(25);
  text(timer, 500, 50);
}

void level3()
{
  background(0);
  String word = "reindeer";
  String guess = ""; 
  int len= word.length();
  fill(0);
  rect(80, 50, 400, 100);
  fill(255);
  textSize(25);
  text("Hint: Santa's pets", 160, 100); // hint for the player to guess the word

  //for (int i = 0; i < word.length(); i++)  {
  if (keyPressed)
  {
    if (i<word.length() && key == word.charAt(i)) // if the character’s key equals any letter in the word prints word to screen and if its the same length
    {

      int i_3=0;
      String t_=""; 
      for (char c : word.toCharArray()) { 
        if (i_3!=i)
          len= len-i_3;

        else 
        t_ +=c;
        i_3++;
        len = len-i_3;
      }//for 

      type += t_;    // key;
      //println(type);
      i++;
    }
  }//println(type);


  // }
  guess += key + type;
  guess = guess.toUpperCase(); // letters stay on screen but only print one and if line 129-132 is removed multiple letters get printed
  for (int a =0; a < guess.length(); a++) // verifies to see if every guessed letter is correct
  {
    if (word.length() == type.length() && 
      word.charAt(0)==type.charAt(0)
      && word.charAt(1)==type.charAt(1)
      && word.charAt(2)==type.charAt(2)
      && word.charAt(3)==type.charAt(3)
      && word.charAt(4)==type.charAt(4) 
      && word.charAt(5)==type.charAt(5)
      && word.charAt(6)==type.charAt(6)
      && word.charAt(7)==type.charAt(7))
    {
      gameLevel=4; // player moves to restart()
      i=0;
    }
  }
  fill(0);
  textSize(40);
  text(guess, 160, 250);
  if (timer == 0)
  {
    exit();
  }

  timer--;
  fill(0);
  textSize(25);
  text(timer, 490, 50);
}

void restart() // when the game is won, screen pops up to prompt the player if they wih to play again.
{
  background(0);
  fill(0, 255, 0);
  textSize(50);
  text("Press >y< to play again", 50, 150);
  text("Press >n< to leave game", 50, 250);
  text("Congrats on winning", 50, 350);
  if (key == 'y')
  {
    gameLevel=1; //player returns to startMenu() to play again.
    type="____"; 
    delay(500);
  } else if (key == 'n')
  {
    exit(); // exits the game if they dont want to play again.
  }
}

You must set type to “____” at start of every level and have its length as the length of the word you have to guess.
Chrisir

its a mispelling on purpose

chesire

new version - see post above …

1 Like

thanks i’ll try that

it worked thank you, but why is that tho im wondering, is it cuz the global variable type starts as that

yes, “type” starts the way (before setup()) and we need to set it to this in restart() and also when starting a new level