text adventure game problem

Discussion in 'Python' started by corvettecraz92, Apr 9, 2008.

  1. 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 (Text):


    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?
     
    corvettecraz92, Apr 9, 2008
    #1
    1. Advertisements

  2. corvettecraz92

    Dan Bishop Guest

    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.
     
    Dan Bishop, Apr 9, 2008
    #2
    1. Advertisements

  3. corvettecraz92

    André Guest

    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é
     
    André, Apr 9, 2008
    #3
  4. corvettecraz92

    André Guest

    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é
     
    André, Apr 9, 2008
    #4
  5. 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?
     
    corvettecraz92, Apr 9, 2008
    #5
  6. corvettecraz92

    André Guest


    The above comment is wrong.
    The "pass" statement is redundant.
    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é
     
    André, Apr 9, 2008
    #6
  7. 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()
     
    corvettecraz92, Apr 9, 2008
    #7
  8. corvettecraz92

    Terry Reedy Guest

    | 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
     
    Terry Reedy, Apr 9, 2008
    #8
  9. corvettecraz92

    Carl Banks Guest

     
    Carl Banks, Apr 9, 2008
    #9
  10.  
    corvettecraz92, Apr 9, 2008
    #10

  11. 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!
     
    corvettecraz92, Apr 10, 2008
    #11
  12.  
    Tommy Nordgren, Apr 10, 2008
    #12
  13. corvettecraz92

    Carl Banks Guest

     
    Carl Banks, Apr 11, 2008
    #13
  14. I still can't run that....after fixing the simple stuff like 'invalid
    syntax', there's the "Name examine is not defined."
    So...
     
    corvettecraz92, Apr 11, 2008
    #14
  15. I still can't run that....after fixing the simple stuff like 'invalid
    syntax', there's the "Name examine is not defined."
    So...
     
    corvettecraz92, Apr 11, 2008
    #15
  16. corvettecraz92

    Neil Cerutti Guest

     
    Neil Cerutti, Apr 11, 2008
    #16
  17. 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.
     
    Gabriel Genellina, Apr 14, 2008
    #17
  18. corvettecraz92

    Sean DiZazzo Guest

    Thank you! Just downloaded and am about to have a blast into the past
    with PlanetFall!
     
    Sean DiZazzo, Apr 15, 2008
    #18
  19. corvettecraz92

    Carl Banks Guest

     
    Carl Banks, Apr 15, 2008
    #19
  20. corvettecraz92

    Neil Cerutti Guest

    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/
     
    Neil Cerutti, Apr 16, 2008
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.