2017-04-25 53 views
-2

我創建了代碼並且遊戲開始正常工作,但是在我放入方向之後,它不會進入房間。我檢查過這兩個函數是否都可以自己工作,並且工作正常,但我不確定它們如何鏈接在一起。遊戲沒有在方向功能之後玩房間部分

import time 
import sys 
import random 

Forward = ["F", "f", "forward", "FORWARD", "Forward"] 
Backwards = ["B", "b", "back", "BACK", "backwards", "BACKWARDS"] 
Left = ["L", "l", "LEFT", "left", "Left"] 
Right = ["R", "r", "right", "RIGHT", "Right"] 
directionL = Right + Left + Backwards + Forward 

health = 10 

def delay_print(s): 
    for c in s: 
     sys.stdout.write('%s' % c) 
     #sys.stdout.flush() 
     time.sleep(0.10) 
def name(): 
    name = input('Hey, Who are you? ') 
    return name 

def direction(): 
    direction = input("What direction would you like to go: Forward, Left, Right or Backwards?") 
    while direction not in directionL: 
     direction = input ("Please enter Forward, Left, Right or Backwards.") 
    else: 
     print ("You went", direction) 
def room(): 
    global health 
    a=random.randint(1,8) #1=Monster 2=Exit 3-4=Ghost 5-8=Empty 
    if a==1: 
     delay_print('You encountered the monster you lose 5HP ') 
     health=health-5 
    elif a==2: 
     delay_print('You escaped the dungeon ') 
    elif a==3 or a==4: 
     delay_print('You encountered the ghost ') 
     x=random.randint(1,11) 
     y=random.randint(1,11) 
     print((x),'+',(y)) 
     answer=int(input("What is the answer")) 
     realanswer = x+y 
     if answer==realanswer: 
     print("That's the correct answer") 
     health = health+2 
    else: 
     print("Wrong answer, the answer was",realanswer,"!") 
     health = health-2 
elif a==5 or a==6 or a==7 or a==8: 
    print ('This room is empty') 
    health = health-1 
return print("You have", health,"HP left ") 

if health <1: 
    print ("Game Over") 


print('You hear a voice echo in the background.') 
delay_print('H...') 
delay_print('Hey...') 
delay_print('Welcome %s you are stuck in this labyrinth and you need to 
escape.' % name()) 
print ('\n') 
room(direction()) 
+0

您使用哪個調試器來遍歷代碼? – lit

回答

0

分析

它不會去的房間,因爲錯誤消息(S)的它給你。您的壓痕是錯在好幾個地方,最後的死亡是在這裏:

room(direction()) 

這是說「叫方向,並使用它的返回值來調用房間」。但是,方向不返回一個值,並且房間不接受一個。

進展

使用增量編程。部分混淆來自編寫超過50行代碼而未正確測試鏈接。寫幾行,測試它們,確保它們正常工作,直到解決問題才能繼續。

在這種情況下,將你所謂的單個例程擱置一旁。試着通過迷宮式的「存根」例程取得進展,這些代碼只做一件合理的事情。例如:

def direction(): 
    return "L" 

def room(): 
    print ("You entered another room.") 

從這裏開始研究如何從一個房間移動到另一個房間。

如何做呢

研究,就像堆棧溢出的前奏之旅告訴你。互聯網上有無數的地牢爬行遊戲。例如,您可以從「學習艱難的方式學習Python」中獲取模板。

相關問題