0

I am currently creating my first text based adventure game in Python. I am using Mu 1.0.3 to write the code. My game is a 'haunted mansion' with various rooms to quest through. I have created the rooms, linked them, and added some items, as well as some NPCs. My problem:

How do I add multiple items to one room? I have tried but all that happens is the original item disappears, to be replaced by the newest one, so only one item appears in each room. I am basically a beginner so don't really know where to go from here! I have been using an online Future Learn course so far but it does not have the answer to my question and I cannot find an answer elsewhere. Any help would be much appreciated! Thank you.

Here is my Room code so far:

class Room():
    number_of_rooms = 0
    def __init__(self, room_name):
        self.name = room_name
        self.description = None
        #create new attribute
        #ask dictionary for a specific element by name
        #the {} creates an empty dictionary
        self.linked_rooms = {}
        self.character = None
        self.item = None 

        Room.number_of_rooms = Room.number_of_rooms + 1

    def set_description(self, room_description):
        self.description = room_description

    def get_description(self):
        return self.description

    def describe(self):
        print(self.description)

    def set_name(self, room_name):
        self.name = room_name

    def get_name(self):
        return self.name

    def set_character(self, new_character):
        self.character = new_character

    def get_character(self):
        return self.character

    def set_item(self, new_item):
        self.item = new_item

    def get_item(self):
        return self.item

    #link room method
    #object, room object linked to, relative direction of object
    def link_room(self, room_to_link, direction):
        self.linked_rooms[direction] = room_to_link

    def get_details(self):
        print(self.name)
        print("--------------------")
        print(self.description)
        for direction in self.linked_rooms:
            room = self.linked_rooms[direction]
            print("The " + room.get_name() + " is " + direction)

    def move(self, direction):
        if direction in self.linked_rooms:
            return self.linked_rooms[direction]
        else:
            print("You cannot go that way")
            return self

Here is my Item code so far:

class Item():
    def __init__(self, item_name):
        self.name = item_name
        self.description = None

    def set_description(self, item_description):
        self.description = item_description

    def get_description(self):
        return self.description

    def set_name(self, item_name):
        self.name = item_name

    def get_name(self):
        return self.name

    def describe(self):
        print("You have found [" + self.name + "] in here - " + self.description)
  • Where exactly do you add multiple items? – M-Chen-3 Jan 30 '21 at 16:49
  • 2
    How about having a list of `items` in `Room` instead of a single `item` attribute? – khelwood Jan 30 '21 at 16:49
  • Looks like Mu has some [debugging features](https://codewith.mu/en/tutorials/1.0/debugger) right now while you are learning to code you should start learning how to use those features - that experiance/knowledge will transfer to other IDE's in the future if needed. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Jan 30 '21 at 17:18
  • `self.item = None` --> `self.item = []` , `self.item = new_item` --> `self.item.append(new_item)`. And maybe change the method name to `add_item` instead of `set_item`. – wwii Jan 30 '21 at 17:29
  • Thank you ever so much all, a list is a far better idea. The debugging link is also very helpful. – whatever2021 Feb 02 '21 at 18:19

0 Answers0