我打算在python游戏中添加一个健康系统。我希望它在game_state ['']中。我将如何制定相对于你所受伤害的卫生系统?
像这样:
print "Your health is", game_state['health']
print "Zombie attacks"
global health = game_state['health'] - 10
print "Your health is", game_state['health'] - 10
这样的事情会起作用吗?我可以使用全球吗?
答案 0 :(得分:1)
你想这样做吗?
game_state['health'] = game_state['health'] - 10
答案 1 :(得分:0)
(回答提问者对早期答案的评论)这是你如何用课堂实现健康。
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)
正如我所说,这是过度的,特别是如果你有一个游戏循环,可能只需要在每个循环结束时检查一次健康状况,但如果你开始增加更多的复杂性,可能会有用。