Game Development Reference
In-Depth Information
9 - Hangman
82. print(letter, end=' ')
83. print()
84.
85. def getGuess(alreadyGuessed):
86. # Returns the letter the player entered. This function
makes sure the player entered a single letter, and not
something else.
87. while True:
88. print('Guess a letter.')
89. guess = input()
90. guess = guess.lower()
91. if len(guess) != 1:
92. print('Please enter a single letter.')
93. elif guess in alreadyGuessed:
94. print('You have already guessed that letter.
Choose again.')
95. elif guess not in 'abcdefghijklmnopqrstuvwxyz':
96. print('Please enter a LETTER.')
97. else:
98. return guess
99.
100. def playAgain():
101. # This function returns True if the player wants to
play again, otherwise it returns False.
102. print('Do you want to play again? (yes or no)')
103. return input().lower().startswith('y')
104.
105.
106. print('H A N G M A N')
107. missedLetters = ''
108. correctLetters = ''
109. secretWord = getRandomWord(words)
110. gameIsDone = False
111.
112. while True:
113. displayBoard(HANGMANPICS, missedLetters,
correctLetters, secretWord)
114.
115. # Let the player type in a letter.
116. guess = getGuess(missedLetters + correctLetters)
117.
118. if guess in secretWord:
119. correctLetters = correctLetters + guess
120.
121. # Check if the player has won
122. foundAllLetters = True
123. for i in range(len(secretWord)):
124. if secretWord[i] not in correctLetters:
125. foundAllLetters = False
126. break
127. if foundAllLetters:
128. print('Yes! The secret word is "' + secretWord
+ '"! You have won!')
129. gameIsDone = True
Search WWH ::




Custom Search