每次发生事情时,如何让python程序检查一个值?

时间:2012-12-02 18:57:14

标签: python text

我正在用python制作游戏,它有一个健康系统。我是否可以在不使用game_state['health']的情况下检查if game_state['health'] = =< 0我一直有的变化?

1 个答案:

答案 0 :(得分:2)

绝对阅读一些教育材料,教程等。

基本方法可能如下所示:

class MyCharacter(object):
    """Simple Character Class"""

    def __init__(self, health):
        self._health = health

    @property
    def health(self):
        return self._health

    @health.setter
    def health(self, new_value):
        self._health = new_value
        if new_value <= 0:
            print "What's that strange white light...?"
            raise EndGameEvent("Character is dead... :(")

    def takes_damage(self, damage):
        print "Ouch... that really hurt!"
        self.health = self.health - damage

您的主游戏线程将收到EndGameEvent并对其进行适当操作。我想这仍在使用您的支票,但是每次要检查健康状况时都不必明确写一行代码。