text adventure game problem

André andre.roberge at gmail.com
Tue Apr 8 21:39:39 EDT 2008


On Apr 8, 10:25 pm, André <andre.robe... at gmail.com> wrote:
> On Apr 8, 10:01 pm, corvettecra... at gmail.com 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
> > [/code]
>
> > 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é



More information about the Python-list mailing list