Looking to make a very simple health function in Python

Sugardust :

Let me preface by saying that I am very very new to programming in general. I've been doing it for about a week and have successfully made things like calculators and all that beginner jazz and have been working on my first real "project", which is a text-based adventure game. My problem right now is that I cannot put together a very simple health system for the life of me and would like to get any pointers any of you might have.

I've rewritten the code for it many times but here is the basic framework of what I've been working with:

def healthSystem(enemy_atk = 0):   # Called at the beginning of every game screen.
    watermark()   # A function that prints the game title and other things at the top of every screen

    max_health = 10       # Player max health
    no_health = 0         # Player dies at this threshold
    modifier = enemy_atk  # An enemy attack roll is taken from another function and stored here
    current_health = max_health - modifier   # Calculating the new health

    print("+" * current_health)   # Prints the new health to the screen, every 1 health is represented with a "+"
    print()

It's very barebones and it works on the first game screen. However, when you progress to the next game screen, everything is recalculated and the health is redrawn to the screen every time, so you get a different amount of health every screen.

Would this call for a class? I have yet to use classes and have seen some examples using them, but I was hoping I could stick to functions for this first project. Thanks for any help everyone.

GandhiGandhi :

Looks like the problem is that you want the program to remember the player's health between screens?

If so, I think you'll need some kind of variable outside of the function to store the player's health in between attacks. Since right now it looks like the function resets the health to 10 every time.

Also, I'd recommend using another argument to the function to pass in your current health, and a return value to calculate what your new health should be after the attack.

If that's the approach you're going for, I think your top level function would look kind of like this --

# Starting Health
my_health = 10
...
# Main loop
while(not_quit):
    ...
    # Update health
    my_health = healthSystem(enemy_attack, my_health)
    ...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=19365&siteId=1