2016-05-14 72 views
-1
#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岁,并且正在死亡。基本上我无法解决的是健康问题。我没有设法看到健康状况是否正常,因为我永远无法让敌人接受健康状况。

任何帮助,将不胜感激。

+2

好吧,你计算'current_enemy_health = enemy_health-dmg'和'dmg = randint(0,50)',所以难怪这是一个随机数量......顺便说一句。您可以在代码上应用[PEP 8](https://www.python.org/dev/peps/pep-0008/)格式化程序,如[autopep8](https://pypi.python.org/pypi/autopep8)也许 – miraculixx

回答

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 

这本质上确实是从敌人的当前运行状况,这将导致在减去一个随机数你报告的随机数。为了解决这个问题,玩家使用任何武器都会被分配一个“损害”,并将其作为参数放入函数中。您的新功能是这样的:

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) 

祝你好运,快乐编码!