2016-05-14 5 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 и умирает. В основном то, что я не могу исправить, это часть health-dmg. Мне не удалось увидеть, работает ли здоровье < 0, так как я не могу довести врага до 0 здоровья.

Любая помощь будет оценена по достоинству.

+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) 

удачи и счастливого кодирования!

Смежные вопросы