我的Python游戏没有检查敌人是否已经死亡

时间:2017-01-09 20:44:17

标签: python python-3.x

我不知道为什么这个游戏不起作用。每当健康状况低于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)

3 个答案:

答案 0 :(得分:1)

if H <= 0开头的块有错误的缩进。它目前在while True循环之外,因此永远不会被执行。您需要在此块中添加4个空格才能在while True循环中移动它:

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

然后它被执行。

P.S。从13岁开始编程开始的好事。让事情继续下去!

答案 1 :(得分:0)

你在GameOver()调用后错过了一个break语句吗?

答案 2 :(得分:-1)

我已将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