text adventure game problem
corvettecraz92 at gmail.com
corvettecraz92 at gmail.com
Fri Apr 11 10:16:37 EDT 2008
On Apr 11, 1:40 am, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Thu, 10 Apr 2008 05:06:42 -0700 (PDT), corvettecra... at gmail.com
> declaimed the following in comp.lang.python:
>
> > okay, that explains it...
> > could you provide a working example of a two-room game using your
> > method please so I can understand it better? Thanks in advance!
>
> <sigh> Okay... It isn't the best thought out system -- I have too
> many specific classes... It would probably be better to put actions into
> dictionaries and use some custom .getattr() to handle them.
>
> Will four rooms, a hidden chunk of gold, and one other movable
> object suffice? Watch out for news client line wrapping. The only
> parsing done is of the form: verb object; no attempt to handle: verb
> direct_object filler indirect_object
>
> -=-=-=-=-=-=-
> #
> # TAGS.py Text Adventure Game Shell
> #
>
> # I've probably defined too many special classes here
> class Gobject(object): #Game object
> def __init__(self, name, description, hidden=False, fixed=True):
> self._name = name
> self._description = description
> self.hidden = hidden #hidden objects are not visible to
> EXAMINE
> self.fixed = fixed #fixed objects can not be taken
> def _getName(self):
> return self._name
> name = property(_getName)
> def _getDescription(self):
> return self._description
> description = property(_getDescription)
>
> class Exit(Gobject):
> def __init__(self, description, target, hidden=False):
> super(Exit, self).__init__(None, description, hidden)
> self._target = target
> def _getTarget(self):
> return self._target
> target = property(_getTarget)
>
> class Room(Gobject): #rooms (container object)
> def __init__(self, name, description, exits=None, details=None):
> super(Room, self).__init__(name, description, hidden=False,
> fixed=True)
> self._exits = exits
> if details:
> self._details = details #other objects that are inside
> the room
> else:
> self._details = {}
> def setExits(self, exits):
> self._exits = exits
> def go(self, ext):
> return self._exits.get(ext, None)
> def setDetails(self, details):
> self._details = details
> def addDetail(self, itm):
> self._details[itm.name] = itm
> def delDetail(self, name):
> if (name in self._details and
> not self._details[name].fixed):
> itm = self._details[name]
> del self._details[name]
> else:
> itm = None
> return itm
> def examine(self, name=None):
> if not name: return self.description
> if (name in self._details
> and not self._details[name].hidden):
> return self._details[name].description
> else:
> return None
> def _detailedDescription(self):
> items = "nothing of interest"
> if self._details:
> items = ", ".join([itm.name for itm in
> self._details.values()
> if not itm.hidden])
> exits = ", ".join([ext for ext in self._exits.keys()
> if not self._exits[ext].hidden])
> #there must be at least one exit (the way you came in)
> return "%s. In the %s you see %s. Passages lead to %s." %
> (self._description,
> self.name,
> items,
> exits)
> description = property(_detailedDescription)
>
> class Thing(Gobject):
> def __init__(self, name, description, parent, hidden=False,
> fixed=False):
> super(Thing, self).__init__(name, description, hidden, fixed)
> self.parent = parent
> def take(self):
> if self.fixed:
> return None
> else:
> self.hidden = False #if taken, one now can see it
> return self.parent.delDetail(self.name)
> def drop(self, parent):
> self.parent.delDetail(self.name)
> parent.addDetail(self)
>
> class TriggerThing(Thing):
> def __init__(self, name, description, parent,
> hidden=False, fixed=True, triggers=None):
> super(TriggerThing, self).__init__(name, description, parent,
> hidden, fixed)
> self._triggers = triggers
> def _detailedDescription(self):
> if self._triggers:
> items = ", ".join([itm.name for itm in
> self.parent._details.values()
> if itm in self._triggers
> and itm.hidden])
> else:
> items = ", ".join([itm.name for item in
> self.parent._details.values()
> if itm.hidden])
> if not items: items = "nothing of interest"
> return "%s. In the %s you see %s." % (self._description,
> self.name,
> items)
> description = property(_detailedDescription)
>
> class Money(Thing):
> # note: I've not worked out how to safely handle combining money
> objects
> def __init__(self, name, description, amount, parent, hidden=False,
> fixed=False):
> super(Money, self).__init__(name, description, parent, hidden,
> fixed)
> self._value = amount
> def _detailedDescription(self):
> return "%s pieces of gold" % self._value
> description = property(_detailedDescription)
> def combine(self, money):
> self._value += money._value
>
> class Avatar(Gobject):
> def __init__(self, name, description, location, hidden=True,
> fixed=True):
> super(Avatar, self).__init__(name, description, hidden, fixed)
> self.location = location
> self._inventory = {}
> def addInv(self, itm):
> itm.hidden = False
> if itm.name in self._inventory: #presume only gold is
> duplicated
> self._inventory[itm.name].combine(itm)
> del itm
> else:
> self._inventory[itm.name] = itm
> def remInv(self, name):
> itm = self._inventory.get(name, None)
> if itm: del self._inventory[name]
> return itm
> def inv(self):
> return ", ".join([itm for itm in self._inventory.keys()])
>
> #create the universe -- first the raw rooms
> room1 = Room("empty room",
> "a completely empty room")
> room2 = Room("time passages",
> "a fourth-dimensional room having no fixed shape or size")
> room3 = Room("control room",
> "the control room of a TARDIS")
> room4 = Room("locker room",
> "a room full of storage spaces")
>
> #create each exit
> exit1_2 = Exit("you squeeze through the mouse hole", room2)
> exit1_4 = Exit("you take the doorway", room4)
> exit2_3 = Exit("the TARDIS door opens and you pass in", room3,
> hidden=True)
> exit3_2 = Exit("you leave the TARDIS", room2)
> exit3_1 = Exit("you sneak deeper into the depths of the TARDIS", room1)
> exit4_1 = Exit("you move through the door", room1)
> exit2_1 = Exit("you take the wormhole out", room1)
> exit2_4 = Exit("you follow the light", room4)
> exit4_2 = Exit("you follow the shadow", room2)
> exit2_2a = Exit("as you enter the reddish passage you see yourself
> leaving the room", room2)
> exit2_2b = Exit("you feel a bit older as you take the blue passage",
> room2)
> exit2_2c = Exit("you feel confused as you cross webs of the timestream",
> room2)
>
> #connect rooms to exits
> room1.setExits({"hole" : exit1_2,
> "door" : exit1_4})
> room2.setExits({"tardis" : exit2_3,
> "wormhole" : exit2_1,
> "light" : exit2_4,
> "past" : exit2_2a,
> "future" : exit2_2b,
> "sideways" : exit2_2c})
> room3.setExits({"out" : exit3_2,
> "in" : exit3_1})
> room4.setExits({"door" : exit4_1,
> "shadow" : exit4_2})
>
> #create a few non-room objects
> container1 = Thing("closet", "an empty closet", room4, fixed=True)
> gold = Money("gold", None, 8, room4, hidden=True)
> container2 = TriggerThing("footlocker", "a fancy carved captain's
> chest", room4,
> fixed=True, triggers=[gold])
>
> room4.setDetails({container1.name : container1,
> container2.name : container2,
> gold.name : gold})
>
> #the tardis is both an exit and a thing
> tardis = Thing("tardis", "a beat-up type 40 TARDIS", room2, fixed=True)
> room2.setDetails({tardis.name : tardis})
>
> screwdriver = Thing("screwdriver", "a well worn sonic screwdriver",
> room3)
> room3.setDetails({screwdriver.name : screwdriver})
>
> player = Avatar("Wulfraed", "a nondescript individual", room1)
>
> #note: no end conditions are defined
> while True:
> print player.location.description
> while True:
> cmdstr = raw_input("command> ").strip().lower()
> if cmdstr: break
> words = cmdstr.split()
> verb = words[0]
> if len(words) > 1:
> objct = words[1]
> else:
> objct = None
> if verb in ["look", "examine"]:
> if objct:
> desc = player.location.examine(objct)
> else:
> desc = player.location.examine()
> if desc:
> print desc
> else:
> print "I don't see that here"
> elif verb in ["grab", "take"]:
> if objct:
> itm = player.location.delDetail(objct)
> if itm:
> player.addInv(itm)
> print "%s taken" % itm.name
> else:
> print "I can not %s the %s" % (verb, objct)
> else:
> print "%s what?" % verb
> elif verb in ["drop", "leave"]:
> if objct:
> itm = player.remInv(objct)
> if itm:
> player.location.addDetail(itm)
> print "%s dropped" % itm.name
> else:
> print "I don't have the %s" % objct
> else:
> print "%s what?" % verb
> elif verb in ["walk", "run", "go"]:
> if objct:
> ext = player.location.go(objct)
> if ext:
> print ext.description
> player.location = ext.target
> else:
> print "I can't go that way"
> else:
> print "%s where?" % verb
> elif verb...
>
> read more »
I still can't run that....after fixing the simple stuff like 'invalid
syntax', there's the "Name examine is not defined."
So...
More information about the Python-list
mailing list