-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歲,並且正在死亡。基本上我無法解決的是健康問題。我沒有設法看到健康狀況是否正常,因爲我永遠無法讓敵人接受健康狀況。
任何幫助,將不勝感激。
好吧,你計算'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