而循环冒险游戏在Python中

时间:2015-06-26 22:23:44

标签: python

我按照本实验室的说明进行操作:http://programarcadegames.com/index.php?chapter=lab_adventure&lang=en

我很困惑如何在设置这些if语句后允许用户前往其他房间。我按照说明操作,但我可能不对。 到目前为止,这是我的代码:

room_list=[]

room = ["You are in a small bedroom! There is a door to the north.", 1, None, None, None]
room_list.append(room)
room = ["You are now in a bigger bedroom! There is a door to the East!", None, 2, None, None]
room_list.append(room)
room = [ "You are now in the North hall! There is a Door to the South!", None, None, 3, None]
room_list.append(room)
room = [" You are now in the South hall! There is a door to the East!", None, 4, None, None]
room_list.append(room)
room = [" You are now in the dining room but you smell something past the North door!", 5, None, None, None]
room_list.append(room)

current_room = 0


done = False
while not done:
   print(room_list[current_room][0])
   user_c = input("What direction do you want to go?")
   if user_c.lower() == "north":
      next_room = room_list[current_room][1]
      current_room = next_room
   elif user_c.lower() == "east":
      user_c = room_list[current_room][2]
   elif user_c.lower() == "south":
      user_c = room_list[current_room][3]
   elif user_c.lower() == "west":
      user_c = room_list[current_room][4]
   else:
      print("I dont understand.")
   if user_c == None :
      print("You can't go that way.")

一旦用户进入北室,我如何进入东室而不与其他if语句冲突? 为这个愚蠢的问题道歉,谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

您可能想尝试使用非常简单的类和词典。它们基本上就是你要找的东西。

class Room:
    def __init__(self, text, other_rooms=None):
        self.text = text
        self.other_rooms = other_rooms

    def set_other_rooms(self, other_rooms):
        self.other_rooms = other_rooms

rooms = [Room("You are in a small bedroom! There is a door to the north."),
         Room("You are now in a bigger bedroom! There is a door to the East!"),
         Room("You are now in the North hall! There is a Door to the South!"),
         Room("You are now in the South hall! There is a door to the East!"),
         Room("You are now in the dining room. You smell something past the North door!"),]

rooms[0].set_other_rooms( { "north":rooms[1],
                            "south":None,
                            "east":None,
                            "west":None } )

rooms[1].set_other_rooms( { "north":None,
                            "south":None,
                            "east":rooms[2],
                            "west":None } )

rooms[2].set_other_rooms( { "north":None,
                            "south":rooms[3],
                            "east":None,
                            "west":None } )

rooms[3].set_other_rooms( { "north":None,
                            "south":None,
                            "east":rooms[4],
                            "west":None } )

rooms[4].set_other_rooms( { "north":rooms[5],
                            "south":None,
                            "east":None,
                            "west":None } )

current_room = None
done = False

while not done:
    print(current_room.text)
    user_c = input("What direction do you want to go?").lower()
    if user_c == "exit":
        done = True
    elif current_room.other_rooms[ user_c ]:
        current_room = current_room.other_rooms[ user_c ]
    else:
        print("You can't go that way.")

另外,伙计,这是一个非常糟糕的教程。它可能有一些教学初学者 Python的优点,但如果您实际上是在Python中进行开发,那么它根本不是您想要做的。虽然网上学习Python确实有很多很棒的内容,但也有很多垃圾。承认没有最好的方法来做事,但同时尽量遵循最佳实践。这通常包括咨询Stack Exchange。

相关问题