0

I'm planning to add a health system in a python game. I want it to be in a game_state['']. How would I go about making the health system relative to the damage you've taken?

Like this:

    print "Your health is", game_state['health']
    print "Zombie attacks"
    global health = game_state['health'] - 10
    print "Your health is", game_state['health'] - 10

Would something like that work? Can I use global?

4

2 回答 2

1

你想这样做吗?

game_state['health'] = game_state['health'] - 10
于 2012-12-02T17:59:07.113 回答
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)

正如我所说的那样,这太过分了,特别是如果您有一个游戏循环并且可能只需要在每个循环结束时检查一次健康状况,但如果您开始增加更多复杂性,则可能很有用。

于 2012-12-02T19:28:34.577 回答