Я не понимаю, почему эта игра не работает. Всякий раз, когда здоровье становится меньше 0, оно должно быть закончено или выиграно, но оно просто пропускается и становится отрицательным.

Это мой код до сих пор:

def Battle(E,H,S,PH):

  print('Battle starting')
  win = False
  while win == False:
    while True:
      Answer = input('Attack(\033[0;37;41mA\033[0;0;0m) or Defend(\033[0;37;41mA\033[0;0;0m)')
      if Answer == 'A':
        D = randint(0, S)
        PH = PH - D
        print(E,'attacking')
        print('deals',D,'damage')
        print('you now have',PH,'health')
        PA = randint(1,5)
        print('you deal',PA,'damage')
        H = H - PA
        print('the ',E,' has',H,'health')
        break
      elif A == 'D':
        D = randint(0, S-randint(0,2))
        PH = PH - D
        print( E,'atacking')
        print('deals',D,'damage')
        print('you now have',PH,'health')
        break
  if H <= 0:
    print('You killed the',E)
    win = True
    break
  elif PH <= 0:
    GameOver()

Нижняя часть - это бит, который должен выходить и отображать текст, когда вы проигрываете или выигрываете:

 if H <= 0:
        print('You killed the',E)
        win = True
        break
      elif PH <= 0:
        GameOver()

Полная программа

#COPYRIGHT TYLER JAMES 2017 

import random
from random import randint

#_________________________________________________________________
#_________________________________________________________________
print('\033[0;0;0m')
print('\033[0;30;42m <><><><><><><><><><><><><><><><><><><><><><><><><><><><><>')
print('\033[0;30;42m <>                                                      <>')
print('\033[0;30;42m <> _        _______  _______  _______  _        ______  <>')
print('\033[0;30;42m <>( \      (  ____ \(  ____ \(  ____ \( (    /|(  __  \ <>')
print('\033[0;30;42m <>| (      | (    \/| (    \/| (    \/|  \  ( || (  \  )<>')
print('\033[0;30;42m <>| |      | (__    | |      | (__    |   \ | || |   ) |<>')
print('\033[0;30;42m <>| |      |  __)   | | ____ |  __)   | (\ \) || |   | |<>')
print('\033[0;30;42m <>| |      | (      | | \_  )| (      | | \   || |   ) |<>')
print('\033[0;30;42m <>| (____/\| (____/\| (___) || (____/\| )  \  || (__/OX)<>')
print('\033[0;30;42m <>(_______/(_______/(_______)(_______/|/    )_)(______/ <>')
print('\033[0;30;42m <>                                       By  Tyler James<>')
print('\033[0;30;42m <>                                                      <>')
print('\033[0;30;42m <><><><><><><><><><><><><><><><><><><><><><><><><><><><><>')
print('\033[0;0;0m have Fun playing this game,i worked hard to make it thanks')
print('___________________________________________________________')

#_________________________________________________________________
#_________________________________________________________________
#DEFINITION OF GAMEOVER
def GameOver():
  while True:
    print('GAME OVER')
#DEFINITION OF GAMEOVER

def Battle(E,H,S,PH):

  print('Battle starting')
  win = False
  while win == False:
    while True:
      Answer = input('Attack(\033[0;37;41mA\033[0;0;0m) or Defend(\033[0;37;41mD\033[0;0;0m)')
      if Answer == 'A':
        D = randint(0, S)
        PH = PH - D
        print(E,'attacking')
        print('deals',D,'damage')
        print('you now have',PH,'health')
        PA = randint(1,5)
        print('you deal',PA,'damage')
        H = H - PA
        print('the ',E,' has',H,'health')
        break
      elif A == 'D':
        D = randint(0, S-randint(0,2))
        PH = PH - D
        print( E,'atacking')
        print('deals',D,'damage')
        print('you now have',PH,'health')
        break

    if H <= 0:
      print('You killed the',E)
      win = True
      break
    elif PH <= 0:
      GameOver()



#DEFINITION OF QUIZ
def Quiz(Q,AN):
  print(Q)
  INP = input('Answer?')
  if INP == 'debug42':
    print('debug42')
  elif INP != AN:
    GameOver()
  else:
    print('CORRECT')
#DEFINITION OF QUIZ
#_________________________________________________________________
#_________________________________________________________________
#LEVEL 1
Name = input('What is your Name?')
print('Ok your name is','\033[0;37;41m',Name )
print('\033[0;0;0m')
print('You are in a small room with a door, opposite sits a dresser')
key = False
while True:
  A = input('What do you do?')
  if A == 'open door':
    if key == True:
      print('you try the key in the door')

      print('it works, you open the door and walk through')

      break
    elif key == False:
      print('The door wont open')
  elif A == 'look door':
    print('the door has a panel design and a keyhole on the right hand side')
  elif A == 'look dresser':
    print('their are two drawers')
  elif A == 'open dresser':
    print('there is nothing in the top drawer')

    print('but in the second is a brass key')
    key = True
  elif A == 'help':
    print('---------------------------------------------')
    print('Commands')
    print('open object...use object')
    print('look object...describe object')
    print('room...repeats the infomation given at start')
    print('COMMANDS NEED TO BE\033[1m \033[4mLOWERCASE\033[0m NEVER USE \033[1m\033[4mCAPS\033[0m')
    print('---------------------------------------------')  
  elif A == 'room':
    print('You are in a small room with a door, opposite sits a dresser')
  elif A == 'debug42':
    break
  else:
    print('\033[0;37;41m    Incorrect command or string', '\033[0;0;0m')
print('congrats you escaped the room')
#LEVEL 1
#_________________________________________________________________
#_________________________________________________________________
#LEVEL 2
print('____________________________________________')
print('A wise looking man walks up to you')
print('\033[1;33;40m"Here is 3 riddles get ONE wrong DIE!!"\033[0;0;0m')




Quiz('\033[1;33;40m"You will always find me in the past. I can be created in the present, But the future can never taint me. What am I?"\033[0;0;0m','history')


Quiz('\033[1;33;40m"Which vehicle is spelled the same forwards and backwards?"\033[0;0;0m','racecar')


Quiz('\033[1;33;40m"What is it that no man ever yet did see, which never was, but always is to be?"\033[0;0;0m','tomorrow')


print('\033[1;33;40m"Fine you can pass take these Jems, I wish you luck... If you ever make it out of this town "\033[0;0;0m')
print('\033[1;33;42m',Name,' got 5₪ (₪ = Ducat) \033[0;0;0m')
Money = 5
#LEVEL 2
#_________________________________________________________________
#_________________________________________________________________
#LEVEL 3
print('You enter a shop')
print('\033[1;33;40m"Welcome too my shop would you like to purchase a level 1 sword and sheild for 5₪?"\033[0;0;0m')
print('you have', Money,'₪')
print('you buy the item -5₪')
print('\033[1;33;42m',Name,'got a Sword[1] and Shield[1]')
print('you leave the shop')
print('_____________________________________________________')
Battle('Bat',10,5,50)
0
Catmantj 9 Янв 2017 в 23:44

3 ответа

Лучший ответ

Я переместил if наверх, и теперь это работает:

def Battle(E,H,S,PH):

  print('Battle starting')
  win = False
  while win == False:
    if H <= 0:
      print('You killed the',E)
      win = True
      break
    elif PH <= 0:
      GameOver()
    while True:
      Answer = input('Attack(\033[0;37;41mA\033[0;0;0m) or Defend(\033[0;37;41mD\033[0;0;0m)')
      if Answer == 'A':
        D = randint(0, S)
        PH = PH - D
        print(E,'attacking')
        print('deals',D,'damage')
        print('you now have',PH,'health')
        PA = randint(1,5)
        print('you deal',PA,'damage')
        H = H - PA
        print('the ',E,' has',H,'health')
        break
      elif A == 'D':
        D = randint(0, S-randint(0,2))
        PH = PH - D
        print( E,'atacking')
        print('deals',D,'damage')
        print('you now have',PH,'health')
        break
-1
mkrieger1 10 Янв 2017 в 00:22

Блок, начинающийся с if H <= 0, имеет неправильный отступ. В настоящее время он находится вне цикла while True и, следовательно, никогда не выполняется. Вам нужно добавить 4 пробела в этот блок, чтобы переместить его в цикл while True:

if H <= 0:
  print('You killed the',E)
  win = True
  break
elif PH <= 0:
  GameOver()

И тогда это выполняется.

Постскриптум хорошо, начиная с программирования в 13 лет.

1
hansaplast 9 Янв 2017 в 20:56

Вам не хватает оператора break после вызова GameOver ()?

0
anserk 9 Янв 2017 в 21:19