基于文本的战斗机制 - Python

时间:2016-11-30 19:12:39

标签: python text-based

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

概述

它的工作方式是向用户呈现:他们的攻击(Att)+ HP(HP)的当前情况,以及具有更高智力(Inl)的人将首先出现。如果用户先行,他们会获得三种选择 - 攻击,吓跑和任何其他移动。在示例中,我使用了“等你的时间”。

对手(不是用户控制的)然后随机选择他们的任何3个动作之间的机会各不相同,然后执行它。然后它应该切换回用户转向,现在无论是否有比另一个更高的智力。

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

在代码中,“战斗”变量取决于战斗是否已启动。如果'battle'为1,则循环应该继续,但是如果它是0,那么它应该停止(在你把它吓跑之后添加0,或

问题:

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

  • 我是否误解了while循环实际上是如何工作的?

未来计划

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

  • 我还计划添加课程,这些课程最终会用特定课程的行动取代“等你的时间”。

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

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

2 个答案:

答案 0 :(得分:0)

我看到的主要问题是,当您说battle == 0时,您没有为变量battle指定0。您正在测试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

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

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

答案 1 :(得分:0)

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

    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变量。它会检查但不会改变。

应该在ifwhile语句中进行这些检查,因为您实际上正在检查该条件是否成立。