2014-11-21 116 views
-1

我寫了一個python文本冒險遊戲,我想添加的最後一件東西是一個計數器,可以計算在遊戲結束時顯示的轉數。打開python文本冒險計數器

它只需要計算每一次玩家輸入的東西,但林不知道如何編寫這一點,它有點尷尬因爲我確定這將是一個非常簡單的解決方案

IM使用Python 3.4.1

while True: 
    playerInput = input("What do you want to do? ") 
    playerInput = playerInput.lower() 
    playerWords = playerInput.split(" ", 1) 
    verb = playerWords[0] 
    if len(playerWords) == 2: 
     noun = playerWords[1] 
    else: 
     noun = "" 


    if playerInput == "quit": 
     break 



    elif playerInput == "look": 
     print(roomDescriptions[currentRoom]) 



    ##--Controls movement--##    
    elif playerInput in dirs: 
     playerInput = playerInput[0] 
     if "treasure" in invItems and playerInput == "s" and currentRoom == "strangeWall":##--Checks for treasure in inventory before allowing game to be won--## 
      print("!!!!Congratulations you have escaped from the dark dungeon!!!!") 
      break 

     elif playerInput in roomDirections[currentRoom]: 
      currentRoom = roomDirections[currentRoom][playerInput] 
      print(roomEntrance [currentRoom]) 
     else: 
      print("You can't go that way") 



    elif playerInput == "lookdown":##--checks for room items on the ground--## 
     printList ("You see;", roomItems[currentRoom]) 



    elif playerInput == "inventory" or playerInput == "inv":##--Displays inventory items--## 
     printList ("You are carrying;", invItems) 



    elif verb == "get":##--Controls picking up items and adding them to inventory/removes from room--## 
     if noun in roomItems[currentRoom]: 
      print("picked up", noun) 
      invItems.append(noun) 
      roomItems[currentRoom].remove(noun) 
     else: 
      print("There is nothing to pick up") 



    elif verb == "drop":##--Controls dropping items and removing them from the inventory/adds to room items--## 
     if noun in invItems: 
      print("You drop the", noun) 
      roomItems[currentRoom].append(noun) 
      invItems.remove(noun) 
     else: 
      print("You are not carrying", noun) 


    elif verb == "use":##--Controls using the lamp and snow boots--## 
     if noun in invItems:##--Checks inventory for lamp or snowboots before allowing certain directional movement--## 
      if noun == "lamp": 
       print("You light the lamp") 
       invItems.remove(noun) 
       roomDirections["hallMid"]["e"] = "giantNature" 

      elif noun == "snowboots": 
       print("You put on the snowboots") 
       invItems.remove(noun) 
       roomDirections["hallMid"]["s"] = "snowRoom" 
      else: 
       print("You cannot use that") 
     else: 
      print("You do not have", noun) 





    else: 
     print ("I don't understand") 

回答

0

沒有看到你的代碼的例子,幾乎不可能告訴你任何具體的東西都可以在你的代碼中工作。

但我可以給你一些東西,你可以適應你的代碼。現在

class CountedInput(object): 
    def __init__(self): 
     self.counter = 0 
    def input(self, *args): 
     self.counter += 1 
     return input(*args) 

counted_input = CountedInput() 

,在你的代碼的任何地方,你叫input(),你改爲調用counted_input.input()

而當你想顯示轉彎計數器,那只是counted_input.counter

(如果你使用的是Python 2.x中,改變inputraw_input。)


現在你已經添加了一個例子來的問題:

這個建議會工作得很好事實上,但你可以讓事情變得更簡單。

您的整個遊戲都是圍繞一個命令循環構建的。每個循環您只需撥打input一次。所以,你需要做的就是統計你在這個循環中多少次。你可以是這樣做的:

counter = 0 
while True: 
    counter += 1 
    playerInput = input("What do you want to do? ") 
    # all the rest of your code 

而現在,您只需打印出來或以其它方式使用counter同任何其他變量。例如:

elif playerInput == "score": 
     print("You have 0/0 points after", counter, "turns") 

(我猜,你實際上並不想用一個score命令嚇嚇你的球員,當你不記分,但應該告訴你的理想。)


如果你想變得聰明,有一個更簡單的方法來做到這一點:只需循環所有數字從1到無窮大。怎麼樣?該count功能,它的工作原理有點像range除了沒有stop價值,因爲它從未停止:

from itertools import count 

for counter in count(1): 
    # the rest of your code 
+0

哦對不起我不認爲你需要看到它,即時通訊仍然是一個新手 – kebab 2014-11-21 01:12:58

+0

@kebab:沒問題。閱讀本網站上的幫助是一個好主意;有一些關於如何提出好問題的很好的信息。但與此同時,我更新了我的答案以處理更具體的問題(正如您所看到的,這樣做更簡單,這是提出更具體問題的好原因之一)。 – abarnert 2014-11-21 01:28:08

+0

是啊我有它的唯一我的第三天使用該網站,只有我的第一學期的Python,所以我很明白,你仍然與我同時在我學習的同時,非常感謝 – kebab 2014-11-21 01:30:24

0

我知道許多人或許會不喜歡這個主意,因爲我已經看到了發生衝突的全局變量的使用意見,但是我將使用全局變量來存儲轉數,並使用全局函數來跟蹤它。

例如:

global turn_counter 
turn_counter = 0 

然後,採取的行動時,你可以這樣做:

turn_counter += 1 

我相信你需要有包括在你的函數全局,但是。

例子:

def game_input_handler(): 
    """ handles all user input """ 
    global turn_counter 

    // game prompt code here 
    // if..elif...else for option handling