文字游戏 - 游戏循环

时间:2015-10-15 09:47:18

标签: python loops

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

1 个答案:

答案 0 :(得分: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."