2016-05-14 28 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) 

祝你好運,快樂編碼!