I'm making a basic version of pokemon, and I have a function that looks like this:
global userhp, comphp
userhp = 100
comphp = 100
def doturn(attacker, enemy, move, attackerhp, enemyhp):
from random import randint
if move == 1:
dmg = randint(18, 25)
enemyhp -= dmg
print("\t" + attacker, "have done", str(dmg), "damage")
print("\t" + enemy, "now have", enemyhp, "health\n")
if move == 2:
dmg = randint(8, 35)
enemyhp -= dmg
print("\t" + attacker, "have done", str(dmg), "damage")
print("\t" + enemy, "now have", enemyhp, "health\n")
if move == 3:
dmg = randint(15, 22)
attackerhp += dmg
print("\t" + attacker, "have healed", str(dmg), "health")
print("\t" + attacker, "now have", attackerhp, "health\n")
Here is an example call:
doturn(user, comp, 3, userhp, comphp)
The problem I'm having is that changing attackerhp and enemyhp doesn't affect userhp and comphp. It stays at 100, and the game is unable to progress. I need a way of changing the global variables, but also doing it without hard-coding the variables into the function, because that way it would only work for the user or the computer.
I've been able to solve it, but my solution was to return the damage value and applying that to userhp and comphp outside this function. I could write separate functions for user and comp, or have an if statement check who's turn it is and do it that way, but I'd quite like to keep this compact. Any feedback would be appreciated.