pythontext-based

User/Player Health is not saved during attack simulation using a coin and random


I am a novice to python. My longterm project is to design a choose your own adventure text-based game.

A major component of this game is attack scenarios. With that in mind, I have been constructing a python program for simulating an attack scenario. In this case, by flipping a coin to first determine whether the player or the enemy attacks first. Afterwards, a random integer between 1 and 10 is used as the attack damage. A function (HealthCheck), checks the health of the player/enemy to determine whether the player/enemy is dead.

My main problem is that the enemy's and player's health restarts after an attack. How can my program save the user's health after an attack, instead of resetting to 10 HP?

Below is my python code. Thank you for your help.

import random
import time
import sys

enemyHealth = 10
playerHealth = 10

def playerAttack(enemyHealth):
    attack_damage = random.randint(1, 10)
    print("The player does " + str(attack_damage) + " damage points to 
    the enemy.")
    enemyHealth -= attack_damage
    print("The enemy has " + str(enemyHealth) + " HP left!")
    enemyHealthCheck(enemyHealth)
    pass

def enemyAttack(playerHealth):
    attack_damage = random.randint(1, 10)
    print("The enemy does " + str(attack_damage) + " damage points to 
    the player.")
    playerHealth -= attack_damage
    print("The player has " + str(playerHealth) + " HP left!")
    playerHealthCheck(playerHealth)
    pass

def turnChoice():
    h = 1
    t = 2
    coin = ""
    while coin != "h" and coin != "t":
        coin = input("Player, flip a coin to decide who attack first.\n"
                     "Heads or tails? H for heads. T for tails.\n")
        
        if coin == "h":
            print("You chose heads.\n"
                  "Flip the coin. \n"
                  ". . .")
            time.sleep(2)
        else:
        print("You chose tails.\n"
              "Flip the coin. \n"
              ". . .")
        time.sleep(2)

        choice = random.randint(1, 2)
        if choice == coin:
            print("Great choice. You go first.")
            playerAttack(enemyHealth)
        else:
            print("Enemy goes first.")
            enemyAttack(playerHealth)

def replay():
    playAgain = ""
    while playAgain != "y" and playAgain != "n":
        playAgain = input("Do you want to play again? yes or no")

    if playAgain == "y":
        print("You chose to play again.")
        print(".")
        print(".")
        print(".")
        time.sleep(2)
        turnChoice()
    else:
        print("Game over. See you soon.")
        sys.exit()

def playerHealthCheck(playerHealth):
    if playerHealth <=0:
        print("Player is dead. Game over.")
        replay()
    else:
        print("The player has " + str(playerHealth) + " HP points!")
        print("It is your turn to attack.")
        playerAttack(enemyHealth)

def enemyHealthCheck(enemyHealth):
    if enemyHealth <=0:
        print("Enemy is dead. You win.")
        replay()
    else:
        print("Enemy is not dead. The enemy has " + str(enemyHealth) + " HP points.")
        print("It is their turn to attack.")
        enemyAttack(playerHealth)

turnChoice()

Solution

  • To make the code edit the variables you need to use globals. When you call the variable with the parentheses in the function they are only edited in the scope of that variable, but when you use globals they get edited for the whole program. Here is an example. Below is the code that is using globals:

    import random
    import time
    import sys
    
    enemyHealth = 10
    playerHealth = 10
    
    
    def playerAttack():
        global enemyHealth
        attack_damage = random.randint(1, 10)
        print("The player does " + str(attack_damage) + " damage points to the enemy.")
        enemyHealth -= attack_damage
        print("The enemy has " + str(enemyHealth) + " HP left!")
        enemyHealthCheck()
        pass
    
    
    def enemyAttack():
        global playerHealth
        attack_damage = random.randint(1, 10)
        print("The enemy does " + str(attack_damage) + " damage points to the player.")
        playerHealth -= attack_damage
        print("The player has " + str(playerHealth) + " HP left!")
        playerHealthCheck()
        pass
    
    
    def turnChoice():
        h = 1
        t = 2
        coin = ""
        while coin != "h" and coin != "t":
            coin = input("Player, flip a coin to decide who attack first.\n"
                         "Heads or tails? H for heads. T for tails.\n")
    
            if coin == "h":
                print("You chose heads.\n"
                      "Flip the coin. \n"
                      ". . .")
                time.sleep(2)
            else:
                print("You chose tails.\n"
                      "Flip the coin. \n"
                      ". . .")
            time.sleep(2)
    
            choice = random.randint(1, 2)
            if choice == coin:
                print("Great choice. You go first.")
                playerAttack()
            else:
                print("Enemy goes first.")
                enemyAttack()
    
    
    def replay():
        playAgain = ""
        while playAgain != "y" and playAgain != "n":
            playAgain = input("Do you want to play again? yes or no")
    
        if playAgain == "y":
            print("You chose to play again.")
            print(".")
            print(".")
            print(".")
            time.sleep(2)
            turnChoice()
        else:
            print("Game over. See you soon.")
            sys.exit()
    
    
    def playerHealthCheck():
        global playerHealth
        if playerHealth <= 0:
            print("Player is dead. Game over.")
            replay()
        else:
            print("The player has " + str(playerHealth) + " HP points!")
            print("It is your turn to attack.")
            playerAttack()
    
    
    def enemyHealthCheck():
        global enemyHealth
        if enemyHealth <= 0:
            print("Enemy is dead. You win.")
            replay()
        else:
            print("Enemy is not dead. The enemy has " + str(enemyHealth) + " HP points.")
            print("It is their turn to attack.")
            enemyAttack()
    
    
    turnChoice()