text adventure game problem
corvettecraz92 at gmail.com
corvettecraz92 at gmail.com
Tue Apr 8 22:14:26 EDT 2008
On Apr 8, 9:55 pm, André <andre.robe... at gmail.com> wrote:
> On Apr 8, 10:44 pm, corvettecra... at gmail.com wrote:
>
>
>
> > On Apr 8, 9: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é
>
> > 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()
More information about the Python-list
mailing list