N3r0_H4x0r_EvilSec

hangman python made by StingV

Sep 25th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. made by StingV Member Of EvilSecurity
  2.  
  3.  
  4. import random
  5. import winsound
  6.  
  7. HANGMANPICS = ['''
  8.  
  9. +---+
  10. | |
  11. |
  12. |
  13. |
  14. |
  15. =========''', '''
  16.  
  17. +---+
  18. | |
  19. O |
  20. |
  21. |
  22. |
  23. =========''', '''
  24.  
  25. +---+
  26. | |
  27. O |
  28. | |
  29. |
  30. |
  31. =========''', '''
  32.  
  33. +---+
  34. | |
  35. O |
  36. /| |
  37. |
  38. |
  39. =========''', '''
  40.  
  41. +---+
  42. | |
  43. O |
  44. /|\ |
  45. |
  46. |
  47. =========''', '''
  48.  
  49. +---+
  50. | |
  51. O |
  52. /|\ |
  53. / |
  54. |
  55. =========''', '''
  56.  
  57. +---+
  58. | |
  59. O |
  60. /|\ |
  61. / \ |
  62. |
  63. =========''']
  64. words = 'ant baboon badger bat bear beaver camel cat clam cobra cougar coyote crow deer dog donkey duck eagle ferret fox frog goat goose hawk lion lizard llama mole monkey moose mouse mule newt otter owl panda parrot pigeon python rabbit ram rat raven rhino salmon seal shark sheep skunk sloth snake spider stork swan tiger toad trout turkey turtle weasel whale wolf wombat zebra'.split()
  65.  
  66. def PlaySoundFile( filename, options):
  67. print( "playing " + filename)
  68. winsound.PlaySound( filename, winsound.SND_FILENAME | options)
  69.  
  70. def getRandomWord(wordList):
  71. # This function returns a random string from the passed list of strings.
  72. wordIndex = random.randint(0, len(wordList) - 1)
  73. return wordList[wordIndex]
  74.  
  75. def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord):
  76. print(HANGMANPICS[len(missedLetters)])
  77. print()
  78.  
  79. print('Missed letters:', end=' ')
  80. for letter in missedLetters:
  81. print(letter, end=' ')
  82. print()
  83.  
  84. blanks = '_' * len(secretWord)
  85.  
  86. for i in range(len(secretWord)): # replace blanks with correctly guessed letters
  87. if secretWord[i] in correctLetters:
  88. blanks = blanks[:i] + secretWord[i] + blanks[i+1:]
  89.  
  90. for letter in blanks: # show the secret word with spaces in between each letter
  91. print(letter, end=' ')
  92. print()
  93.  
  94. def getGuess(alreadyGuessed):
  95. # Returns the letter the player entered. This function makes sure the player entered a single letter, and not something else.
  96. while True:
  97. print('Guess a letter.')
  98. guess = input()
  99. guess = guess.lower()
  100. if len(guess) != 1:
  101. print('Please enter a single letter.')
  102. elif guess in alreadyGuessed:
  103. print('You have already guessed that letter. Choose again.')
  104. elif guess not in 'abcdefghijklmnopqrstuvwxyz':
  105. print('Please enter a LETTER.')
  106. else:
  107. return guess
  108.  
  109. def playAgain():
  110. # This function returns True if the player wants to play again, otherwise it returns False.
  111. print('Do you want to play again? (yes or no)')
  112. return input().lower().startswith('y')
  113.  
  114. PlaySoundFile("coolshitBra", winsound.SND_LOOP | winsound.SND_ASYNC)
  115.  
  116. print('H A N G M A N')
  117. missedLetters = ''
  118. correctLetters = ''
  119. secretWord = getRandomWord(words)
  120. gameIsDone = False
  121. while True:
  122. displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
  123.  
  124. # Let the player type in a letter.
  125. guess = getGuess(missedLetters + correctLetters)
  126.  
  127. if guess in secretWord:
  128. correctLetters = correctLetters + guess
  129.  
  130. # Check if the player has won
  131. foundAllLetters = True
  132. for i in range(len(secretWord)):
  133. if secretWord[i] not in correctLetters:
  134. foundAllLetters = False
  135. break
  136. if foundAllLetters:
  137. print('Yes! The secret word is "' + secretWord + '"! You have won!')
  138. gameIsDone = True
  139. else:
  140. missedLetters = missedLetters + guess
  141.  
  142. # Check if player has guessed too many times and lost
  143. if len(missedLetters) == len(HANGMANPICS) - 1:
  144. displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord)
  145. print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"')
  146. gameIsDone = True
  147.  
  148. # Ask the player if they want to play again (but only if the game is done).
  149. if gameIsDone:
  150. if playAgain():
  151. missedLetters = ''
  152. correctLetters = ''
  153. gameIsDone = False
  154. secretWord = getRandomWord(words)
  155. else:
  156. print ('Just Shut Down Game And Music Will Stop!')
  157. break
Advertisement
Add Comment
Please, Sign In to add comment