[Tutor] Picking up Item()s

Andy W toodles@yifan.net
Mon, 18 Feb 2002 09:16:02 +0800


Hi Britt,

> I have the following code for a very very simple text adventure. Right
> now a player can move around a few rooms and there are a couple of
> objects in the room that one should be able to pick up.
>
> Unfortunately I'm stuck on this point: When a player enters a room it
> doesn't say "This room contains a key." Instead it says "This room
> contains  [<__main__.Item instance at 0x00AC5320>]". How can I make it
> so that it displays the name of the Item. Furthermore, I'd want the
> player to be able to type "get key" to pick up the key, not
> "__main__.Item instance at 0x00AC5320". How could I do this?
>
> Britt
>

This line here needs to be changed:
>                    print "This room contains ", rooms[playLoc].items

At the moment it's just a list of the instances, so we need to do something
to it. There's several ways, I'll show a couple.

List comprehension:
item_names=[item.name for item in rooms[playLoc].items]

Map:
item_names=map(lambda item: item.name, rooms[playLoc].items)

and then...
print "This room contains ".join(item_names)

For the second question...
>             if command[1] in rooms[playLoc].items.name:
You can use the item_names we constructed for the last problem.

I hope I answered your questions,
Andy

> import sys
> import string
>
> class Room:
>     def __init__(self, name):
>         self.name = name
>         self.items = []
>
>     def createExits(self, exits):
>         self.exits = exits
>
>     def itemsInRooms(self, item):
>         self.items.append(item)
>
> class Item:
>     def __init__(self, name):
>         self.name = name
>         self.inPosses = 0
>
> areas = ['porch', 'foyer', 'bathroom', 'mid_stairs', 'living',
> 'hallway']
> rooms = []
> for n in areas:
>     rooms.append(Room(n))
>
> rooms[0].createExits({'e':1})
> rooms[1].createExits({'e':5, 'w':0})
> rooms[2].createExits({'s':5})
> rooms[3].createExits({'n':5})
> rooms[4].createExits({'w':5})
> rooms[5].createExits({'e':4, 'n':2, 's':3, 'w':1})
>
> items = ['key', 'door']
> things = []
> for n in items:
>     things.append(Item(n))
>
> rooms[0].itemsInRooms(things[0])
> rooms[1].itemsInRooms(things[1])
>
> playLoc = 0
> words = ['n','s','e','w','q','get']
> command = []
>
> while 1:
>     command = string.split(raw_input("--> "))
>     if command[0] not in words:
>         print "Come again?"
>     else:
>         if command[0] == 'q':
>             sys.exit(1)
>         elif command[0] in ['n','s','e','w']:
>             if rooms[playLoc].exits.has_key(command[0]):
>                 playLoc = rooms[playLoc].exits.get(command[0])
>                 print "You find yourself in the " + rooms[playLoc].name
>                 print "You can move ", rooms[playLoc].exits.keys()
>                 if len(rooms[playLoc].items) != 0:
>                     print "This room contains ", rooms[playLoc].items
>             else:
>                 print "Your path is blocked."
>         else:
>             if command[1] in rooms[playLoc].items.name:
>                 print "You picked up the", command[1]
>                 for i in range(len(rooms[playLoc].items)):
>                     print i
> ##                    print rooms[playLoc].items[command[1]]
> ##                    print things[i].name
>
>                 rooms[playLoc].items.remove(command[1])
>             else:
>                 print "That item isn't in here!"
>
> =====
> "The ocean, she is strange and wondrous, filled with animals that disturb
even a Frenchman."
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Sports - Coverage of the 2002 Olympic Games
> http://sports.yahoo.com
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>