python - 文字冒险室运动

标签 python text adventure

所以我一直在努力解决这个代码问题一段时间,但我似乎无法理解它。每当您尝试向任何方向移动时,它仍然会显示您被困在厨房里。我添加了一条打印当前房间行,这表明程序正在将其移动到其他房间,但没有打印正确的描述。

任何帮助都会很高兴!

提前谢谢。

class Room():
        room = []
        def __init__ ( self, desc, north , east , west , south, northwest, southwest, northeast, southeast):
            self.room.append(desc)
            self.room.append(north)
            self.room.append(east)
            self.room.append(west)
            self.room.append(south)
            self.room.append(northwest)
            self.room.append(southwest)
            self.room.append(northeast)
            self.room.append(southeast)
        def get_Desc(self):
            return self.room[0]
        def get_North(self):
            return self.room[1]
        def get_East(self):
            return self.room[2]
        def get_West(self):
            return self.room[3]
        def get_South(self):
            return self.room[4]
        def get_Northwest(self):
            return self.room[5]
        def get_Southwest(self):
            return self.room[6]
        def get_Northeast(self):
            return self.room[7]
        def get_Southeast(self):
            return self.room[8]



#Object Class
class Object():
    name = ""
    description = ""
    room = None
    def __init__ (self, newname, desc, newroom):
        self.name = newname
        self.description = desc
        self.room = newroom
    def get_Name():
        return name
    def set_Name(self , name):
        self.name = name
    def get_Desc(self):
        return description
    def set_Desc(self , description):
        self.description = description
    def get_Room(self):
        return self.description
    def set_Room(self, room):
        self.room = room




#This is a list of Objects in game
p_items = []
puppy = Object("Puppy", "OMG ROFL, LMAO, ITS CUTE AS ALL BALLS, GRAB IT AND SQUEEZE IT!!!", 2)
p_items.append(puppy)
Inventory = []
#This is a list of Rooms
room_list = []
room_1 = Room("This is the kitchen - 1. There is a passage to the North, East, and North East.", 3, 2, None, None , None, None, 7, None)
room_list.append (room_1)
room_2 = Room("This is the dining room - 2. There is a passage to the North, North East, and North West", 7, None, 1, None, 3, None, 6, None)
room_list.append(room_2)
room_3 = Room("This is the West hallway -3. There is a passage to the North, East, West, North East, North West, South East, and South West.", 4, 7, 1, None, 4, 1, 5, 2)
room_list.append(room_3)
room_4 = Room("This is the bedroom - 4. There is a passage to the East, West, and Southeast.", None, 5, 3, None, None, None, None, 7)
room_list.append(room_4)
room_5 = Room("This is the office -5. There is a passage to the South, West, Southwest, Northwest, and Northeast .", None, None, 7, 4 , 3, None, None, 6)
room_list.append(room_5)
room_6 = Room("This is the balcony. There is a passage to the West, North West, and South West.", None, None, None, 7, 5, 2, None, None )
room_list.append(room_6)
room_7 = Room("This is the East hallway. There is a passage to the North, South, and West, North East, North West, South East, and South West.", 5, None, 2, 3, 4, 1, 5, 2)
room_list.append(room_7)

current_room = 3
done = False
while not done:
    print (current_room)
    print ((room_list[current_room]).get_Desc()) 
    for object in p_items:
        if object.get_Room() == current_room:
            print("There is an Object in this room : " + object.get_Name() + " - " + object.get_Desc())
    answer = input("Which direction would you like to go? \n N, E, S, W, NE, SE, NW, SW, O to open Inventory, P to pick up an item, or Q to quit ").lower()
    if answer == ("q"):
        done = True
    elif answer == ("n") and room_list[current_room].get_North() != None:
        current_room = room_list [current_room].get_North() 
    elif answer == ("e") and room_list[current_room].get_East() != None:
        current_room = room_list[current_room].get_East() 
    elif answer == ("w") and room_list[current_room].get_West() !=None:
        current_room = room_list[current_room].get_West() 
    elif answer == ("s") and room_list[current_room].get_South() !=None:
        curent_room = room_list[current_room].get_South() 
    elif answer == ("nw") and room_list[current_room].get_Northwest() !=None:
        current_room = room_list[current_room].get_Northwest() 
    elif answer == ("sw") and room_list[current_room].get_Southwest() !=None:
        curent_room = room_list[current_room].get_Southwest() 
    elif answer == ("ne") and room_list[current_room].get_Northeast() !=None:
        current_room = room_list[current_room].get_Northeast() 
    elif answer == ("se") and room_list[current_room].get_Southeast() !=None:
        curent_room = room_list[current_room].get_Souteasth() 
    elif answer == ("o"):
        print ("\nYou have the following objects in your Inventory:")
        for object in inventory:
            print ( "-" + object.get_Name() + " ; " + object.get_Desc());
    elif answer == ("p"):
        print ("\nThere are these items in this room:")
        for object in inventory:
            if object.get_Room() == current_room:
                print ( "-" + object.get_Name() + " ; " + object.get_Desc());                
                pu = input("Would you like to pick it up? (y/n): ").lower()
                if  pu == "y":
                    object.set_Room(666)
                    inventory.append(object)
                elif pu == "n":
                    print ( "Dont be dumb, pick it up!")
                else:
                    print ("That is not a valid option")  

                    print ("That is not a valid option")

    else:
        print ("\nThat is not a valid option!\n") 
    print (answer)
    print (current_room)


 #No quiting allowed everrrrr

最佳答案

导致您问题的原因是您的Room类别。

class Room:
    room = []

这一行使 room 成为类级别列表(静态变量)。因此,每当您创建 Room 的新实例时,您都会将数据附加到此类级列表,但第一个索引将由您的游戏逻辑访问

def get_Desc(self):
    return self.room[0]

etc...

将始终是同一房间的值。

在方法内部创建时,列表不再位于类级别,并且您的问题已解决。

class Room():
    def __init__(self, desc, north, east, west, south, northwest, southwest, northeast, southeast):
        self.room = []

关于python - 文字冒险室运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34174901/

相关文章:

python - 循环遍历大表的两个字段

python - Tensorboard 在 Windows 操作系统上显示空白网页

iphone - 如何测试 UITextField 是否为零?

javascript - 将文本添加到容器以在调整大小时填充它

html - 文本宽度受图像限制

python - 将故事分配给游戏中的位置的最佳方式?

java - 如何延迟在 TextView 中显示文本(逐字逐句)

python - 在 C++ 库中调用 exit() 会终止使用 swig 包装该库的 python 脚本

python - 在python中分割文本文件

java - 用于解析简单的基于文本的数据文件的正则表达式