gpt4 book ai didi

Python RPG 困惑

转载 作者:行者123 更新时间:2023-12-01 01:00:10 25 4
gpt4 key购买 nike

我的代码有问题,当调用时,它似乎使战斗持续的时间比应有的时间长。

我认为问题出在 if 语句上,所以我一直在研究它们,但我似乎无法把它弄对。

class Enemy:
def __init__(self, Name, HP, ATK, DEF, Damage):
self.Name = Name
self.HP = HP
self.ATK = ATK
self.DEF = DEF
self.Damage = Damage

def attack(attacker, attackee): # The attack function
hit = random.randint(min_roll, max_roll) + attacker.ATK

if (hit > attackee.DEF):
print(attacker.Name, "inflicts", attacker.Damage)

attackee.HP = attackee.HP - attacker.Damage

if attackee.HP <= 0: # if the attackee's health drops below zero
print("With a forceful attack,", attackee.Name, "dies.")
else:
print(attackee.Name, "has", attackee.HP, "HP remaining.")
else:
print("You missed. Better defend!")


def fight(attacker, enemy): # The attack loop takes in two enemy objects
while(attacker.HP >= 0 and enemy.HP >=0):
if attacker.HP >= 0 and enemy.HP >= 0:
attack(attacker, enemy)
else:
print("You're dead")

if enemy.HP >= 0 and attacker.HP >= 0:
attack(enemy, attacker)
else:
print("The enemy is dead")

theClass= Enemy("warrior", 10, 4, 5, 5)
skeleton1 = Enemy("The Skeleton", 10, 4, 5, 5) # This creates a new Skeleton enemy. The order is the Name, HP, ATK, DEF, and Damage.

fight(theClass, skeleton1)

输出应该在其中一个角色死亡时停止,并且每个角色每次攻击只能攻击一次。由于某种原因,当我这次运行代码时,最后的攻击让战士在骷髅死亡之前跑了三遍。

我也发现有时它也能正常工作。不一致的结果是不行的。谢谢!

最佳答案

你想让他们在 0 hp 时继续攻击吗?当您将检查更改为 ifenemy.HP > 0 andattacker.HP > 0 时,输出是什么?

此外,在发现其中一个已死亡的子句中放置一个 return 语句可能会有所帮助;这样,你就可以确定,一旦其中一人死亡,战斗就会结束。

关于Python RPG 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55870505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com