我是 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)
千巷猫影
相关分类