Reputation: 95
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?
Upvotes: 0
Views: 4409
Reputation: 9858
(In response to the questioner's comment on an earlier answer) This is how you could implement health with a class.
import os
class GameStatus:
def __init__(self):
self.health = 100
def reduce_health(self):
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)
As I said this is overkill, especially if you have a single game loop and probably only need to check the health once at the end of each loop, but potentially useful if you start adding more complexity.
Upvotes: 0
Reputation: 387745
Are you trying to do this?
game_state['health'] = game_state['health'] - 10
Upvotes: 1