2024-09-19 20:50:31 发布
网友
我计划在python游戏中添加一个健康系统。我希望它处于游戏状态。我该怎么做才能使卫生系统与你所受的损害相对照呢?在
像这样:
print "Your health is", game_state['health'] print "Zombie attacks" global health = game_state['health'] - 10 print "Your health is", game_state['health'] - 10
这样的东西行吗?我能用环球吗?在
(在回答提问者对先前答案的评论时)这就是如何用类实现health的方法。在
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)
正如我所说,这是一种过度杀戮,特别是如果你有一个游戏循环,并且可能只需要在每个循环结束时检查一次健康状况,但是如果你开始增加更多的复杂性,这可能会有用。在
你想这么做吗?在
game_state['health'] = game_state['health'] - 10
(在回答提问者对先前答案的评论时)这就是如何用类实现health的方法。在
正如我所说,这是一种过度杀戮,特别是如果你有一个游戏循环,并且可能只需要在每个循环结束时检查一次健康状况,但是如果你开始增加更多的复杂性,这可能会有用。在
你想这么做吗?在
相关问题 更多 >
编程相关推荐