【问题标题】:Python repeat random integer in while loopPython在while循环中重复随机整数
【发布时间】:2019-03-07 22:14:21
【问题描述】:

我正在尝试为我正在制作的基于文本的 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

【问题讨论】:

  • 我想可能会重复这个:stackoverflow.com/questions/33806022/…
  • 不是 100% 确定您的要求,但假设 roll.playercrit 和 roll.playerhitchance 是变量,要获得一个新的随机数,您需要在该部分代码再次生成。例如。 roll.playerhitchance = random.randint()

标签: python python-3.x


【解决方案1】:

使用类似的函数

import random
for x in range(10):
    print random.randint(1,101)

为最多 100 人使用一个数组,并生成随机数字以添加到您的代码中。

您还可以使用数组创建随机数字结构,然后在创建时使用要随机排列和添加的数字

from random import *

items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
shuffle(items)
print items

【讨论】:

    【解决方案2】:

    问题在于您的roll_dice 课程。您在类初始化时定义了值,但您再也不会更新它们。因此self.escapeself.spawn 在程序启动后将始终是相同的值。无需大量重写即可解决问题的最简单方法是在每次您想掷骰子时创建另一个 roll_dice() 实例。比如:

    ### 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() # 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.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 ###
    

    【讨论】:

      猜你喜欢
      • 2015-12-12
      • 2016-12-29
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多