玩家PK敌人的小游戏3.0版本。提示:(1)随机生成初始血量100-150,随机生成攻击力30-50。(2)为了避免瞬间出现结果,在适当的地方插入延迟语句。

根据图一功能描述,分多个版本完成小游戏,最终版本可以选择多盘游戏。最终版本运行效果如图二所示。

提示:

(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

代码运行结果:

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风起晨曦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值