擅长:python、mysql、java
<p>(在回答提问者对先前答案的评论时)这就是如何用类实现health的方法。在</p>
<pre><code>import os
class GameStatus:
def __init__(self):
self.health = 100
def reduce_health(self):
self.health = self.health - 10
if self.health <= 0:
game_over()
def main_game():
game_status = GameStatus()
#...
# when player gets hurt
game_status.reduce_health()
# ...
def game_over():
print "game over, sorry"
os._exit(1)
</code></pre>
<p>正如我所说,这是一种过度杀戮,特别是如果你有一个游戏循环,并且可能只需要在每个循环结束时检查一次健康状况,但是如果你开始增加更多的复杂性,这可能会有用。在</p>