根据图一功能描述,分多个版本完成小游戏,最终版本可以选择多盘游戏。最终版本运行效果如图二所示。
提示:
(1)随机生成初始血量100-150,随机生成攻击力30-50。随机数产生方法:import random,random.randint(100, 150)
(2)为了避免瞬间出现结果,在适当的地方插入延迟语句。延迟方法:import time,time.sleep(1.5)
图一(各版本功能描述):
图二(3.0版本运行效果图):
import time
import random
game_player_score = 0
foe_score = 0
while True:
for i in range(1, 4):
print(f" \n——————现在是第 {i} 局——————")
game_player_life = random.randint(100, 150)
game_player_attack = random.randint(30, 50)
foe_life = random.randint(100, 150)
foe_attack = random.randint(30, 50)
print(f"【玩家】\n血量:{game_player_life}\n攻击:{game_player_attack}")
print("-"*23)
time.sleep(1.5) # 让程序暂停2秒钟
print(f"【敌人】\n血量:{foe_life}\n攻击:{foe_attack}")
print("-" * 23)
time.sleep(1.5)
while game_player_life >= 0 and foe_life >= 0:
foe_life -= game_player_attack
print(f"你发起了攻击,【敌人】剩余血量:{foe_life}")
time.sleep(1.5)
if foe_life <= 0:
print(f"敌人无力反击,【玩家】的血量剩余:{game_player_life}")
break
game_player_life -= foe_attack
print(f"敌人发起了攻击,【玩家】的血量剩余:{game_player_life}")
print("-"*23)
time.sleep(1.5)
if game_player_life > 0 and foe_life <= 0:
print("敌人死翘翘了,你赢了!")
game_player_score += 1
elif game_player_life <= 0 and foe_life > 0:
print("你死翘翘了,敌人赢了!")
foe_score += 1
elif game_player_life == 0 and foe_life == 0:
print("一起死翘翘了!")
time.sleep(1.5)
if game_player_score > foe_score:
print("最终结果:你赢了!")
elif game_player_score < foe_score:
print("最终结果:敌人赢了!")
elif game_player_score == foe_score:
print("打成平手。")
time.sleep(1.5)
choose = input("要继续游戏吗,请输入n继续,输入其余退出:")
if choose == "n":
print("欢迎进入游戏!")
continue
else:
print("游戏结束啦!")
break
代码运行结果: