2017-01-09 54 views
0

我不明白为什么这个游戏不工作。每当健康低于0时,它应该是游戏结束或获胜,但它只是跳过并进入底片。我的Python游戏不检查敌人是否死亡

这是我到目前为止的代码:

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

你得到什么错误?通常这个错误会告诉你发生了什么事。 –

+0

它不会返回一个错误,它只是没有激活如果看看 – Catmantj

+0

https://repl.it/FCtI/28 – Catmantj

回答

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

我引用你的游戏:*永远不要使用大写* – mkrieger1

+0

是的,这个工程,但这是因为你增加了缩进。再次尝试您在问题中发布的完整代码。这个也可以。你应该再次移动if块 – hansaplast

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岁开始编程开始。让事情继续下去!

+0

我已经尝试过,但它仍然不起作用 – Catmantj

+0

我的意思是你可以看看整个代码,如果你想https://repl.it/FCtI/28 – Catmantj

+0

不起作用,因为我有2个while循环和你的告诉我把它放在第二个不断要求INPUT – Catmantj

0

你在GameOver()调用之后是否缺少break语句?

+0

没有GameOver()退出 – Catmantj

+0

那么,从你连接GameOver的代码并不真正退出。你无限循环地获得堆栈。 – anserk

+0

嗯,但它工作 – Catmantj