亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在使用硬幣和隨機攻擊模擬期間,不會保存用戶/玩家的健康狀況

在使用硬幣和隨機攻擊模擬期間,不會保存用戶/玩家的健康狀況

小怪獸愛吃肉 2023-10-18 15:53:03
我是 python 的新手。我的長期項目是設計一款選擇你自己的冒險文本游戲。該游戲的一個主要組成部分是攻擊場景??紤]到這一點,我一直在構建一個 python 程序來模擬攻擊場景。這種情況下,通過拋硬幣來首先判斷是玩家先攻擊還是敵人先攻擊。之后,使用1到10之間的隨機整數作為攻擊傷害。功能(HealthCheck),檢查玩家/敵人的健康狀況以確定玩家/敵人是否死亡。我的主要問題是敵人和玩家的生命值在攻擊后重新開始。我的程序如何在受到攻擊后保存用戶的生命值,而不是重置為 10 HP?下面是我的Python代碼。感謝您的幫助。import randomimport timeimport sysenemyHealth = 10playerHealth = 10def 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)    passdef 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)    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(enemyHealth)        else:            print("Enemy goes first.")            enemyAttack(playerHealth)
查看完整描述

1 回答

?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

要使代碼編輯您需要使用的變量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()


查看完整回答
反對 回復 2023-10-18
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號