#Variables
enemy=['Dummy','Ghost','Warrior','Zombie','Skeleton']
current_enemy=random.choice(enemy)
enemy_health=randint(1,100)
dmg=randint(0,50)
current_enemy_health=enemy_health-dmg
#Functions
def enemy_stats(current_enemy_health):
if current_enemy_health<=0:
print(current_enemy,"died.")
if current_enemy_health>0:
dmg=randint(0,50)
current_enemy_health=enemy_health-dmg
print(current_enemy,"has",current_enemy_health,"health left.")
#Meeting an enemy - Attack / Defend Option
def encounter(current_enemy):
print(name,"encountered a",current_enemy,"with",current_enemy_health,"health.","What do you do?")
print("Attack? or Defend?")
def battle():
encounter(current_enemy)
#Attack Or Defend?
choice=input("What do you do?")
if choice!="Attack" or choice!="Defend": #If the choice isn't attack then ask again
print("Do you attack or defend?")
choice=input("What do you do?")
#Say correct sentence depending on what you do.
if choice=="Attack": #If the choice was attack then do a random number of dmg to it
print(name,choice+"s",current_enemy,".","You deal",dmg,"damage","to it.")
enemy_stats(current_enemy_health)
if choice=="Defend": #If ... to it
print(name,choice+"s.")
#Check to see if the enemy is still alive
while current_enemy_health>1:
#Attack Or Defend?
choice=input("What do you do?")
if choice!="Attack" or choice!="Defend": #If the choice isn't attack then ask again
print("Do you attack or defend?")
choice=input("What do you do?")
#Say correct sentence depending on what you do
if choice=="Attack": #If the choice was attack then do a random number of dmg to it
print(name,choice+"s",current_enemy,".","You deal",dmg,"damage","to it.")
enemy_stats(current_enemy_health)
if choice=="Defend": #If ... to it
print(name,choice+"s.")
#Checks to see if the enemy is dead
if current_enemy_health<=0:
print(name,"successfully killed a",current_enemy)
battle(
)
所以我正在制作基于文本的RPG游戏。一切顺利,但有一件事我无法解决,我已经尝试了很多东西来尝试解决问题,基本上当你遇到它并且敌人会产生随机的健康状况时。然后你点击它造成一些伤害。 '20生命的僵尸催生了。你会怎么做?'我攻击并说我造成9点伤害。会发生什么是健康只是一个随机数而不是20-9。或者说我做了21次赔偿。这次发生的事情是健康状况再次变为随机数而不是20-21并且死亡。基本上我无法解决的问题是health-dmg部分。我还没有设法看到健康&lt; 0是否有效,因为我永远无法让敌人获得0健康。
任何帮助都将不胜感激。
答案 0 :(得分:0)
在你的功能中:
def enemy_stats(current_enemy_health):
if current_enemy_health<=0:
print(current_enemy,"died.")
if current_enemy_health>0:
dmg=randint(0,50)
current_enemy_health=enemy_health-dmg
print(current_enemy,"has",current_enemy_health,"health left.")
你有这两行:
dmg=randint(0,50)
current_enemy_health=enemy_health-dmg
这基本上做的是从敌人的当前生命值中减去一个随机数,这会导致你报告的随机数。要解决这个问题,请将玩家正在使用的任何武器分配给#34;损坏&#34;这样做并将其作为参数放入函数中。你的新功能看起来像这样:
def enemy_stats(current_enemy_health, dmg):
if current_enemy_health<=0:
print(current_enemy,"died.")
elif current_enemy_health>0:
current_enemy_health=enemy_health-dmg
print(current_enemy,"has",current_enemy_health,"health left.")
并将实现如下:
enemy_stats(var_for_current_enemy_health, damage_of_weapon)
祝你好运,编码愉快!