“吊死鬼”游戏其实时一个猜字游戏,很多人应该都玩过,大概规则是:一位玩家先选择一个单词,然后对应着单词的每个字母按照字母的个数画几条线,然后轮到其他玩家来猜这个词,玩家每次只能猜一个字母,如果猜的字母不在单词里,那么失去一些生命值,如果在单词里就在对应的线下面把字母写下来。
用python来实现这个游戏,第一步,先要设置几个单词,然后让程序选择一个单词
import random words = ["chicken", "dog", "cat", "mouse", "frog"] #设置需要猜的词 def pick_a_word(): word_position = random.randint(0, len(words) - 1) #随机数--单词所在的位置 return words[word_position] #选择单词 print(word_position)
关于random.randint(a, b),这个函数用于生成一个指定范围内的整数。其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
第二步,要让程序识别输入字母,然后判断是否正确,代码如下:
import random words = ["chicken", "dog", "cat", "mouse", "frog"] #设置需要猜的词 lives_remaining = 14 #设置血量 def pick_a_word(): word_position = random.randint(0, len(words) - 1) return words[word_position] def play(): word = pick_a_word() while True: guess = get_guess(word) if process_guess(guess, word): print('You win ! Well Done ! ') break if lives_remaining == 0: print('You are Hung! ') print('The word was: ' + word) break def get_guess(word): return 'a' def process_guess(guess, word): global lives_remaining lives_remaining = lives_remaining - 1 return False play()
来分析一下这段代码,首先定义一个列表来储存所要猜的单词,函数pick_a_word
从列表中随机抽取一个单词,get_guess
函数模拟玩家一直在猜“a”,process_guess假设玩家一直在猜错,这样lives_remaining就会减一,然后返回False,这样很快就用掉了14次机会
一步一步来,来修改一下get_guess函数完善一下程序
def get_guess(word): print('继续输入') print('剩余的机会还有' + str(lives_remaining) + '次') guess = input('输入一个字母或者整个单词: ') return guess
内建函数input
用来把参数作为提示符,并输出显示,然后返回用户输入的值,python2里为raw_input
下一步,创建一个print_word
函数,用来显示一些类似m--u-e这样的信息,并修改一下之前的process_guess
函数
guessed_letters = '' def print_word(word): display_word = '' for letter in word: if guessed_letters.find(letter) > -1: #字母在单词里 display_word = display_word + letter else: display_word = display_word + '-' print(display_word) def process_guess(guess, word): global lives_remaining global guessed_letters lives_remaining = lives_remaining - 1 guessed_letters = guessed_letters + guess return False
内建函数find()
,用来见此这个字母是否在guess_letters当中,如果不在里面,返回 -1 ,否则返回字母的位置
分析一下,首先添加一个全局变量 guessed_letters
,每次输入的字母都会添加到这个字符串中,然后在print_word函数中定义空字符串display_word,如果输入单词正确,就会添加到这个字符串中,否则添加“-”,把到现在写的程序整合一下,然后运行一下:
现在看起来已经有模有样了,不过还要继续完善一下,现在还不能判断输赢
玩家输入猜的单词时可以有两种情况,一、输入单个字母,二、输入整个单词,创建两个函数,分别处理输入单个字母和输入整个单词两种情况
def whole_word_guess(guess, word): global lives_remaining if guess.lower() == word.lower(): #把输入的单词和需要猜的单词都转换为小写 return True else: lives_remaining = lives_remaining - 1 return False def single_letter_guess(guess, word): global guessed_letters global lives_remaining if word.find(guess) == -1: lives_remaining = lives_remaining - 1 guessed_letters = guessed_letters + guess if all_letters_guessed(word): return True def all_letters_guessed(word): for letter in word: if guessed_letters.find(letter) == -1: return False return True
分析一下两个函数,第一个whole_word_guess()
用来处理用户输入整个单词,先把单词和输入单词转小写,如果输入单词和要猜的单词一致,返回真,否则次数 -1 ,返回假;第二个函数single_letter_guess()
用来处理单个字母输入,首先查找guess是否存在于word里面,如果没有,次数-1,如果存在,不执行if语句里面的逻辑,把输入的字母存入guessed_letters,然后执行all_letters_guessed
函数,这个函数的作用是用来查看guess_letters是否匹配word。
接下来就该实现最后一步了,修改函数process_guess
:
def process_guess(guess, word): if len(guess) > 1: return whole_word_guess(guess, word) else: return single_letter_guess(guess, word)
当玩家输入的单词个数大于 1 时,认为输入的为单词,调用whole_word_guess()
否则认为输入的为字母,调用single_letter_guess(),
不存在输入多个单词的情况,否则就没意思了。
整体代码:
#hangman.py __author__ = 'guhao' import random words = ["chicken", "dog", "cat", "mouse", "frog"] #设置需要猜的词 lives_remaining = 14 #设置吊死鬼的血量,也就时一共可以猜错几次 guessed_letters = '' def play(): word = pick_a_word() while True: guess = get_guess(word) if process_guess(guess, word): print('你赢了 ! ') break if lives_remaining == 0: print('你被吊死了! ') print(''' ------------- | | | O | /|\ | | | / \ | =================== ''') print('这个单词是: ' + word) break def process_guess(guess, word): if len(guess) > 1: return whole_word_guess(guess, word) else: return single_letter_guess(guess, word) def pick_a_word(): word_position = random.randint(0, len(words) - 1) return words[word_position] def get_guess(word): print_word(word) print('剩余的机会还有' + str(lives_remaining) + '次') guess = input('输入一个字母或者整个单词: ') return guess def print_word(word): display_word = '' for letter in word: if guessed_letters.find(letter) > -1: #字母在单词里 display_word = display_word + letter else: display_word = display_word + '-' print(display_word) def whole_word_guess(guess, word): global lives_remaining if guess.lower() == word.lower(): #把输入的单词和需要猜的单词都转换为小写 return True else: lives_remaining = lives_remaining - 1 return False def single_letter_guess(guess, word): global guessed_letters global lives_remaining if word.find(guess) == -1: lives_remaining = lives_remaining - 1 guessed_letters = guessed_letters + guess if all_letters_guessed(word): return True def all_letters_guessed(word): for letter in word: if guessed_letters.find(letter) == -1: return False return True play()
大概输出: