文本冒险室运动

时间:2015-12-09 09:19:19

标签: 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

1 个答案:

答案 0 :(得分:3)

导致您的问题的是您的会议室课程。

class 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 = []