text adventure game problem

C

corvettecraz92

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.'''
            gold = gold+8
            gold_taken = True
            pass4()
        elif prompt_kit == 'examine cabinet 1' and 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?'''
            pass4()

def pass4():
    global gold
    print 'You have', gold, 'gold'
    pass

Okay, now for my problem.
In the above function, there's the option to examine a cabinet and get
8 gold. (everyone here knows that...but I'm just trying to state my
problem...)
Unfortunately, it kind of doesn't work.
After the first time I 'examine cabinet 1' in my game, I get 8 gold
and I can't get it again.
But, If I leave the room and come back to it, then it's as if I had
never gotten the gold the first time, and I can get it again.
How do I fix this?
 
D

Dan Bishop

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.'''
gold = gold+8
gold_taken = True
pass4()
elif prompt_kit == 'examine cabinet 1' and 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?'''
pass4()

def pass4():
global gold
print 'You have', gold, 'gold'
pass

Okay, now for my problem.
In the above function, there's the option to examine a cabinet and get
8 gold. (everyone here knows that...but I'm just trying to state my
problem...)
Unfortunately, it kind of doesn't work.
After the first time I 'examine cabinet 1' in my game, I get 8 gold
and I can't get it again.
But, If I leave the room and come back to it, then it's as if I had
never gotten the gold the first time, and I can get it again.

That's because your function starts with "gold_taken = False".

The easiest fix is to make gold_taken a global variable initialized
outside the function, like you're already doing with gold.
 
A

André

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.'''
            gold = gold+8
            gold_taken = True
            pass4()
        elif prompt_kit == 'examine cabinet 1' and 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?'''
            pass4()

def pass4():
    global gold
    print 'You have', gold, 'gold'
    pass

Okay, now for my problem.
In the above function, there's the option to examine a cabinet and get
8 gold. (everyone here knows that...but I'm just trying to state my
problem...)
Unfortunately, it kind of doesn't work.
After the first time I 'examine cabinet 1' in my game, I get 8 gold
and I can't get it again.
But, If I leave the room and come back to it, then it's as if I had
never gotten the gold the first time, and I can get it again.
How do I fix this?

quick guess: define gold_taken as a global variable and initialize it
outside of the function.

Warning: avoid global variables if at all possible.

;-)
André
 
A

André

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:
[/QUOTE]
[QUOTE]
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.'''
            gold = gold+8
            gold_taken = True
            pass4()
        elif prompt_kit == 'examine cabinet 1' and 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?'''
            pass4()[/QUOTE]
[QUOTE]
def pass4():
    global gold
    print 'You have', gold, 'gold'
    pass
Okay, now for my problem.
In the above function, there's the option to examine a cabinet and get
8 gold. (everyone here knows that...but I'm just trying to state my
problem...)
Unfortunately, it kind of doesn't work.
After the first time I 'examine cabinet 1' in my game, I get 8 gold
and I can't get it again.
But, If I leave the room and come back to it, then it's as if I had
never gotten the gold the first time, and I can get it again.
How do I fix this?

quick guess: define gold_taken as a global variable and initialize it
outside of the function.

Warning: avoid global variables if at all possible.

;-)
André

Actually, what I would do if I were designing such a game is probably
define an object with various states, so that instead of gold_taken,
I'd have
state.gold_taken_in_cabinet_1

Alternatively, you could define a dict at the beginning with things
like
gold_taken = {'cabinet 1': False,
'cabinet 2': False, ...}

This approach would allow to identify at a glance all relevant game
situations rather than having to go through the entire code.

André
 
C

corvettecraz92

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:
[/QUOTE]
[QUOTE]
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.'''
            gold = gold+8
            gold_taken = True
            pass4()
        elif prompt_kit == 'examine cabinet 1' and 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?'''
            pass4()[/QUOTE]
[QUOTE]
def pass4():
    global gold
    print 'You have', gold, 'gold'
    pass
Okay, now for my problem.
In the above function, there's the option to examine a cabinet and get
8 gold. (everyone here knows that...but I'm just trying to state my
problem...)
Unfortunately, it kind of doesn't work.
After the first time I 'examine cabinet 1' in my game, I get 8 gold
and I can't get it again.
But, If I leave the room and come back to it, then it's as if I had
never gotten the gold the first time, and I can get it again.
How do I fix this?

quick guess: define gold_taken as a global variable and initialize it
outside of the function.

Warning: avoid global variables if at all possible.

;-)
André

Here's a sample code that, in fact, does work. In this code, when run,
I can only get the gold once.

def prompt_house():
global gold
gold_taken = False
while True:
prompt_hou = raw_input('>')
if prompt_hou == 'examine table' and not gold_taken:
print \
'''There are a lot of car magazines here.
You flip through them and find 5 gold.
'''
gold = gold+5
gold_taken = True
elif prompt_hou == 'go west':
# this gets you out of the loop
go_west()
# more elif choices here ...
elif prompt_hou == 'examine table' and gold_taken:
print '''There are a lot of car magazines here.'''
go_west()
def go_west():
# just a dummy funk
global gold
print gold
pass
# test
gold = 0
prompt_house()

But what's the difference between this and the one that I posted?
 
A

André

On Apr 8, 10:01 pm, (e-mail address removed) wrote:
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.'''
            gold = gold+8
            gold_taken = True
            pass4()
        elif prompt_kit == 'examine cabinet 1' and 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?'''
            pass4() 
def pass4():
    global gold
    print 'You have', gold, 'gold'
    pass
Okay, now for my problem.
In the above function, there's the option to examine a cabinet and get
8 gold. (everyone here knows that...but I'm just trying to state my
problem...)
Unfortunately, it kind of doesn't work.
After the first time I 'examine cabinet 1' in my game, I get 8 gold
and I can't get it again.
But, If I leave the room and come back to it, then it's as if I had
never gotten the gold the first time, and I can get it again.
How do I fix this?
quick guess: define gold_taken as a global variable and initialize it
outside of the function.
Warning: avoid global variables if at all possible.
;-)
André

Here's a sample code that, in fact, does work. In this code, when run,
I can only get the gold once.

def prompt_house():
    global gold
    gold_taken = False
    while True:
        prompt_hou = raw_input('>')
        if prompt_hou == 'examine table' and not gold_taken:
            print \
                  '''There are a lot of car magazines here.
You flip through them and find 5 gold.
'''
            gold = gold+5
            gold_taken = True
        elif prompt_hou == 'go west':
            # this gets you out of the loop


The above comment is wrong.
            go_west()
            # more elif choices here ...
        elif prompt_hou == 'examine table' and gold_taken:
            print '''There are a lot of car magazines here.'''
            go_west()
def go_west():
# just a dummy funk
    global gold
    print gold
    pass

The "pass" statement is redundant.
                # test
gold = 0
prompt_house()

But what's the difference between this and the one that I posted?

It is hard to say as you are not posting the entire code. As I
indicated above, you wrote a comment indicating that a given choice
was taking you out of the loop - which could only happen through a
break statement. You may want to post a ("small") code sample that
can be run by itself and reproduces the problem behaviour you
observe. The sample you posted include infinite loops with no way to
get out, so your original claim that you could leave the room and come
back is highly suspicious ;-)

André
 
C

corvettecraz92

On Apr 8, 10:01 pm, (e-mail address removed) wrote:
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.'''
            gold = gold+8
            gold_taken = True
            pass4()
        elif prompt_kit == 'examine cabinet 1' and 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?'''
            pass4() 
def pass4():
    global gold
    print 'You have', gold, 'gold'
    pass
Okay, now for my problem.
In the above function, there's the option to examine a cabinet and get
8 gold. (everyone here knows that...but I'm just trying to state my
problem...)
Unfortunately, it kind of doesn't work.
After the first time I 'examine cabinet 1' in my game, I get 8 gold
and I can't get it again.
But, If I leave the room and come back to it, then it's as if I had
never gotten the gold the first time, and I can get it again.
How do I fix this?
quick guess: define gold_taken as a global variable and initialize it
outside of the function.
Warning: avoid global variables if at all possible.
;-)
André
Here's a sample code that, in fact, does work. In this code, when run,
I can only get the gold once.
def prompt_house():
    global gold
    gold_taken = False
    while True:
        prompt_hou = raw_input('>')
        if prompt_hou == 'examine table' and not gold_taken:
            print \
                  '''There are a lot of car magazines here.
You flip through them and find 5 gold.
'''
            gold = gold+5
            gold_taken = True
        elif prompt_hou == 'go west':
            # this gets you out of the loop

The above comment is wrong.
            go_west()
            # more elif choices here ...
        elif prompt_hou == 'examine table' and gold_taken:
            print '''There are a lot of car magazines here.'''
            go_west()
def go_west():
# just a dummy funk
    global gold
    print gold
    pass

The "pass" statement is redundant.
                # test
gold = 0
prompt_house()
But what's the difference between this and the one that I posted?

It is hard to say as you are not posting the entire code.  As I
indicated above, you wrote a comment indicating that a given choice
was taking you out of the loop - which could only happen through a
break statement.  You may want to post a ("small") code sample that
can be run by itself and reproduces the problem behaviour you
observe.  The sample you posted include infinite loops with no way to
get out, so your original claim that you could leave the room and come
back is highly suspicious ;-)

André

Here ya go...this is an excerpt from my main code, with an example
room added on.
gold = 0
def kitchen():
print 'you have', gold, 'gold'
print '''You are in the kitchen of the house. There is a lot of
cooking
equipment here, along with 3 cabinets, a food pantry, and a drawer. At
the far end of the
room is an icebox and a stove. To the south there is a living room,
and to
the east is a den.'''
print
prompt_kitchen()
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.'''
gold = gold+8
gold_taken = True
pass4()
elif prompt_kit == 'examine cabinet 1' and 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?'''
pass4()

elif prompt_kit == 'south':
extra_room()

def extra_room():
print 'you have', gold, 'gold'
print 'This is a dead end room. Go north.'
kitchen()

def pass4():
global gold
print 'You have', gold, 'gold'
pass

kitchen()
 
T

Terry Reedy

| In the above function, there's the option to examine a cabinet and get
| 8 gold. (everyone here knows that...but I'm just trying to state my
| problem...)
| Unfortunately, it kind of doesn't work.
| After the first time I 'examine cabinet 1' in my game, I get 8 gold
| and I can't get it again.
| But, If I leave the room and come back to it, then it's as if I had
| never gotten the gold the first time, and I can get it again.
| How do I fix this?

I would define a container class. The init function gives it a name and
contents (8 gold, for instance). Give kitchen a container('cabinet', 8).
Give containers a .examine() method which gives the contents to the player.
and a message which varies with the contents. You can even make some
container 'refreshable' if you want.

tjr
 
C

Carl Banks

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:
[/QUOTE]
[QUOTE]
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.'''[/QUOTE]

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.[/QUOTE]
[snip]


I don't know about you, but I usually start with specific cases like
this and then generalize them.  This could be a baby step, or merely a
first stage of evolution.


Carl Banks
 
C

corvettecraz92

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:
[/QUOTE]
[QUOTE]
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.'''[/QUOTE]

        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
        [email protected]             [email protected]
                HTTP://wlfraed.home.netcom.com/
        (Bestiaria Support Staff:               [email protected])
                HTTP://www.bestiaria.com/[/QUOTE]

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

corvettecraz92

        My apologies -- it was spur of the moment pseudo-code to illustrate
the concepts, but was never meant to be directly executable.

        The concepts are that: all rooms are basically the same -- they have
a list of objects that exist within the room (and, as I now realize, the
"gold within the cabinet/cup" would still be an object within the room
-- but would have an attribute "hidden=True" which would control if it
is itemized when one "examines" the room), a list of exits, and a maybe
a list of actions which can be performed in the room or on named objects
(though I see the actions being common methods invoked by the command
parser -- I illustrated a simple "verb object" command, but a good
parser should probably accept more complex commands "throw <object1> at
<object2>" for example). Uh, I'm saying "list", but "dictionary" would
be the likely implementation.

        When one "examines" a room, one essentially gets back a list of the
objects that are in the room instance. When one "examines" one of those
objects (by name), one gets back the detailed description of that
object. If one implements a "hidden" attribute, that object will not be
listed in the higher level "examine". That "gold" inside another object
does get a bit tricky -- I now see it as a hidden object in the room,
but the description of the object has to be somewhat dynamic -- that is,
if the object (cabinet) sees that the "gold" is in the room, examining
the object will report the gold. But a "take gold" command still acts on
the room -- removing the gold from the room inventory, putting it into
the player inventory, and changing the "hidden" property so it is now
visible.

        When one does a  "move <direction>", the player's "location"
attribute is changed from the current room to the room connected to that
direction.

        For both objects and directions, if the player enters a term that is
not in the relevant dictionary, a suitable message is returned to the
user. Oh, room objects, besides that "hidden" attribute, may have a
"portable" attribute which controls if one can "take" the object... Or
an attribute for "throwable" which controls if one can throw an object
at another (and the other would have a method/action for when something
is thrown at it -- a default would be "No effect").

        From what I saw of your small sample, you were treating each room as
a discrete function. Moving from one room to another means calling the
new room's function. But that means you are continuously nesting deeper
in the Python stack, and someone that keeps moving back and forth
between two rooms will eventually exceed the Python stack limit.

        By making the "current location" an attribute of a "player
instance", no recursion is involved -- you move between rooms by simply
assigning the new room as the "current location".

--
        Wulfraed        Dennis Lee Bieber               KD6MOG
        (e-mail address removed)             (e-mail address removed)
                HTTP://wlfraed.home.netcom.com/
        (Bestiaria Support Staff:               (e-mail address removed))
                HTTP://www.bestiaria.com/


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!
 
T

Tommy Nordgren

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[/QUOTE]
	Python is not a suitable language for Text Adventure Development.
You should use one of the several excellent free text adventure  
languages instead.
In particular languages like TADS (The text adventure development  
system) have strong
built-in support for the tricky command parsing.
-----------------------------------
See the amazing new SF reel: Invasion of the man eating cucumbers from  
outer space.
On congratulations for a fantastic parody, the producer replies :  
"What parody?"

Tommy Nordgren
[email protected]
 
C

Carl Banks

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:
[/QUOTE]
[QUOTE]
def prompt_kitchen():
global gold[/QUOTE]

Python is not a suitable language for Text Adventure Development. 
Ridiculous.


You should use one of the several excellent free text adventure
languages instead.
In particular languages like TADS (The text adventure development
system) have strong
built-in support for the tricky command parsing.[/QUOTE]

There are many good reasons why someone might want to use a general
purpose language like Python to write a text adventure, such as so
they're not stuck with a quasi hack of a language if they have to do
something that doesn't fit the framework anticipated by the language
designer.



Carl Banks
 
C

corvettecraz92

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...
 
C

corvettecraz92

        <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...

I still can't run that....after fixing the simple stuff like 'invalid
syntax', there's the "Name examine is not defined."
So...
 
N

Neil Cerutti

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:
[/QUOTE]
[QUOTE]
def prompt_kitchen():
global gold [/QUOTE]

Python is not a suitable language for Text Adventure Development.[/QUOTE]

Ridiculous.[/QUOTE]

Agreed. "Not suitable" is an exaggeration. However, designing and
implementing your own adventure game framework in Python is a total
waste of time. Don't do it except for the sake of it.

You can even claim one of several Python-based frameworks out of
mothballs as a starting point, if you want to use Python.
[QUOTE]
There are many good reasons why someone might want to use a general
purpose language like Python to write a text adventure, 
Yes.

such as so
they're not stuck with a quasi hack of a language if they have to do
something that doesn't fit the framework anticipated by the language
designer.[/QUOTE]

That's not a reason, it's FUD.
 
G

Gabriel Genellina

You will have to manually fix the long lines that were splitted/wrapped by
the mailer program. As a rule-of-thumb, try joining any line (inside a
function or class) that you see over the left margin, onto the previous
line.

By example, the EXAMINE word above should be the last word on its previous
line, in fact it's part of the comment.
 
S

Sean DiZazzo

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.'''
            gold = gold+8
            gold_taken = True
            pass4()
        elif prompt_kit == 'examine cabinet 1' and 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?'''
            pass4()

def pass4():
    global gold
    print 'You have', gold, 'gold'
    pass

Okay, now for my problem.
In the above function, there's the option to examine a cabinet and get
8 gold. (everyone here knows that...but I'm just trying to state my
problem...)
Unfortunately, it kind of doesn't work.
After the first time I 'examine cabinet 1' in my game, I get 8 gold
and I can't get it again.
But, If I leave the room and come back to it, then it's as if I had
never gotten the gold the first time, and I can get it again.
How do I fix this?

Thank you! Just downloaded and am about to have a blast into the past
with PlanetFall!
 
C

Carl Banks

On 9 apr 2008, at 03.01, (e-mail address removed) wrote:
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 
Python is not a suitable language for Text Adventure Development.[/QUOTE][/QUOTE]
[QUOTE]
Ridiculous.[/QUOTE]

Agreed. "Not suitable" is an exaggeration. However, designing and
implementing your own adventure game framework in Python is a total
waste of time.[/QUOTE]

That depends.
[QUOTE]
Don't do it excet for the sake of it.

You can even claim one of several Python-based frameworks out of
mothballs as a starting point, if you want to use Python.


That's not a reason, it's FUD.[/QUOTE]

No, it's prudence.

If I am writing a banal, been-done-a-million-times before, cookie-
cutter text adventure, yes I'd just use a domain-specific language.

If I am writing a unique game that does new things, sooner or later
I'm going to want to do something the designer of the domain-specific
language and framework didn't anticipate.  And there is no uncertainty
or doubt about this: when it comes to that, I don't want to be
restrained by a narrow framework or domain-specific language, and I
would want to be using Python, and it isn't even remotely close.

The only question would be whether there's enough general purpose
programming required that it would outweigh the extra work.  Which,
let's be honest, is not all that much for a text adventure.


Carl Banks
 
N

Neil Cerutti

No, it's prudence.

If I am writing a banal, been-done-a-million-times before, cookie-
cutter text adventure, yes I'd just use a domain-specific language.

If I am writing a unique game that does new things, sooner or later
I'm going to want to do something the designer of the domain-specific
language and framework didn't anticipate. And there is no uncertainty
or doubt about this: when it comes to that, I don't want to be
restrained by a narrow framework or domain-specific language, and I
would want to be using Python, and it isn't even remotely close.

The only question would be whether there's enough general purpose
programming required that it would outweigh the extra work. Which,
let's be honest, is not all that much for a text adventure.

Thanks for the more detailed response.

Your reasoning is valid, but your understanding of the properties of
existing text game frameworks seem to be be out of date.

A usable, extensible, customizable text adventure library for use in
any general-purpose programming language is still a pipe dream.
There aren't even very many failed attempts.

The decent text adventures composed from scratch in a general-purpose
programming language are rare exceptions to the rule. Even among the
tolerable ones, I have personally never seen one that took advantage
of the general-purpose nature of its implementation language to do
something that couldn't have been easily done in the handful of mature
domain-specific languages.

Moreover, the small, but dedicated text-adventure-playing internet
community has developed expectations for the features, infrastructure
and portability of their text adventures that are not trivial to meet
when starting from (close to) ground zero. A few bold pioneers have
managed it. If you're keen to attempt it, Python is an OK choice. The
most recent success I know of was "Aunts & Butlers" [1], composed in
Javascript, which made portability less of an issue than it would be
for Python. There are no completed text adventures in the wild
composed in Python, that I'm aware of. That doesn't mean it can't
happen.

[1] http://versificator.co.uk/auntsandbutlers/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top