当前位置:Gxlcms > Python > Python实现的简单hangman游戏实例

Python实现的简单hangman游戏实例

时间:2021-07-01 10:21:17 帮助过:13人阅读

本文实例讲述了Python实现的简单hangman游戏。分享给大家供大家参考。具体如下:

  1. #!/usr/bin/env python
  2. import random
  3. import cPickle
  4. class Hangman(object):
  5. '''A simple hangman game that tries to improve your vocabulary a bit '''
  6. def __init__(self):
  7. # the variables used, this is not necessary
  8. self.dumpfile = '' #the dictionary file
  9. self.dictionary = {} #the pickled dict
  10. self.words = [] #list of words used
  11. self.secret_word = '' #the 'key'
  12. self.length = 0 #length of the 'key'
  13. self.keys = [] #inputs that match the 'key'
  14. self.used_keys = [] #keys that are already used
  15. self.guess = '' #player's guess
  16. self.mistakes = 0 #number of incorrect inputs
  17. return self.load_dict()
  18. #insert some random hints for the player
  19. def insert_random(self, length):
  20. randint = random.randint
  21. # 3 hints
  22. if length >= 7: hint = 3
  23. else: hint = 1
  24. for x in xrange(hint):
  25. a = randint(1, length - 1)
  26. self.keys[a-1] = self.secret_word[a-1]
  27. def test_input(self):
  28. #if the guessed letter matches
  29. if self.guess in self.secret_word:
  30. indexes = [i for i, item in enumerate(self.secret_word) if item == self.guess]
  31. for index in indexes:
  32. self.keys[index] = self.guess
  33. self.used_keys.append(self.guess)
  34. print "used letters ",set(self.used_keys),'\n'
  35. #if the guessed letter didn't match
  36. else:
  37. self.used_keys.append(self.guess)
  38. self.mistakes += 1
  39. print "used letters ",set(self.used_keys),'\n'
  40. # load the pickled word dictionary and unpickle them
  41. def load_dict(self):
  42. try :
  43. self.dumpfile = open("~/python/hangman/wordsdict.pkl", "r")
  44. except IOError:
  45. print "Couldn't find the file 'wordsdict.pkl'"
  46. quit()
  47. self.dictionary = cPickle.load(self.dumpfile)
  48. self.words = self.dictionary.keys()
  49. self.dumpfile.close()
  50. return self.prepare_word()
  51. #randomly choose a word for the challenge
  52. def prepare_word(self):
  53. self.secret_word = random.choice(self.words)
  54. #don't count trailing spaces
  55. self.length = len(self.secret_word.rstrip())
  56. self.keys = ['_' for x in xrange(self.length)]
  57. self.insert_random(self.length)
  58. return self.ask()
  59. #display the challenge
  60. def ask(self):
  61. print ' '.join(self.keys), ":", self.dictionary[self.secret_word]
  62. print
  63. return self.input_loop()
  64. #take input from the player
  65. def input_loop(self):
  66. #four self.mistakes are allowed
  67. chances = len(set(self.secret_word)) + 4
  68. while chances != 0 and self.mistakes < 5:
  69. try:
  70. self.guess = raw_input("> ")
  71. except EOFError:
  72. exit(1)
  73. self.test_input()
  74. print ' '.join(self.keys)
  75. if '_' not in self.keys:
  76. print 'well done!'
  77. break
  78. chances -= 1
  79. if self.mistakes > 4: print 'the word was', ''.join(self.secret_word).upper()
  80. return self.quit_message()
  81. def quit_message(self):
  82. print "\n"
  83. print "Press 'c' to continue, or any other key to quit the game. "
  84. print "You can always quit the game by pressing 'Ctrl+D'"
  85. try:
  86. command = raw_input('> ')
  87. if command == 'c': return self.__init__() #loopback
  88. else : exit(0)
  89. except EOFError: exit(1)
  90. if __name__ == '__main__':
  91. game = Hangman()
  92. game.__init__()

希望本文所述对大家的Python程序设计有所帮助。

人气教程排行