Sugardust
Sugardust

Reputation: 35

Looking to make a very simple health function in Python

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.

Upvotes: 3

Views: 992

Answers (2)

wjandrea
wjandrea

Reputation: 32997

The root problem is that you're not tracking health at all. current_health is defined in the function but not returned or saved to a global or anything. Ultimately a class is going to be the best solution (probably - I've never built a game myself), but the bare minimum fix would be to take current_health as a parameter, modify it in the function, return current_health at the end, and track it outside the function until the next loop.

Upvotes: 0

GandhiGandhi
GandhiGandhi

Reputation: 1350

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)
    ...

Upvotes: 3

Related Questions