1

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

4

1 回答 1

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 并对其采取适当的行动。我猜这仍然使用您的检查,但是您不必每次要检查健康状态时都显式编写一行代码。

于 2012-12-02T20:18:47.307 回答