Help with a project (game to guess words)

ahhhhhhh ok so im supposed to go type="______" because the next word is baubles? I wanted to leave the word blank for the second level to give it a little mystery for the player, i got someone to test it out and they took awhile before they guessed the right word.

OKAY!!!

Then it’s " " but not ""

ok thank you, ill change it now

here…



int timer= 6000; // timer for game
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();
    getMenuKeys();   
    break;

  case 1:
    level1();
    break;

  case 2:
    level2();

    break;

  case 3:
    level3();
    break;

  case 4:
    restart();
    break;
  }
}

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

void getMenuKeys() {
  if (key == ' ')
  {
    gameLevel=1;

    // level1(); // NO
  } else 
  {
    gameLevel=0;
  }
}

void startMenu()
{
  background(0);

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


  text("Press >Space Bar< 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 = "";


  //println(type);
  fill(255);
  textSize(40); 
  text(type, 
    160, 400); //shows the letters the player guesses 

  if (keyPressed) {
    for (int i = 0; i < word.length(); i++) {
      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++;
      }
    }
  }

  // }
  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))
    {
      fill(255, 0, 0); 
      text("Hit space", 111, 311); 
      if (keyPressed&&key==' ') {
        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 = ""; 

  fill(255);
  textSize(40);
  text(type, 
    160, 400); 

  if (keyPressed) {
    for (int i = 0; i < word.length(); i++) {
      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++;
      }
    }
  }


  //println(type);


  // }
  guess = key + type;
  guess = guess.toUpperCase(); 

  // int count =0;
  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))
    {
      // WON !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      fill(255, 0, 0); 
      text("Hit space", 111, 311); 
      if (keyPressed&&key==' ') {
        // if the word is guessed then player moves to next level
        gameLevel=3;
        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 = ""; 

  fill(255);
  textSize(40);
  text(type, 
    160, 400); 

  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

  if (keyPressed) {
    for (int i = 0; i < word.length(); i++) {
      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++;
      }
    }
  }


  // }
  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))
    {
      // WON !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      fill(255, 0, 0); 
      text("Hit space", 111, 311); 
      if (keyPressed&&key==' ') {
        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 level1 to play again.
    type="____";
    delay(500);
  } else if (key == 'n')
  {
    exit(); // exits the game if they dont want to play again.
  }
}
//

Thank you so much, i was sure that i had nearly everything you did in mine and i couldnt figure out what was missing thanks again for your help

1 Like

this is not so nice…

instead you can make a further screen in draw() where it says:

You Lost

Try again? y/n

Then restart or exit();

thanks for the suggestion so that means i have a void lose() so if timer==0 gameLevel=5; and have the play again or not? because thats the way im understanding it unless i could be wrong

1 Like

That’s what I meant, yeah…

after lose gamelevel would be 1 I guess

hmmm i suppose so thanks

1 Like

when using if(keyPressed) , a for loop , an if(key == char) statement and a letter counter why might the letter counter count multiple times. for instance when u press ‘n’ it should count 1 instead it counts 5 and then when u press another key it counts 6.

yeah…

We haven’t discussed this

the variable keyPressed registers multiple times

As long as you hold the key

As opposed to the function keyPressed() of the same name which registers only once

The way we build this program, it doesn’t matter up to now

But in theory the function is better

1 Like

ahh ok thank you because i didn’t understand at first why my counter kept repeating but i found a solution

1 Like