如何从函数返回值并在while循环中引用它

时间:2015-06-19 20:21:47

标签: python

基本上我想从healthcheck函数返回一个值,这样我就可以阻止小宠物攻击彼此了。你会看到我遗漏了“if”行的其余部分,因为我不确定要放在这里。我需要一种从healthcheck函数返回函数的方法,但是我无法使return语句起作用。

class enemy():
    global enemylives
    global dead
    dead=0
    enemylives=10
    def healthcheck():
        if enemylives<=0:
        print("You defeated the enemy!")


while True:
               time.sleep(1)
               enemy1.attacked()
               enemy1.healthcheck()
               if 
                   break;
               time.sleep(1)
               squirtle.attacked()
               squirtle.healthcheck()

(这不是我的全部代码)

1 个答案:

答案 0 :(得分:2)

不确定游戏的逻辑是什么,但你应该创建属性并避免使用全局,使用实例属性和方法。如果你的生命值达到0,你可以使用sys.exit退出循环。

Attack successful, enemy  life decreased by 10  to 90
Attack unsuccessful, enemy increases life to 100
Attack successful, enemy  life decreased by 10  to 90
Attack successful, enemy  life decreased by 10  to 80
Attack successful, enemy  life decreased by 10  to 70
Attack successful, enemy  life decreased by 10  to 60
Attack successful, enemy  life decreased by 10  to 50
Attack unsuccessful, enemy increases life to 60
Attack unsuccessful, enemy increases life to 70
Attack successful, enemy  life decreased by 10  to 60
Attack successful, enemy  life decreased by 10  to 50
Attack successful, enemy  life decreased by 10  to 40
Attack successful, enemy  life decreased by 10  to 30
Attack successful, enemy  life decreased by 10  to 20
Attack unsuccessful, enemy increases life to 30
Attack successful, enemy  life decreased by 10  to 20
Attack successful, enemy  life decreased by 10  to 10
Attack successful, enemy  life decreased by 10  to 0
You defeated the enemy!

示例输出:

class Enemy():
    def __init__(self,diff):
        self.dead = 0
        self.life = 100
        self.diff = diff

    def attack(self):
        if random() > self.diff:
            self.life -= 10
            print("Attack successful, enemy  life decreased by 10  to {}".format(self.life))
        elif self.life <= 90:
            self.life += 10
            print("Attack unsuccessful, enemy increases life to {}".format(self.life))

    def health_check(self):
        if self.life <= 0:
            print("You defeated the enemy!")
            sys.exit()


enemy1 = Enemy(.20) 

你也可以制作一个难度属性,通过增加或减少你对random()的比较来设置成功攻击的几率:

class Enemy():
    def __init__(self,diff):
        self.dead = 0
        self.life = 100
        self.diff = diff

    def attack(self):
        if random() > self.diff:
            self.life -= 10
            print("Attack successful, enemy  life decreased by 10  to {}".format(self.life))
        elif self.life <= 90:
            self.life += 10
            print("Attack unsuccessful, enemy increases life to {}".format(self.life))


    def health_check(self):
        if self.life <= 0:
            print("You defeated the enemy!")
            return True
        return False

enemy1 = Enemy(.3)
while True:
        time.sleep(1)
        enemy1.attack()
        if enemy1.health_check():
             break
        time.sleep(1)

如果您想要暂停,请返回方法并使用if:

进行检查
declare @iter int
set @iter = 1