Search code examples
pythonfunctionvariablesgame-physics

Variable assignment in function


I'm writing a basic fighting game and am trying to make it so that with each attack it subtracts the amount of the attack from the enemy's health and prints the enemy's current health. However, the health resets to its original amount after I run the script once and loop it. How can I resign the enemies health with the current health?

Here is the script:

import random

while True:
    HEALTH = 20
    ENEMY_HEALTH = 20

    def punch():
        mylist = (xrange(0,3))
        x = random.choice(mylist)
        if x == 3:
            print"your hit was very effective enemy lost 3 hp"
            print("Enemy Health is" ENEMY_HEALTH - x)
        if x == 2:
            print "Your punch was effective enemy lost 2 hp"
            print("Enemy Health is" ENEMY_HEALTH - x)
        if x == 1:
            print "enemy lost 1 point"
            print("Enemy Health is" ENEMY_HEALTH - x)

    def kick():
        mylist = (xrange(0,5))
        x = random.choice(mylist)
        if x > 3:
            "%d" % x
            print"your kick was very effective enemy lost %d hp"
            print("Enemy Health is", ENEMY_HEALTH - x)
        if x > 1 < 3:
            "%d" % x
            print "Your kick was effective enemy lost %d hp"
            print("Enemy Health is" ENEMY_HEAlTH - x)
        if x == 1:
            print "enemy lost 1 point"
            print("Enemy Health is" ENEMY_HEALTH - x)

    def attackChoice(c):
        if c == "punch":
            punch()
        if c == "kick":
            kick()

    c = raw_input("Choice Attack\nKick Or Punch: ")
    attackChoice(c)

I want it to print:

choose attack
kick or punch:kick
enemy lost 3 hp
enemy's heath is 17
choose attack
kick or punch:punch
enemy lost 1 hp
enemy's health is 16

Solution

    • As Blender pointed out you are not changing the value of ENEMY_HEALTH you are just subtracting x from it and printing it without assigning that value back.
    • move your initial assignment out of the while loops.
    • you have extra string thing in your kick code
    • use randint instead of building a list and picking one

    maybe like:

    import random
    health = 20
    enemy_health = 20
    
    def punch():
        global enemy_health
        x = random.randint(1,3)
        enemy_health -= x
        if x == 3:
            print"your hit was very effective enemy lost 3 hp"
        if x == 2:
            print "Your punch was effective enemy lost 2 hp"
        if x == 1:
            print "enemy lost 1 point"
        print "Enemy Health is", enemy_health
    
    def kick():
        global enemy_health
        x = random.randint(1,5)
        enemy_health -= x
        if x > 3:
            print "your kick was very effective enemy lost %d hp" % x
        if x > 1 < 3:
            print "Your kick was effective enemy lost %d hp" % x
        if x == 1:
            print "enemy lost 1 point"
        print "Enemy Health is", enemy_health
    
    def attackChoice(c):
        if c == "punch":
            punch()
        if c == "kick":
            kick()
    
    while True:
        c = raw_input("Choice Attack\nKick Or Punch: ")
        attackChoice(c)