闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧湱鈧懓瀚崳纾嬨亹閹烘垹鍊為悷婊冪箻瀵娊鏁冮崒娑氬幈濡炪値鍘介崹鍨濠靛鐓曟繛鍡楃箳缁犳娊鏌嶈閸撴瑧绮诲澶婄?闂侇剙鍗曢崶顒夋晬婵犲﹤鎳愰悞濂告煟鎼搭垳绉甸柛瀣╃劍缁傚秴饪伴崼鐔哄幐闂佸憡鍔戦崝宀勫焵椤掑倹鏆鐐茬箻閸╁嫰宕樿缁犳艾顪冮妶鍡楀闁稿﹥娲熷鎼佸箣濠€垹閰e畷鎯邦檪闂婎剦鍓熼弻鐔碱敊閻e本鍣板銈冨灪濡啫鐣烽悢鐓幬╅柕澶堝€曢ˉ姘舵煟閻斿摜鐭婄紒缁樺浮瀹曟岸骞掗幘鍓佺槇濠殿喗锕╅崜娑㈩敇濞差亝鈷戦柟绋垮閻撱儵鏌涘Ο鑽ょ煉鐎规洘鍨块獮妯肩磼濡粯鐝栭梻渚€鈧偛鑻晶鎾煙椤曗偓缁犳牠寮幘缁樺亹闁肩⒈鍓﹂崥瀣繆閻愵亜鈧牕螞娴h鍙忛柕鍫濐樈閺佸﹪鏌¢崶銉ョ仾闁抽攱甯掗湁闁挎繂鎳忛幉鍝ョ磼婢跺苯鍔嬪ǎ鍥э躬椤㈡洟濮€閳ュ厖娣梻浣筋嚃閸犳岸宕戦妶澶婃瀬闁告劦鍠栫壕鍏兼叏濡潡鍝洪柣鎿勭秮濮婄粯鎷呴崫銉ㄥ┑鈽嗗亜濞硷繝骞冮悙鐑樻櫇闁稿本绋戞禍妤呮⒑闂堟侗妲撮柡鍛矒閹繝鎮㈤崗鑲╁幐闂佹悶鍎弲娑欑濠婂牊鐓冪憸婊堝礈濞嗗骏鑰块梺顒€绉撮悞鍨亜閹哄秷鍏岄柛鐔哥叀閺岀喖宕欓妶鍡楊伓     濠电姷鏁告慨鐑藉极閸涘﹥鍙忛柣鎴f閺嬩線鏌涘☉姗堟敾闁告瑥绻橀弻锝夊閻樺樊妫岄梺杞扮閿曨亪寮婚垾鎰佸悑閹肩补鈧磭顔愰梻鍌氬€搁崑鍡涘垂闁秴桅闁告洦鍨伴崘鈧梺闈浤涢崨顖氬笌缂傚倸鍊峰ù鍥╃礄娴兼潙纾规繝闈涱儏閽冪喖鏌ㄥ┑鍡╂Ч闁哄懏鐓¢弻娑樷槈閸楃偞鐏嶉梺鍦厴娴滃爼骞冨Δ鍐╁枂闁告洦鍓涢ˇ銊╂⒑缂佹ɑ鎯堢紒缁樼箓椤曪絾绻濆顓炰簻闁荤偞绋堥埀顒€鍘栨竟鏇炩攽閻愭潙鐏︽い蹇庡嵆楠炲鏁冮埀顒傚閸ф鐓涢柛銉㈡櫅閺嬫梻绱掗悩鑽ょ暫闁哄瞼鍠撻埀顒佺⊕宀e潡骞婇崘顔界厽闊洤锕ュ▍濠囨煛瀹€瀣М妤犵偞岣块埀顒勬涧閹诧繝宕氬☉銏♀拺闁告繂瀚﹢浼存煟閳哄﹤鐏﹂柣娑卞櫍瀹曞爼顢楅埀顒傜矆閸岀偞鐓曟繝闈涘閸旀粓鏌¢崨顓滃仮婵﹦绮幏鍛存惞閻熸壆顐奸梻浣烘嚀閹诧繝骞冮崒鐐偓渚€寮介妸銉х獮婵犵數濮寸€涒晝绱炴惔鈾€鏀介柣鎰级閳绘洖霉濠婂嫮鐭掓鐐村灴閹虫粓鎮欓柅娑氱泿闂備浇顫夊畷妯衡枖濞戞瑧顩锋繝濠傚暊閺€浠嬫煃閳轰礁鏆為柕鍥ㄧ箖閹便劍绻濋崨顕呬哗闂佺懓寮堕幐鍐茬暦閻旂⒈鏁囬柣鏃偳归弲鎼佹⒑鐠囧弶鍞夋い顐㈩槸鐓ら柣鏂捐荡缂傛氨鎲搁弮鍫涒偓浣割潩鐠轰綍銊╂煥閺傚灝鈷旈柣锕€鐗撳娲箹閻愭彃濮岄梺鍛婃煥缁夋挳鍩㈠澶婎潊闁靛牆妫岄幏娲煟閻樺厖鑸柛鏂胯嫰閳诲秹骞囬悧鍫㈠幍闂佸憡鍨崐鏍偓姘炬嫹

45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:Python开发简单hangman游戏的实例代码分享

Python开发简单hangman游戏的实例代码分享

2015-07-21 20:13:54 来源:www.45fan.com 【

Python开发简单hangman游戏的实例代码分享

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

#!/usr/bin/env python
import random 
import cPickle 
class Hangman(object):
 '''A simple hangman game that tries to improve your vocabulary a bit '''
 def __init__(self):
  # the variables used, this is not necessary
  self.dumpfile = ''  #the dictionary file
  self.dictionary = {}  #the pickled dict
  self.words = []   #list of words used
  self.secret_word = '' #the 'key'
  self.length = 0   #length of the 'key'
  self.keys = []   #inputs that match the 'key'
  self.used_keys = []  #keys that are already used
  self.guess = ''   #player's guess
  self.mistakes = 0  #number of incorrect inputs
  return self.load_dict()
 #insert some random hints for the player
 def insert_random(self, length):
  randint = random.randint
  # 3 hints
  if length >= 7: hint = 3
  else: hint = 1
  for x in xrange(hint):
    a = randint(1, length - 1)
    self.keys[a-1] = self.secret_word[a-1]
 def test_input(self):
  #if the guessed letter matches
  if self.guess in self.secret_word:
   indexes = [i for i, item in enumerate(self.secret_word) if item == self.guess]
   for index in indexes:
    self.keys[index] = self.guess
    self.used_keys.append(self.guess)
    print "used letters ",set(self.used_keys),'\n'
  #if the guessed letter didn't match
  else:
   self.used_keys.append(self.guess)
   self.mistakes += 1
   print "used letters ",set(self.used_keys),'\n'
 # load the pickled word dictionary and unpickle them 
 def load_dict(self):
  try :
   self.dumpfile = open("~/python/hangman/wordsdict.pkl", "r")
  except IOError:
   print "Couldn't find the file 'wordsdict.pkl'"
   quit()
  self.dictionary = cPickle.load(self.dumpfile)
  self.words = self.dictionary.keys()
  self.dumpfile.close()
  return self.prepare_word()
 #randomly choose a word for the challenge
 def prepare_word(self):
  self.secret_word = random.choice(self.words)
  #don't count trailing spaces
  self.length = len(self.secret_word.rstrip())
  self.keys = ['_' for x in xrange(self.length)]
  self.insert_random(self.length)
  return self.ask()
 #display the challenge
 def ask(self):
  print ' '.join(self.keys), ":", self.dictionary[self.secret_word] 
  print 
  return self.input_loop()
 #take input from the player
 def input_loop(self):
  #four self.mistakes are allowed
  chances = len(set(self.secret_word)) + 4   
  while chances != 0 and self.mistakes < 5:
   try:
    self.guess = raw_input("> ")
   except EOFError:
    exit(1)
   self.test_input()
   print ' '.join(self.keys)
   if '_' not in self.keys:
    print 'well done!'
    break
   chances -= 1
  if self.mistakes > 4: print 'the word was', ''.join(self.secret_word).upper()
  return self.quit_message()
 def quit_message(self):
  print "\n"
  print "Press 'c' to continue, or any other key to quit the game. "
  print "You can always quit the game by pressing 'Ctrl+D'"
  try:
   command = raw_input('> ')
   if command == 'c': return self.__init__() #loopback
   else : exit(0)
  except EOFError: exit(1)
if __name__ == '__main__':
 game = Hangman()
 game.__init__()

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

本文地址:http://www.45fan.com/a/question/15258.html
Tags: 实现 python 简单
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部