猿问

在使用硬币和随机攻击模拟期间,不会保存用户/玩家的健康状况

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

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

我的主要问题是敌人和玩家的生命值在攻击后重新开始。我的程序如何在受到攻击后保存用户的生命值,而不是重置为 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)



小怪兽爱吃肉
浏览 95回答 1
1回答

千巷猫影

要使代码编辑您需要使用的变量globals。当您在函数中使用括号调用变量时,它们只会在该变量的范围内进行编辑,但是当您使用全局变量时,它们会针对整个程序进行编辑。下面是使用全局变量的代码:import randomimport timeimport sysenemyHealth = 10playerHealth = 10def 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()    passdef 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()    passdef 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()
随时随地看视频慕课网APP

相关分类

Python
我要回答