User/Player 在使用硬币和随机攻击模拟期间不保存生命值

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

我是python的新手。我的长期项目是设计一款基于文本的自选冒险游戏。

这个游戏的一个主要组成部分是攻击场景。考虑到这一点,我一直在构建一个 python 程序来模拟攻击场景。在这种情况下,通过掷硬币来决定是玩家先攻击还是敌人先攻击。之后,使用 1 到 10 之间的随机整数作为攻击伤害。一个函数 (HealthCheck),检查 player/enemy 的健康状况以确定 player/enemy 是否已死亡。

我的主要问题是敌人和玩家的生命值在攻击后重新开始。我的程序如何在攻击后保存用户的生命值,而不是重置为 10 HP?

下面是我的 python 代码。感谢您的帮助。

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

要使代码编辑您需要使用的变量 globals。当您在函数中使用括号调用变量时,它们只会在该变量的范围内进行编辑,但是当您使用全局变量时,它们会在整个程序中进行编辑。这是一个example。下面是使用全局变量的代码:

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