在小游戏中轮换攻击(python 2.7)

在小游戏中轮换攻击(python 2.7),python,Python,我一直在学习python,我正在制作一个小游戏,到目前为止做得很好。我需要给每个玩家一次进攻机会,首先是代码: from random import randint class Dragon: def __init__(self,name,health): self.name = name self.health = health def kill(self,target): while target.health >= 0

我一直在学习python,我正在制作一个小游戏,到目前为止做得很好。我需要给每个玩家一次进攻机会,首先是代码:

from random import randint
class Dragon:
    def __init__(self,name,health):
        self.name = name
        self.health = health
    def kill(self,target): 
        while target.health >= 0:
            if target.health <= 50:
                hit = randint(1,20)
                target.health -= hit
                print '%s hits %s for %s' %(self.name,target.name,hit)
            elif target.health <= 0:
                print '%s has killed %s' %(self.name,target.name)
            else:
                hit = randint(1,50)
                if hit >= 40:
                    target.health -= hit
                    print "%s critically hits %s for %s" %(self.name,target.name,hit)
                else:
                    target.health -= hit
                    print "%s hits %s for %s" %(self.name,target.name, hit)
    def Dead(self):
        if self.health <= 0:
            print "%s is dead" %self.name
class Hero:
    def __init__(self,name,health):
        self.name = name
        self.health = health
Diet = Dragon('Diet',200)
Pizza = Hero('Pizza', 100)
if __name__ == '__main__':
    Diet.kill(Pizza)
来自随机导入randint
龙类:
定义初始(自我、姓名、健康状况):
self.name=名称
自我健康
def杀伤(自身、目标):
当target.health>=0时:

如果target.health将
kill
方法从while循环中移除,那么每次调用该方法时,它都只是一次攻击,而不是直到其他人死亡

然后将整个游戏放置在一个while循环中,并为结束条件设置一些哨兵值,例如:

GameIsRunning = True
while GameIsRunning:
    #heros action
    #enemies action
    if EndConditionIsMet():
        GameIsRunning = False
在每次循环结束时,检查游戏是否结束,例如是否所有敌人都死了或英雄都死了,并将
GameIsRunning
设置为false

编辑:

无效选项1 按原样,
Dead
方法只打印一条消息。除了向用户显示某某已经死了之外,你不能使用它。但是如果你想这样做,可以在攻击者
kill
方法中的某个地方调用它。或者在游戏循环中。如果您希望在实例死亡时自动调用它,那么我将在一个方法中总结任何造成的损害:

def DealDamage(self, damage):
    self.health -= damage
    self.Dead()
然后使用

def Kill(self, target):
    #damage picking logic stays the same
    target.DealDamage(damage)
无效选项2 如果您想让said
Dead
方法表示事情发生或不发生,它应该有一个布尔签名,例如:

def IsDead(self):
    # Stick in a display message here if you like, or let the game do it
    if self.health <= 0:
        return True
    else:
        return False

这种方法也为你提供了一种在游戏中产生更多敌人的简单方法。

你应该将你的
杀死方法分解为更小的方法。当你有几个小的棋子而不是一个大的棋子时,你会更容易发现什么东西没有按你期望的方式工作。
如果是真的
和一个
破发
在每个玩家的动作后都应该有一个检查。如果英雄杀死了龙,龙就不会得到回合。但是你不应该因此退出游戏循环。在这个版本之后,您可能需要添加更多的敌人,这迫使您重新编写逻辑。你可以在每次行动前进行检查,或者在每个人身上执行逻辑,这样如果他们死了,他们的行动毫无意义。我想的是,一旦生命值达到0或以下,敌人的死亡方法应该被返回,以确保循环将被打破,并在未来添加更多功能,如战利品或通电,以确保我不需要重复这些代码请参阅我的编辑。我个人更喜欢第二种方式。
if Hero.IsDead() or len(Enemies) == 0:
    return True