我正在用python制作游戏,它有一个健康系统。我是否可以在不使用game_state['health']
的情况下检查if game_state['health'] = =< 0
我一直有的变化?
答案 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并对其进行适当操作。我想这仍在使用您的支票,但是每次要检查健康状况时都不必明确写一行代码。