text adventure game problem

corvettecraz92 at gmail.com corvettecraz92 at gmail.com
Wed Apr 9 08:25:19 EDT 2008


On Apr 9, 1:24 am, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Tue, 8 Apr 2008 18:01:01 -0700 (PDT), corvettecra... at gmail.com
> declaimed the following in comp.lang.python:
>
> > okay, I'm having this one problem with a text adventure game. It's
> > kind of hard to explain, but I'll do my best.
> > [code]
>
> > def prompt_kitchen():
> >     global gold
> >     gold_taken = False
> >     while True:
> >         prompt_kit = raw_input('>')
> >         if prompt_kit == 'examine cabinet 1' and not gold_taken:
> >             print '''This cabinet has a lot of cups in it with all
> > different
> > designs and shapes. Where are the people anyway? How come there's
> > nobody here?
> > In one of the cups you find 8 gold.'''
>
>         Forgive me, but.... Ugh!
>
>         It looks like you are defining a function for each possible room
> which incorporates code for the actions possible in that room...
>
>         I suspect I'd try to create a generic room class which has as
> attributes a dictionary of actions and a dictionary of movable objects.
> Along with methods that work on objects... Working an action that digs
> into other objects may take some doing.
>
> class gobject(object):
>         def examine(self, item=None):
>                 if not item: item = self
>                 print "you look at the %s; you see %s" %              \
>                                  (item.desc,
>                                         ", ".join([k for k in item.subobjs.keys()]))
>         def go(self, direction):
>                 return moves[directions]
>         def take(self, item):
>                 itmobj = self.subobjs[item]
>                 del self.subobjs[item]
>                 return itmobj
>         def dropto(self, itemname, item):
>                 self.subobjs[itemname] = item
>
> class movobj(gobject):
>         def __init__(self, desc="a vague blob", subobjs=None, moves=None):
>                 ...
>
> class room(gobject):
>         def __init__(self, desc="a bare chamber",
>                                         subobjs=None, moves=None):
>                 ...
>
> g1 = movobj(desc="8 pieces of gold")
> c1 = movobj(desc="kitchen cabinet", subobjs={"gold" : g1})
>
> rooms = {"kichen" : room(desc="you are in the kitchen",
>                                                         subobjs={"cabinet" : c1},
>                                                         moves={"n" : rooms["dining"],
>                                                                         "out" : rooms["dining"}
>                 "dining" : room(desc="you are in the dining room",
>                                                         moves={"s" : rooms["kitchen"} }
>
> player = Player()
> player.location = rooms["dining"]
> player.run()
>
> {where run() incorporates the parser loop -- something like}
> while True:
>         self.location.examine()
>         command = raw_input("=> ")
>         words = command.split()
>         if words[0] in ["go", "move", "walk", "run"]:
>                 self.location = self.location.go(words[1])
>         elif words[0] in ["look", "examine", "study"]:
>                 self.location.examine(words[1])
>         elif words[0] in ["take"]:
>                 self.subobjs[words[1]] = self.location.take(words[1])
>         elif words[0] in ["drop", "leave"]:
>                 self.location.dropto(words[1], self.subobjs[words[1]])
>                 del self.subobjs[words[1]]
>         elif ...
>
> {of course, this ignores adding logic for failure to find the
> subobj/move in the relevant dictionary -- which should result in
>
>         I do not see that here
> or
>         I can not go that direction
>
> objects should also have attributes to indicate if they can be thrown
> (and what the results are), etc.
>
>         In the above the display should look something like (ignoring the
> unchecked for missing attributes)
>
> you look at the dining room, you see ? (need check for empty)
> =>
>
> go s
>
> you look at the kitchen, you see cabinet
> =>
>
> examine cabinet
>
> you look at the kitchen cabinet, you see gold
> =>
>
> --
>         Wulfraed        Dennis Lee Bieber               KD6MOG
>         wlfr... at ix.netcom.com             wulfr... at bestiaria.com
>                 HTTP://wlfraed.home.netcom.com/
>         (Bestiaria Support Staff:               web-a... at bestiaria.com)
>                 HTTP://www.bestiaria.com/

I can't even compile your code to see how it works, Dennis. I'm
confused about what that does.



More information about the Python-list mailing list