加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python在while循环中重复随机整数

发布时间:2020-12-17 17:39:05 所属栏目:Python 来源:网络整理
导读:我正在尝试为正在制作的基于文本的RPG游戏编写对玩家和暴民的攻击代码,为玩家和暴民的出勤率和暴击率设置了randomint,但我不知道每次我如何为他们获取新的整数重新启动循环,它使用的是它第一次进入循环时获得的整数. ### GAME VALUES ###class roll_dice: de

我正在尝试为正在制作的基于文本的RPG游戏编写对玩家和暴民的攻击代码,为玩家和暴民的出勤率和暴击率设置了randomint,但我不知道每次我如何为他们获取新的整数重新启动循环,它使用的是它第一次进入循环时获得的整数.

### GAME VALUES ###
class roll_dice:
    def __init__(self):
        self.spawn = random.randint(1,100)
        self.escape = random.randint(1,100)
        self.playercrit = random.randint(1,100)
        self.playerhitchance = random.randint(1,100)
        self.mobcrit = random.randint(1,100)
        self.mobhitchance = random.randint(1,100)
roll = roll_dice()    


### ORC SPAWN ###
if fight_walk.lower() == 'fight':
    orcMobSpawn()
    while True:
        fight_orc = input(">>> ")
        if fight_orc.lower() == 'a':

            ### PLAYER ATTACK ###
            while True:
                roll.playercrit
                roll.playerhitchance
                if roll.playercrit <= 10 and roll.playerhitchance >= 6:
                    print("You crit orc for",str(userPlayer.atk * 2),"damage!")
                    orcMob.hp = orcMob.hp - userPlayer.atk * 2
                    print("Orc HP:",orcMob.hp)
                    break
                elif roll.playercrit >= 11 and roll.playerhitchance >= 6:
                    print("You hit orc for",str(userPlayer.atk),"damage!")
                    orcMob.hp = orcMob.hp - userPlayer.atk
                    print("Orc HP:",orcMob.hp)
                    break
                elif roll.playercrit >= 11 and roll.playerhitchance <= 5:
                    print("You missed!")
                    break
                elif roll.playercrit <= 10 and roll.playerhitchance <= 5:
                    print("You missed!")
                    break
                elif orcMob.hp <= 0 and userPlayer.hp >= 1:
                    print("Your HP:",str(userPlayer.hp))
                    print("You win!")
                    break
                elif userPlayer.hp <= 0:
                    print("You died!")
                    exit()

            ### ORC ATTACK ###
            while True:
                roll.mobcrit
                roll.mobhitchance
                if orcMob.hp <= 0 and userPlayer.hp >= 1:
                    break
                if roll.mobcrit <= 5 and roll.mobhitchance >= 25:
                    print("nOrc crit for",str(orcMob.atk * 2),"damage!")
                    userPlayer.hp = userPlayer.hp - orcMob.atk * 2
                    print("Your HP:",str(userPlayer.hp))
                    break
                elif roll.mobcrit >= 5 and roll.mobhitchance >= 25:
                    print("nOrc hit for",str(orcMob.atk),"damage!")
                    userPlayer.hp = userPlayer.hp - orcMob.atk
                    print("Your HP",str(userPlayer.hp))
                    break
                elif roll.mobcrit <= 5 and roll.mobhitchance <= 25:
                    print("Orc missed!")
                    print("Your HP:",str(userPlayer.hp))
                    break
                elif roll.mobcrit >= 5 and roll.mobhitchance <= 25:
                    print("Orc missed!")
                    print("Your HP:",str(userPlayer.hp))
                    break
        if orcMob.hp <= 0 and userPlayer.hp >= 1:
            break
        elif orcMob.hp >= 1:
            continue
最佳答案
问题出在您的roll_dice类上.您具有在类初始化时定义的值,但以后再也不会更新它们.因此,self.escape或self.spawn在程序启动后将始终是相同的值.解决问题而无需大量重写的最简单方法是,每次要掷骰子时都要制作另一个roll_dice()实例.就像是:

### GAME VALUES ###
class roll_dice:
    def __init__(self):
        self.spawn = random.randint(1,100)
# roll = roll_dice() # you don't need to make an instance here


### ORC SPAWN ###
if fight_walk.lower() == 'fight':
    orcMobSpawn()
    while True:
        fight_orc = input(">>> ")
        if fight_orc.lower() == 'a':

            ### PLAYER ATTACK ###
            while True:
                roll = roll_dice() # make a new instance with each loop
                roll.playercrit
                roll.playerhitchance
                if roll.playercrit <= 10 and roll.playerhitchance >= 6:
                    print("You crit orc for",str(userPlayer.hp))
                    print("You win!")
                    break
                elif userPlayer.hp <= 0:
                    print("You died!")
                    exit()

            ### ORC ATTACK ###

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读