函数中的循环随机停止

发布于 2025-01-17 23:57:05 字数 3789 浏览 3 评论 0 原文

是的,我正在制作RPG,而战斗并不可靠...如果您能弄清楚为什么那会很棒!

#Enemy System
def MMEnemy(PL, HP, STR, DEF, SLTH, Name, CLASSNO):
    EnemyLvl = random.randint(PL, (PL * 2))
    EnemyHP = random.randint(EnemyLvl * 40, EnemyLvl * 80)
    Defend = False
    while HP >= 1 & EnemyHP >= 1:
        HPPercent = HP / PL
        HPPercent = int(HPPercent)
        if HPPercent >= 90:
            HealthBar = ("█████████▓  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 80 and HPPercent <= 89:
            HealthBar = ("████████▓░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 70 and HPPercent <= 79:
            HealthBar = ("███████▓░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 60 and HPPercent <= 69:
            HealthBar = ("██████▓░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 50 and HPPercent <= 59:
            HealthBar = ("█████▓░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 40 and HPPercent <= 49:
            HealthBar = ("████▓░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 30 and HPPercent <= 39:
            HealthBar = ("███▓░░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 20 and HPPercent <= 29:
            HealthBar = ("██▓░░░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 10 and HPPercent <= 19:
            HealthBar = ("█▓░░░░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 0 and HPPercent <= 9:
            HealthBar = ("▓░░░░░░░░░  |  ", HP, " / ",(100 * PL))
        else:
            HealthBar = "Unknown Health!!"
        print("The ", Name," is still standing...  (", HealthBar, " HP)")
        print("What should you do?")
        print("1: Attack\n2: Heal\n3: Defend\n4: Run")
        choice = input()
        choice = int(choice)
        if choice == 1:
            roll = random.randint(STR, STR + 2)
            print("You attack for ", roll)
            EnemyHP -= (roll)
            print(Name, " is at ", EnemyHP)
        elif choice == 2:
            roll = random.randint(PL * -2, PL * 5)
            if CLASSNO == 2:
                roll += PL * 3
                if roll >= 1:
                    print("You healed for ", roll)
                if roll <= 0:
                    print("You failed your heal [", roll," HP]")
                HP += roll
        elif choice == 3:
            Defend = True
        elif choice == 4:
            print("Run failed, not implemented yet")
        else:
            print("None were chosen, you stood still")
        DamageTaken = random.randint(5, EnemyLvl * 8)
        if Defend == True:
            prin("Your Defend was successful")
            Defend = False
        else:
            HP -= DamageTaken
            DamageTaken = str(DamageTaken)
            print("You got damaged for ", DamageTaken,"!")
        if HP <= 0:
            print("You have been defeated by the ", Name,"...")
            return "Lose"
        if EnemyHP <= 0:
            print("You defeated ", Name,"!")
            return "Win"
CLASSNO = 1
level = 1
Strength = 5
Defence = 5
Stealth = 5
HP = 100
GameEnd = False
battle = MMEnemy(level, HP, Strength, Defence, Stealth, "Irc", CLASSNO)
battle = str(battle)
if battle != "Win" or battle != "Lose":
    while battle != "Win" or battle != "Lose":
        print("Restarted")
        battle = MMEnemy(level, HP, Strength, Defence, Stealth, "Irc", CLASSNO)
        battle = str(battle)
        if battle == "Win":
            print("Battle Won")
            GameEnd = True
        elif battle == "Lose":
            print("Battle lost")
            GameEnd = True
        else:
            print("Nothing Worked")

香港专业教育学院尝试更改循环的标志并简化它,但似乎并没有做很多事情。它应该加载并让您战斗,但它停止了一半,而没有归还任何东西,所以它不会脱离循环

So yea, im making an RPG and the battle isnt too reliable... if you can figure out why then that would be great!

#Enemy System
def MMEnemy(PL, HP, STR, DEF, SLTH, Name, CLASSNO):
    EnemyLvl = random.randint(PL, (PL * 2))
    EnemyHP = random.randint(EnemyLvl * 40, EnemyLvl * 80)
    Defend = False
    while HP >= 1 & EnemyHP >= 1:
        HPPercent = HP / PL
        HPPercent = int(HPPercent)
        if HPPercent >= 90:
            HealthBar = ("█████████▓  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 80 and HPPercent <= 89:
            HealthBar = ("████████▓░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 70 and HPPercent <= 79:
            HealthBar = ("███████▓░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 60 and HPPercent <= 69:
            HealthBar = ("██████▓░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 50 and HPPercent <= 59:
            HealthBar = ("█████▓░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 40 and HPPercent <= 49:
            HealthBar = ("████▓░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 30 and HPPercent <= 39:
            HealthBar = ("███▓░░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 20 and HPPercent <= 29:
            HealthBar = ("██▓░░░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 10 and HPPercent <= 19:
            HealthBar = ("█▓░░░░░░░░  |  ", HP, " / ",(100 * PL))
        elif HPPercent >= 0 and HPPercent <= 9:
            HealthBar = ("▓░░░░░░░░░  |  ", HP, " / ",(100 * PL))
        else:
            HealthBar = "Unknown Health!!"
        print("The ", Name," is still standing...  (", HealthBar, " HP)")
        print("What should you do?")
        print("1: Attack\n2: Heal\n3: Defend\n4: Run")
        choice = input()
        choice = int(choice)
        if choice == 1:
            roll = random.randint(STR, STR + 2)
            print("You attack for ", roll)
            EnemyHP -= (roll)
            print(Name, " is at ", EnemyHP)
        elif choice == 2:
            roll = random.randint(PL * -2, PL * 5)
            if CLASSNO == 2:
                roll += PL * 3
                if roll >= 1:
                    print("You healed for ", roll)
                if roll <= 0:
                    print("You failed your heal [", roll," HP]")
                HP += roll
        elif choice == 3:
            Defend = True
        elif choice == 4:
            print("Run failed, not implemented yet")
        else:
            print("None were chosen, you stood still")
        DamageTaken = random.randint(5, EnemyLvl * 8)
        if Defend == True:
            prin("Your Defend was successful")
            Defend = False
        else:
            HP -= DamageTaken
            DamageTaken = str(DamageTaken)
            print("You got damaged for ", DamageTaken,"!")
        if HP <= 0:
            print("You have been defeated by the ", Name,"...")
            return "Lose"
        if EnemyHP <= 0:
            print("You defeated ", Name,"!")
            return "Win"
CLASSNO = 1
level = 1
Strength = 5
Defence = 5
Stealth = 5
HP = 100
GameEnd = False
battle = MMEnemy(level, HP, Strength, Defence, Stealth, "Irc", CLASSNO)
battle = str(battle)
if battle != "Win" or battle != "Lose":
    while battle != "Win" or battle != "Lose":
        print("Restarted")
        battle = MMEnemy(level, HP, Strength, Defence, Stealth, "Irc", CLASSNO)
        battle = str(battle)
        if battle == "Win":
            print("Battle Won")
            GameEnd = True
        elif battle == "Lose":
            print("Battle lost")
            GameEnd = True
        else:
            print("Nothing Worked")

Ive tried changing the flags for the loop and simplifying it but it doesnt seem to do much. Its supposed to load and put you into battle but it stops halfway without returning anything making it so it wont get out of the loop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三寸金莲 2025-01-24 23:57:05

想提出对代码的改进,并且在评论中发布代码时遇到了麻烦,因此:

hp_string = ""
if HPPercent > 0:
    full_hp = "█"
    current_hp = "▓"
    used_hp = "░"
    for i in range(int((HPPercent-HPPercent % 10)/10)):
        hp_string += full_hp
    hp_string += current_hp
    if HPPercent <= 90:
        for i in range(9 - int((HPPercent-HPPercent % 10)/10)):
            hp_string += used_hp
    HealthBar = (hp_string + " |  ", HP, " / ",(100 * PL))
else:
    HealthBar = "Unknown Health!!"

此外,正如评论中提到的某人,&amp; 不同>和在Python中。

Wanted to suggest an improvement to the code, and was having trouble posting the code in the comments, so here it is:

hp_string = ""
if HPPercent > 0:
    full_hp = "█"
    current_hp = "▓"
    used_hp = "░"
    for i in range(int((HPPercent-HPPercent % 10)/10)):
        hp_string += full_hp
    hp_string += current_hp
    if HPPercent <= 90:
        for i in range(9 - int((HPPercent-HPPercent % 10)/10)):
            hp_string += used_hp
    HealthBar = (hp_string + " |  ", HP, " / ",(100 * PL))
else:
    HealthBar = "Unknown Health!!"

Also, as someone mentioned in the comments, & is not the same as and in python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文