繁体   English   中英

我需要在python游戏中制作一个健康系统

[英]I need to make a health system in a python game

我打算在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

这样的事情行得通吗? 我可以使用全局吗?

您是否正在尝试这样做?

game_state['health'] = game_state['health'] - 10

(响应提问者对先前答案的评论)这就是您可以如何通过类实现运行状况。

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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM