简体   繁体   English

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

[英]How to return a value from a function and reference it in a while loop

Basically I want to return a value from the healthcheck function so I can stop the pokemons attacking eachother. 基本上,我想从运行状况检查功能返回一个值,这样我就可以阻止宠物小精灵互相攻击。 You will see I have left out the rest of the "if" line, as I am unsure what to put here. 您将看到我遗漏了“ if”行的其余部分,因为我不确定要在此处添加什么。 I need a way to return a function from the healthcheck function, but I can't get the return statement to work. 我需要一种从运行状况检查函数返回函数的方法,但是我无法使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()

(This isn't my whole code) (这不是我的全部代码)

Not sure what the logic of your game is but you should create attributes and avoid the use of global, use the instances attributes and methods. 不确定游戏的逻辑是什么,但是您应该创建属性并避免使用全局属性,而应使用实例的属性和方法。 You can exit the loop with sys.exit if your enemys life gets to 0. 如果敌人的生命变为0,则可以使用sys.exit退出循环。

import time
import sys
from random import random

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

    def attack(self):
        if random() > .20:
            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()
while True:
    time.sleep(1)
    enemy1.attack()
    enemy1.health_check()
    time.sleep(1)

Example output: 输出示例:

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!

You could also make a difficulty attribute which set the chance of successful attacks by increasing or decreasing what you compare random() to: 您还可以创建一个难度属性,该属性通过增加或减少您将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!")
            sys.exit()


enemy1 = Enemy(.20) 

If you wanted to break in the while, return in the method and check with an if: 如果您想暂时中断,请返回方法并检查是否:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM