import random
#定义游戏的难度,生命值越多游戏越简单
difficulty = input('请选择游戏难易度(输入1、2、3):\n 1 容易 \n 2 正常 \n 3 难\n')
difficulty = int(difficulty)
if difficulty == 1:
lives = 12
elif difficulty == 2:
lives = 9
else:
lives = 6
#lives = 9 #游戏开始的时候,玩家有9条命
#初始化猜词游戏的词汇
words = ['pizza','fairy','teeth','shirt','otter','plane','brush','horse','light']
#随机选择一个神秘词,由玩家猜测
secret_word = random.choice(words)
print('提示:神秘但是前两位是'+secret_word[:2])
#神秘词长度
unknown_letters = len(secret_word)
#记录玩家猜测的词,默认用'?'表示,当有猜对一个词时,将'?'换成正确的词
clue = []
index = 0
while index < unknown_letters:
clue.append('?')
index = index + 1
#clue = list('?????') #用于保存线索的列表list
#心形符号
heart_symbol = u'\u2764' #用于显示心形符号,剩余的生命值
#玩家是否猜对
guessed_word_correctly = False #记录玩家是否猜对了结果
#更新玩家猜对的词,将'?'换成猜对的词
def update_clue(guessed_letter,secret_word,clue,unknown_letters):
index1 = 0
#循环神秘词
while index1 < len(secret_word):
#只要玩家输入的词在神秘词中,则判断猜对了,并将对应位的'?'赋值为该词
if guessed_letter == secret_word[index1]:
clue[index1] = guessed_letter
unknown_letters = unknown_letters - 1
index1 = index1+1
return unknown_letters
while lives>0:
print(clue)
print('剩余生命值:'+heart_symbol*lives)
guess = input('猜测并输入一个字母或者整个单词:')
if guess == secret_word:
guessed_word_correctly = True
break
if guess in secret_word:
unknown_letters = update_clue(guess,secret_word,clue,unknown_letters)
else:
print('错误,玩家丢失一条生命')
lives = lives-1
if unknown_letters == 0:
guessed_word_correctly = True
break
if guessed_word_correctly == True:
print('玩家赢了!神秘单词是:'+secret_word)
else:
print('玩家输了!神秘单词是:'+secret_word)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
《编程真好玩》Python基础实操代码.rar (8个子文件)
2Python基础
密码生成器03.py 959B
使用模块from_import.py 86B
密码生成器02.py 874B
单词九连猜02.py 2KB
单词九连猜01.py 1KB
密码生成器01.py 652B
使用模块from_import_as.py 150B
使用模块import.py 61B
共 8 条
- 1
资源评论
North_D
- 粉丝: 4115
- 资源: 261
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功