簡體   English   中英

Python在while循環中重復隨機整數

[英]Python repeat random integer in while loop

我正在嘗試為我正在制作的基於文本的RPG游戲編寫玩家和暴徒攻擊,我已經設置了玩家和暴徒的機會和暴擊機會,但我無法弄清楚如何每次我為他們獲取一個新的整數重啟循環,它使用與第一次進入循環時相同的整數。

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

使用像

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM