Game Development Reference
In-Depth Information
9 - Hangman
107. missedLetters = ''
108. correctLetters = ''
109. secretWord = getRandomWord(words)
110. gameIsDone = False
Line 106 is the first actual line that executes in our game. We start by assigning a blank
string for missedLetters and correctLetters , because the player has not guessed
any missed or correct letters yet. Then we call getRandomWord(words) , where words
is a variable with the huge list of possible secret words we assigned on line 59. The return
value of getRandomWord(words) is one of these words, and we save it to the
secretWord variable. Then we also set a variable named gameIsDone to False . We
will set gameIsDone to True when we want to signal that the game is over and the
program should ask the player if they want to play again.
Setting the values of these variables is what we do before the player starts guessing letters.
Displaying the Board to the Player
112. while True:
113. displayBoard(HANGMANPICS, missedLetters,
correctLetters, secretWord)
The while loop's condition is always True , which means we will always loop forever
until a break statement is encountered. We will execute a break statement when the game
is over (either because the player won or the player lost).
Line 113 calls our displayBoard() function, passing it the list of hangman ASCII art
pictures and the three variables we set on lines 107, 108, and 109. Program execution moves
to the start of displayBoard() at line 66. Based on how many letters the player has
correctly guessed and missed, this function displays the appropriate hangman board to the
player.
Letting the Player Enter Their Guess
115. # Let the player type in a letter.
116. guess = getGuess(missedLetters + correctLetters)
If you look at our flow chart, you see only one arrow going from the "Show the board and
the blanks to the player." box to the "Ask a player to guess a letter." box. Since we have
already written a function to get the guess from the player, let's call that function. Remember
that the function needs all the letters in missedLetters and correctLetters
combined, so we will pass as an argument a string that is a concatenation of both of those
strings. This argument is needed by getGuess() because the function has code to check if
the player types in a letter that they have already guessed.
Search WWH ::




Custom Search