2012-07-02 108 views
0

我有這個至今:一個基於文本的遊戲如何循環,取決於輸入在python

print('a skeleton comes into view, the hiker must have been dehydrated.') 
print ('he was wearing a Yankees HAT, to the right of his body he set his BACKPACK   and WOODEN WALKING STICK next to the wall') 
input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ") 
if input2 == 'PICK UP HAT': 
    print 'taken' 
    hat = hat+1 
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ") 
# 
# 
# 

if input2 == 'SEARCH BACKPACK': 
    print ("there are OLD CLOTHES in here as well as a TARP") 
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ") 


elif input2 == 'PICK UP CLOTHES': 
    print ("tsken") 
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ") 


elif input2 == 'PICK UP TARP': 
    print ("taken") 
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ") 


elif input2 == 'PICK UP BONE': 
    print ("taken") 
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ") 


elif input2 == 'PICK UP WOODEN WALKING STICK': 
    print "Taken" 
    input2 = raw_input ("You may SEARCH____(object),PICK UP____, USE____ ON_____, or GO ON: ") 


elif input2 == 'GO ON': 
    input3 = raw_input ("left or right: ") 
    if input3 == 'left': 
     import module3 
    elif input3 == 'right': 
     import module4 

我無法理解,如果我要創建一個或同時在這裏發言。

例如:如何讓玩遊戲的人無法拿起帽子兩次或拿起防水布而不搜索揹包。

+1

有關SO以後的文章,你可能想看看[我如何格式化我的代碼塊?](http://meta.stackexchange.com/q/22186)。我儘可能清理你的代碼縮進。 –

回答

1

用於您的問題的部分解決方案是使用一個調度員:

def pick_up_hat(): 
    return True # do stuff 

def search_backpack(): 
    return False # do stuff 

actions = { 
    'PICK UP HAT': pick_up_hat, 
    'SEARCH BACKPACK': search_backpack, 
    # ... 
} 

go = True 
while go: 
    cmd = raw_input().strip() 
    go = actions[cmd]() 

注意,還有一些其他的問題,你的設計,你將需要解決,如管理的狀態。

+0

謝謝你生病了試試這個 – user1496368