2015-10-15 196 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