1. 理清楚游戏思路
实现功能:2个洞穴选择,一个洞穴是好龙,一个洞穴是坏龙,坏龙可以概率屠龙或者概率逃跑选项(后续难度需要增加宝藏获取装备,随机遇见商人,随着游戏进度逐步减少屠龙概率)
2. 计数和函数导入
import random,time
game_dict={}
game_dict['Treasure_number']=0# 宝藏个数
game_dict['Challenge_draGon_number'] = 0 # 屠龙次数
3. 构造输入验证
def Double_choice(a,b,hint):
'''双选择验证函数
:param a 第一个选项值
:param b 第二个选项值
:param hint 选项信息
:return 返回输入值'''
choice=''
while choice.lower() !=a and choice.lower() !=b:
print(hint)
choice=input()
return choice
4. 构造概率计算
def probability_calculation(success_probability):
'''概率计算
:param success_probability 成功概率
:return True 成功 False 失败'''
pro_list=[]
failure_probability=100-success_probability
for i in range(success_probability):
pro_list.append(True)
for i in range(failure_probability):
pro_list.append(False)
random.shuffle(pro_list)
return random.choice(pro_list)
5. 游戏开始提示
def game_info():
'''游戏开始提示函数'''
print('欢迎来到龙的国度游戏')
print('在你面前你看见二个洞穴')
print('一个洞穴里龙很友好他会和你分享他的宝藏')
print('而另一个洞穴龙贪婪又饥饿,眨眼间就能把你吃掉')
6. 游戏结束提示
def game_over():
'''游戏结束提示函数'''
print('饥饿的龙吃了你...')
if game_dict['Challenge_dragon_number']:
print('饥饿的龙为%d个兄弟报仇雪恨' % game_dict['Challenge_dragon_number'])
if game_dict['Treasure_number']:
print('饥饿的龙获得%d个宝藏'%game_dict['Treasure_number'])
else:
print('饥饿的龙表示你很穷...')
print('Game Over')
7. 屠龙概率计算
def kill_dragon(kill_probability):
'''屠龙概率计算
:param kill_probability 屠龙概率
:return True 屠龙成功 False 屠龙失败'''
if probability_calculation(kill_probability):
game_dict['Challenge_dragon_number'] += 1
print('屠龙成功! (成功屠杀巨龙%d次)'% game_dict['Challenge_dragon_number'])
return True
else:
print('屠龙失败的你倒在了血泊中....')
return False
8. 逃跑概率计算
def run_away(escape_probability):
'''逃跑概率计算
:param escape_probability 逃跑概率
:return True 逃跑成功 False 逃跑失败'''
if probability_calculation(escape_probability):
print('你成功逃跑了...')
return True
else:
print('逃跑失败只能屠龙....')
return False
9. 构造好龙选项
def good_dragon():
game_dict['Treasure_number'] += 1
print('友善的龙送给你了一个宝藏(成功获取宝藏%d次)' % game_dict['Treasure_number'])
10. 构造坏龙选项
def bad_dragon():
'''坏的选项
:return True 屠龙成功,False屠龙失败'''
print('饥饿的巨龙眼冒绿光的看着你....')
kill_probability = random.randint(1, 100) # 生成屠龙概率
escape_probability = random.randint(1, 100) # 生成逃跑概率
message = '你有%d%%的概率屠龙,%d%%的机率逃跑(1屠龙 or 2逃跑)' % (kill_probability, escape_probability)
bad_choice = Double_choice('1', '2', message)
if bad_choice == '1':
if kill_dragon(kill_probability):
return True
elif bad_choice == '2':
if not run_away(escape_probability):
if kill_dragon(kill_probability):
return True
else:
return True
return False
11. 核心洞穴选择判断
def game_start():
'''核心判断洞穴'''
while True:
message='请问你要进那个洞穴(1 or 2)'
chosenCave = Double_choice('1','2',message)
print('你慢慢的走进洞穴中.....')
time.sleep(1)
print('一条巨龙出现在了你面前....')
time.sleep(1)
friendlyCave = random.randint(1, 2)
if chosenCave == str(friendlyCave):
good_dragon()
else:
if not bad_dragon():
break
game_over()
12. 程序外壳
def game_shell():
'''外壳程序'''
game_info()
game_start()
while True:
message='你想在玩一次吗(Y or N)'
again_flag=Double_choice('y','n',message)
if again_flag=='n':
break
game_start()
13. 调用执行函数
game_shell()
0