变量不更新

2024-06-01 08:53:56 发布

您现在位置:Python中文网/ 问答频道 /正文

所以这是我想做的一个游戏的模型。我的问题发生在68-75行之间。由于某些原因,NMHP或mobHP没有更新。谢谢你的帮助

我想要的是:

这个函数应该是“怪物之气”(mchi)小于“玩家之气”(phi)取怪物的生命值(mobHP)并从中减去玩家的伤害,称之为NMHP。然后我想更新函数外面的列表。你知道吗

##ranbat.py
##make random battle for Player

##import random
##import time
##


def mobspawn():
    #imports
    import random
    import time
    #list,tup, and dicts
    plHP = ["30", "40", "50"]
    gchi = [".5", ".6", ".7", ".8", ".9"]
    pchi = [".5", ".6", ".7", ".8", ".9"]
    mchi = [".5", ".6", ".7", ".8", ".9"]
    dam = ["10", "20", "30", "40", "50", "60",]
    enList = ["Slime", "Goblin", "Hound", "NyaaTrape", "Navi"]
    enHP = ["10", "20", "30", "40", "50", "60",]
    enGen = ["he", "She", "it"]
    chiC = ["1", "2"]
    HELP = [("#", "Name", "Description"),("1", "Attack", "Attacks your enemy with equipped weapon"),("2", "Defend", "Use your equipped weapon to defend"),("3", "Heal", "If player has Med_Kit uses it on player"),("3", "Stats", "Prints players stats"),("5", "Finisher", "A secret technique (Has a 50% chance of a insta-kill)"),("6", "HELP", "This comand displays Help")]
    ########
    player = "Oni"
    ##############
    #Mob stats   #
    ##############
    Gen = random.choice(enGen)
    mobHP = random.randint(10, 60)
    mob = random.choice(enList)
    mdam = random.randint(10, 60)
    mchi = random.randint(1, 9)
    NMHP = 0
    mstat = [("HP","NMHP","Damage", "Chi"),(mobHP, NMHP, mdam, mchi)]
    ##############      
    time.sleep(1)
    print("Vofa: Oh no whats that!,", player, "protect me")
    print("")
    time.sleep(3)
    #print("here 2 ")
    print("You have incountered a wild", mob)
    print("")
    time.sleep(3)
    print("Vofa: Looks like", Gen, "has",mobHP , "HP. Best be careful.")
    print("")
    ##############
    #Player stats#
    ##############
    plHP = random.randint(10, 60)
    pldam = random.randint(10, 60)
    pchi = random.randint(1, 9)
    plstat = [("HP","Damage", "Chi"),(plHP, pldam, pchi)]
    ##############
    batMenDis()       
    player=int(input())
    while player !=None:
        player = int(input())

        if player == 1:
            print("You have chosen option #1 Attack")
            print("You Attack")
            time.sleep(3)
            if (mchi) < (pchi):
                (NMHP) = (mobHP) - (pldam)
                (mobHP) = (NMHP)
                if mobHP < 0:
                    print( mob, "is dead")
                    break
                else:
                    print(mob,"Has",mobHP,"HP left")
            if (mchi) > (pchi):
                (NpHP) = (plHP) - (mdam)
                (pHP) = (NpHP)
                if plHP < 0:
                    print("you're almost dead. but you muster the strength to fight for a bit longer to keep vofa safe")
                    (pHP) = (pHP) + random.randint(1, 10)
                else:
                    print(player,"Has",plHP,"HP left")
            if (mchi) == (pchi):
                print("Both you and mob have the same amount of chi")
                time.sleep(1)
                print("")
                print("Coin has been tossed")
                chiC = random.choice(chiC)
                if chiC == 1:
                    mobHP = (mobHP) - (pldam)
                    print(mob,"Has",mobHP,"HP left")
                if chiC == 2:
                    pHP = (plHP) - (mdam)
                    print(player,"Has",plHP,"HP left")
        batMenDis()
        if player == 0:
            #print("here 2")
            print("You have chosen option number 0")
            print("Program Now Exiting")
            time.sleep(2)
            print("Good Bye. Have a nice day! hope to fight with you again")
            break
def batMenDis():
    print("")
    print("What are you going to do")
    print("")
    print("1 = Attack")
    print("2 = Defend")
    print("3 = Heal")
    print("4 = Print current Stats")
    print("5 = Finisher (50% chance of working)")
    print("6 = HELP")
def main():
    mobspawn()
main()

Tags: iftimesleeprandomhpplayermobprint
1条回答
网友
1楼 · 发布于 2024-06-01 08:53:56

问题不在第68-75行之间:

if (mchi) < (pchi):
    (NMHP) = (mobHP) - (pldam)
    (mobHP) = (NMHP)
    if mobHP < 0:
        print( mob, "is dead")
        break
    else:
        print(mob,"Has",mobHP,"HP left")

因为当你进入那里时,你的伤害总是大于怪物的生命值(生命值是1-9,伤害值是10-60)。结果总是怪物被杀死。你知道吗

但如果我们看看另一个分支:

if (mchi) > (pchi):
    (NpHP) = (plHP) - (mdam)
    (pHP) = (NpHP) # error here, should be plHP
    if plHP < 0:
        print("you're almost dead. but you muster the strength to fight for a bit"
              "longer to keep vofa safe")
        (pHP) = (pHP) + random.randint(1, 10) # same here
    else:
        print(player,"Has",plHP,"HP left")

你不是从plHP中减去伤害,而是从pHP中减去伤害(在那之前还没有定义)。这样,plHP始终保持不变。你知道吗

相关问题 更多 >