前言
游戏介绍
词库
引用
全局变量
随机抽取一个单词
hangman的图像
开始
打印字母
输入
再来一局?
主程序(假的)
运行
总结
前言
最近学习到python的游戏编程快速上手的第8章,敲了一会儿后发现不对劲,对着书上的代码复制了一遍,发现书上的代码是错的(第5版好像改过来了),所以就把自己写的代码发出来,一起讨论一下,也请各位佬佬佬不吝赐教,指出后辈啰嗦的代码= =
游戏介绍Hangman直译为“上吊的人”,是一个猜单词的双人游戏。由一个玩家想出一个单词或短语,另一个玩家猜该单词或短语中的每一个字母 。第一个人抽走单词或短语,只留下相应数量的空白与下划线。
出题玩家一般会画一个绞刑架,当猜词的玩家猜出了短语中存在的一个字母时,出题的玩家就将这个字母存在的所有位置都填上。如果玩家猜的字母不在单词或短语中,那么出题的玩家就给绞刑架上小人添上一笔,直到7笔(本次是6笔,因为我没看规则)过后,游戏结束。
Hangman_百度百科
词库来自https://nostarch.com/inventwithpython#diff
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()
引用
import random
import sys
import time
全局变量
因为才刚入门,显得整体代码看起来很乱,大家一定要扎实基础呀
# 字符区
player_guess = '' # 玩家的猜想
chess_word = "" # 给出的随机单词
already_guessed = [] # 玩家已经输入的单词
player_guess_right = [] # 玩家已经猜对的字母
guess_wait = '_' * len(chess_word)
# 数字区
time_guess = 0 # 猜的次数
chess_words_num = 0 # 给出单词的号码
# 单词大小写
low_word = 'a b c d e f g h i j k l m n o p q r s t u v w x y z'.split()
up_word = 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'.split()
# 单词库
ture = ['y', 'yes', 'YES', 'Y']
随机抽取一个单词
# 从words中输出一个随机的单词(√)
def chess_words():
global chess_word
global chess_words_num
chess_words_num = int(random.randint(0, len(words) - 1))
chess_word = words[chess_words_num]
hangman的图像
# 用字符串输出来表现动画(根据time_guess来判断次数,最高6次,之后结束)(√)
def hangman():
if time_guess == 0:
print(f'''
+---+
|
|
|
===
''')
pass
elif time_guess == 1:
time.sleep(0.2)
print(f'''
+---+
O |
|
|
===
''')
pass
elif time_guess == 2:
time.sleep(0.2)
print(f'''
+---+
O |
| |
|
===
''')
pass
elif time_guess == 3:
time.sleep(0.2)
print(f'''
+---+
O |
/| |
|
===
''')
pass
elif time_guess == 4:
time.sleep(0.2)
print(f'''
+---+
O |
/|\ |
|
===
''')
elif time_guess == 5:
time.sleep(0.2)
print(f'''
+---+
O |
/|\ |
/ |
===
''')
else:
time.sleep(0.2)
print(f'''
+---+
O |
/|\ |
/ \ |
===
''')
开始
# 游戏开始前的询问(√)
def begin():
print()
print(' H A N G M A N ')
print(' 欢迎来到这里,你准备好了吗?(yes or no): ', end='')
ready = input()
# 询问个人名字
if str(ready) in ture:
print(' 你准备好了,请告诉我你的名字: ', end='')
name = input()
time.sleep(0.3)
print(' 噢,你的名字叫', name, ', 很高兴来到 H A N G M A N , 期待你的发挥! ')
time.sleep(0.5)
ok = True
if ok:
print(' 是否需要游戏说明?(yes or no): ', end='')
explain = input()
# 询问是否需要说明书
while explain in ture:
print('HANGMAN发源于一款两人铅笔游戏,旨在猜出单词并救下将要被绞刑的人,而你只有六次机会,把握每一次机会,游戏即将开始,加油!')
time.sleep(1)
break
else:
print(' 你还没准备好吗?期待我们下次的见面! ')
sys.exit()
打印字母
# 需要显示的字母打印出来(√)
def out_word(guess_word):
global guess_wait, chess_word
match_index = list(filter(lambda x: x[1] == guess_word,
enumerate(list(chess_word))))
guess_wait = list(guess_wait)
for i in match_index:
guess_wait[i[0]] = guess_word
guess_wait = ''.join(guess_wait)
print(' ', guess_wait)
这里我的方法有三种,但只有这种方法是最优解,另外两个都不能达到理想的效果(烦了一晚上)
输入# 依次输入所猜的字母(√)
def enter():
global player_guess
global time_guess
global already_guessed
global player_guess_right
global guess_wait
print(chess_word)
guess_wait = '_' * len(chess_word)
hangman()
print(' ', guess_wait)
print(" 请输入一个字母: ", end='')
while True:
time.sleep(0.5)
if time_guess < 6:
player_guess = input()
print(' 您输入的字母为: ', player_guess.lower())
if 1 != len(player_guess):
time.sleep(0.2)
print(' 输入不规范,请重新输入! ')
time.sleep(0.3)
print(' 请输入一个字母: ', end='')
elif player_guess in low_word or up_word:
if player_guess in str(chess_word):
if player_guess in already_guessed:
print(' 重复输入,请再次输入! ', end='')
else:
time.sleep(1)
print(" 恭喜你,猜对了! ")
player_guess_right.append(player_guess)
already_guessed.append(player_guess)
hangman()
out_word(player_guess)
time.sleep(0.5)
word = str(guess_wait)
if word == chess_word:
print(f' 恭喜你成功救下了他,这个单词是 {chess_word} !')
break
else:
print(" 请接着输入一个字母:", end='')
already_guessed.append(player_guess)
else:
if player_guess in already_guessed:
print(' 重复输入,请再次输入! ', end='')
else:
time.sleep(1)
print(" 哦不,这个字母并不在猜的单词内! ")
already_guessed.append(player_guess)
time_guess = time_guess + 1
hangman()
time.sleep(0.5)
if time_guess < 6:
print(" 请重试: ", end='')
already_guessed.append(player_guess)
else:
pass
else:
time.sleep(1)
print(" 输入不规范,请重新输入! ")
time.sleep(0.5)
print(" 请输入一个字母: ", end='')
else:
print(' 输入错次数已满! ')
print(' 哦不,他被绞死了! ')
print(' 任 务 失 败 ')
break
因为函数学得 *** 之过急,更多喜欢用修改全局变量,这是坏习惯,给朋友一顿猛笑😄
再来一局?# 询问是否需要再来一局(3)
def again():
x = input(' 游戏结束了,是否需要再来一把(yes/no): ')
if str(x) in ture:
game()
else:
print(' 期待下一次见面!')
sys.exit()
主程序(假的)
# 主程序
def game():
chess_words()
enter()
again()
这样也可以循环运行了,现在看这段代码我真的笑死
运行if __name__ == '__main__':
chess_words()
begin()
enter()
again()
总结
其实学下来感觉python果然不能只能对着书本敲敲代码,去试试解题什么的确实很好玩,加油吧
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)