【问题标题】:Text-Based Battle mechanics - Python基于文本的战斗机制 - Python
【发布时间】:2016-11-30 19:12:39
【问题描述】:

最近,我和我的朋友开始在 Python 上为一款奇幻风格的游戏制作基于文本的战斗序列(在控制台中)。

概述

它的工作方式是向用户呈现:他们的攻击(Att)+ HP(HP)的当前情况,谁拥有更高的智力(Inl)谁先走。如果用户先走,他们将获得三个选项——攻击、吓跑和任何其他动作。在示例中,我使用了“Bide your time”。

对手(不受用户控制)然后在他们的任意 3 个动作之间随机选择,有不同的机会,然后执行。然后它应该切换回用户回合,现在不管一个人是否有智力比其他人高。

这应该继续交替,直到其中一个 HP = 0。如果用户击败对手,那么它应该可以选择吸收对手的统计/属性之一。

在代码中,'battle' 变量取决于战斗是否开始。如果'battle'是1,循环应该继续,但如果它是0,那么它应该停止(在你吓跑它之后添加0,或者

问题:

每当我运行代码时,无论发生什么,它都会继续循环,并且一直轮到我。

  • 我是否误解了 while 循环的实际工作原理?

未来计划

  • 我计划添加不同的种族(即矮人、精灵、兽人等),以平衡的方式改变用户的生命值、攻击力和智力。

  • 我还计划添加职业,最终将用特定职业的招式取代“等待你的时间”招式。

感谢您阅读并提前提出您提出的任何建议/修复!

import random
import time

Att = 10
Inl = 5
HP = 100

OpAtt = 10
OpInl = 4
OpHP = 100

FirstMove = 1
OpFirstMove = 0

battle = 1

while battle == 1:
    if Inl > OpInl:
        FirstMove == 1
        OpFirstMove == 0
    else:
        FirstMove == 0
        OpFirstMove == 1
    if FirstMove > OpFirstMove:
        print("Your Current stats are:")
        print("Attack:", Att)
        print("HP:", HP)
        time.sleep(2)
        print("Your Opponent's current stats are:")
        print("Attack:", OpAtt)
        print("HP:", OpHP)
        time.sleep(2)
        print("Your turn!")
        time.sleep(1)
    else:
        print("Your Current stats are:")
        print("Attack:", Att)
        print("HP:", HP)
        time.sleep(2)
        print("Your Opponent's current stats are:")
        print("Attack:", OpAtt)
        print("HP:", OpHP)
        time.sleep(2)
        print("Opponent's turn!")
        time.sleep(1)
    if FirstMove > OpFirstMove:
        print("Select a move:")
        Move1 = str(input(print("[A] Attack opponent\n[B] Bide your time\n[C] Try to scare your opponent off")))
        if Move1 == 'A' or Move1 == 'a':
            Hit1 = random.randint(1, 4)
            if Hit1 == 4 or 3 or 2:
                damageRand = random.randint(8, 12)
                OpHP = OpHP - damageRand
                print("Success!, Opponet's HP is now", OpHP, ".")
                FirstMove == 0
                OpFirstMove == 1
            else:
                print("Failure, no damage dealt.")
                FirstMove == 0
                OpFirstMove == 1
        elif Move1 == 'b' or Move1 == "B":
            print("You do nothing")
            FirstMove == 0
            OpFirstMove == 1
        elif Move1 == "c" or "C":
            Scare1 = random.randint(1, 7)
            if OpHP > 71:
                if Scare1 == 7 or 6:
                    print("Congratulations!, you scared the opponent away, however, this means you gained no loot.")
                    battle == 0
            elif OpHP < 70:
                if Scare1 == 7 or 6 or 5 or 4:
                    print("Congratulations!, you scared the opponent away, however, this means you gained no loot.")
                    battle == 0
            else:
                print("Failure, the opponent stands his ground")
                FirstMove == 0
                OpFirstMove == 1
    if OpFirstMove > FirstMove:
        OpMove1 = random.randint(1, 8)
        if OpMove1 == 1 or OpMove1 == 2 or OpMove1 == 3 or OpMove1 == 4 or OpMove1 == 5:
            OpHit1 = random.randint(1, 4)
            if OpHit1 == 4 or 3 or 2:
                damageRand = random.randint(8, 12)
                HP = HP - damageRand
                print("Your Opponent Attacks!, Your HP is now", HP, ".")
                FirstMove = 1
                OpFirstMove = 0
            else:
                print("Your Opponent failed to attack you, no damage received.")
                FirstMove = 1
                OpFirstMove = 0
        elif OpMove1 == 6:
            print("Your Opponent does nothing")
            FirstMove = 1
            OpFirstMove = 0
        elif OpMove1 == 7 or OpMove1 == 8:
            OpScare1 = random.randint(1, 7)
            if HP > 71:
                if OpScare1 == 7 or 6:
                    print("Aargh!, you were scared away, don't worry though, this has no bad affects.")
                    battle == 0
            elif HP < 70:
                if OpScare1 == 7 or 6 or 5 or 4:
                    print("Aargh!, you were scared away, don't worry though, this has no bad affects.")
                    battle == 0
            else:
                print("Your Opponent tried to scare you away, but failed")
                FirstMove = 1
                OpFirstMove = 0
else:
    print("Pies are tasty")


if OpHP < 1:
        print ("Congratulations! You deafeated your Opponent, which one of their stats would you like to absorb?")
        print("Your Opponent's current stats are:")
        print("Attack:", OpAtt)
        print("HP:", OpHP)
        print ("Intellect:", OpInl)
        absorb1 = print (str(input("[A] Intellect\n[B] Attack\n[C] HP\n[D] None of the above")))
        if absorb1 == "a" or absorb1 == "A":
            Inl == OpInl
            print ("Opponent's intellect absorbed")
        elif absorb1 == "b" or absorb1 == "B":
            Att == OpAtt
            print ("Opponent's Attack absorbed")
        elif absorb1 == "c" or absorb1 == "C":
            HP == OpHP
            print ("Opponent's HP absorbed")
        elif (absorb1 == "d" or absorb1 == "D"):
            print ("None of your Opponent's stats absorbed")
        print("Your current stats are:")
        print("Attack:", Att)
        print("HP:", HP)
        print ("Intellect:", Inl)
elif HP < 1:
    print ("Oh no! You were defeated by your Opponent!")
    battle == 0

【问题讨论】:

  • 请参阅How to ask a good question 和Creating a Minimal, Complete, and Verifiable Example。一般来说,包含代码的问题应该被缩小到只包含重现问题所需的最少量代码——比如说,工作不正常的特定函数(或该函数的几行),以及最少量的代码。使其运行所需的支持代码。
  • battle == 0 检查battle 是否为零。 battle = 0 将 battle 的值赋值为零
  • 您不要在input() 中放置打印语句。不确定这是否是错误的,但它是不对的......

标签: python text-based


【解决方案1】:

我看到的主要问题是当您说battle == 0 时,您并没有将0 分配给变量battle。您正在测试 battle 是否等于 0。因此,例如,当您说:

            if OpScare1 == 7 or 6 or 5 or 4:
                print("Aargh!, you were scared away, don't worry though, this has no bad affects.")
                battle == 0

最后你并没有改变battle的值,你只是随机检查它是否等于0。你需要改变所有你试图将变量分配给battle = 0的变量。这样的例子有很多。

但是,当您说 OpScare1 == 7 时,这是正确的,因为那是在您正在测试条件的 if 语句中。

【讨论】:

    【解决方案2】:

    总是轮到你,因为代码不会更改任何变量,它只会检查它们是否为真。

        if Inl > OpInl:
            FirstMove == 1
            OpFirstMove == 0
        else:
            FirstMove == 0
            OpFirstMove == 1
    

    这里如果 Inl 大于 OpInl 则不会将 FirstMove 更改为 1,它只检查 FirstMove 是否等于 1。 如果 Inl 为 1 且 OpInl 为 0,则检查 FirstMove 为 1 且 OpFirstMove 为 0。它不会更改这些值。

            if Move1 == 'A' or Move1 == 'a':
                Hit1 = random.randint(1, 4)
                if Hit1 == 4 or 3 or 2:
                    damageRand = random.randint(8, 12)
                    OpHP = OpHP - damageRand
                    print("Success!, Opponet's HP is now", OpHP, ".")
                    FirstMove == 0
                    OpFirstMove == 1
                else:
                    print("Failure, no damage dealt.")
                    FirstMove == 0
                    OpFirstMove == 1
            elif Move1 == 'b' or Move1 == "B":
                print("You do nothing")
                FirstMove == 0
                OpFirstMove == 1
            elif Move1 == "c" or "C":
                Scare1 = random.randint(1, 7)
                if OpHP > 71:
                    if Scare1 == 7 or 6:
                        print("Congratulations!, you scared the opponent away, however, this means you gained no loot.")
                        battle == 0
                elif OpHP < 70:
                    if Scare1 == 7 or 6 or 5 or 4:
                        print("Congratulations!, you scared the opponent away, however, this means you gained no loot.")
                        battle == 0
                else:
                    print("Failure, the opponent stands his ground")
                    FirstMove == 0
                    OpFirstMove == 1  
    

    这里也一样,因为它不会改变 FirstMove 也不会改变 OpFirstMove。它只是检查它是否是真的。所以所有FirstMove == 0, OpFirstMove == 1 和反之亦然所做的只是检查而不是更改同样适用于battle == 0 变量。它检查但没有改变。

    应该进行这些检查的唯一位置是在 if 和 while 语句中,因为您实际上是在检查该条件是否为真。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 2022-01-25
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 2015-09-27
      相关资源
      最近更新 更多