Game Development Reference
In-Depth Information
9 - Hangman
return 'yes'.startswith('y')
return True
The point of the playAgain() function is to let the player type in yes or no to tell our
program if they want to play another round of Hangman. If the player types in YES, then
the return value of input() is the string 'YES' . And 'YES'.lower() returns the
lowercase version of the attached string. So the return value of 'YES'.lower() is
'yes' .
But there's the second method call, startswith('y') . This function returns True if
the associated string begins with the string parameter between the parentheses, and False
if it doesn't. The return value of 'yes'.startswith('y') is True .
Now we have evaluated this expression! We can see that what this does is let the player
type in a response, we lowercase the response, check if it begins with the letter 'y' or 'Y' ,
and then return True if it does and False if it doesn't. Whew!
On a side note, there is also a endswith(someString) string method that will
return True if the string ends with the string in someString and False if it doesn't.
Review of the Functions We Defined
That's all the functions we are creating for this game!
— getRandomWord(wordList) will take a list of strings passed to it as a
parameter, and return one string from it. That is how we will choose a word for the
player to guess.
— displayBoard(HANGMANPICS, missedLetters, correctLetters,
secretWord) will show the current state of the board, including how much of the
secret word the player has guessed so far and the wrong letters the player has
guessed. This function needs four parameters passed to work correctly.
HANGMANPICS is a list of strings that hold the ASCII art for each possible hangman
board. correctLetters and missedLetters are strings made up of the letters
that the player has guessed that are in and not in the secret word. And secretWord
is the secret word the player is trying to guess. This function has no return value.
— getGuess(alreadyGuessed) takes a string of letters the player has already
guessed and will keep asking the player for a letter that is a letter that he hasn't
already guessed. (That is, a letter that is not in alreadyGuessed . This function
returns the string of the acceptable letter the player guessed.
— playAgain() is a function that asks if the player wants to play another round of
Hangman. This function returns True if the player does and False if the player
 
Search WWH ::




Custom Search