2015-10-15 156 views
1
print "Welcome to the game. In the game you can 'look around' and 'examine things'." 
print "There is also some hidden actions." 
print "You wake up." 

input = raw_input("> ") 

haveKey = False 
applesExist = True 


if input == "look around": 
    print "You are in a dusty cell. There is a barrel in the corner of the room, an unmade bed," 
    print "a cabinet and chest. There is also a cell door." 
elif haveKey == False and input == "open door": 
    print "The door is locked." 
elif haveKey == True and input == "open door": 
    print "You open the door, walk out and immediately gets shot with an arrow. You won, kinda." 
elif input == "examine barrel": 
    print "There is apples in the barrel." 
elif applesExist == True and input == "eat apple": 
    print "Mmmmh, that was yummy! But now there are no apples left..." 
    applesExist = False 
elif applesExist == False and input == "eat apple": 
    print "sury, u et al aples befur!!1111" 
elif input == "examine bed": 
    print "The bed is unmade, and has very dusty sheets. This place really needs a maid." 
elif input == "sleep on bed": 
    print "You lie down and try to sleep, but you can't because of all the bugs crawling on you." 
elif input == "examine chest": 
    print "There is a key in the chest." 
elif input == "take key": 
    haveKey = True 
    print "You take the key." 
elif input == "examine cabinet": 
    print "The cabinet is made of dark oak wood. There is a endless cup of tea in it." 
elif input == "drink tea": 
    print "You put some tea in your mouth, but immediately spit it out." 
    print "It seems it has been here for quite some time." 
else: 
    print "Huh, what did you say? Didn't catch that." 

嗯,嗨。我需要幫助。正如你可以看到這是一個非常基礎的文本遊戲, 讓玩家與環境進行交互。我打算擴大 互動的事情,我不需要任何幫助。但隨着遊戲循環,我做到了。您可能會意識到,但對於那些不這樣做的人,每次我寫一個命令時,程序都會關閉。我被告知我需要一個遊戲循環來做到這一點,但我不知道如何。文字遊戲 - 遊戲循環

我需要告訴你的是,與你相比,我有點遲鈍。我不是編程天才,我只是簡單地享受編程。所以,請說,如何,而不是爲什麼。我會自己解決這個問題,因爲我一直都在這個世界上。如果我需要知道爲什麼,那麼我會再問你一次! = D

+0

您*瞭解循環以及如何製作一個?例如,讓一個循環迭代*而某些條件爲真? –

+0

是的,kinda = D。我知道如何讓東西永遠循環,或者直到布爾變化,但這是我的限制=) – Elsvaer

+0

然後想一會兒(雙關語不打算):你做一個循環,而用戶不想退出,就這麼簡單。您需要一個初始化爲「True」的變量,並保持爲「True」,直到用戶輸入一個要退出的命令,並將該變量用作循環條件。 –

回答

0

你必須像while一樣使用循環。你必須從循環內部獲得輸入。你必須檢查遊戲是否結束。輸入將自動重置,因爲raw_input()功能:

print "Welcome to the game. In the game you can 'look around' and 'examine things'." 
print "There is also some hidden actions." 
print "You wake up." 

haveKey = False 
applesExist = True 
gameover = False 

while gameover == False: 
    input = raw_input("> ") 
    if input == "look around": 
     print "You are in a dusty cell. There is a barrel in the corner of the room, an unmade bed," 
     print "a cabinet and chest. There is also a cell door." 
    elif haveKey == False and input == "open door": 
     print "The door is locked." 
    elif haveKey == True and input == "open door": 
     print "You open the door, walk out and immediately gets shot with an arrow. You won, kinda." 
    elif input == "examine barrel": 
     print "There is apples in the barrel." 
    elif applesExist == True and input == "eat apple": 
     print "Mmmmh, that was yummy! But now there are no apples left..." 
     applesExist = False 
    elif applesExist == False and input == "eat apple": 
     print "sury, u et al aples befur!!1111" 
    elif input == "examine bed": 
     print "The bed is unmade, and has very dusty sheets. This place really needs a maid." 
    elif input == "sleep on bed": 
     print "You lie down and try to sleep, but you can't because of all the bugs crawling on you." 
    elif input == "examine chest": 
     print "There is a key in the chest." 
    elif input == "take key": 
     haveKey = True 
     print "You take the key." 
    elif input == "examine cabinet": 
     print "The cabinet is made of dark oak wood. There is a endless cup of tea in it." 
     gameover = True 
    elif input == "drink tea": 
     print "You put some tea in your mouth, but immediately spit it out." 
+1

爲什麼輸入字符串在循環結尾「重置」?無論如何,一旦循環開始,它將首先「重置」。 –

+0

@JoachimPileborg是的,你的權利我犯了一個錯誤,因爲我的測試命令。不需要這行:'input = 0' – mertyildiran

+0

沒有probs,抱歉,遲到的答覆,去resturaunt並吃了一些壽司。但是我會保留:input = 0,因爲它讓我想起了你。我愛你。感謝您解決我的微不足道的問題= D – Elsvaer