patternpythonMinor
Text adventure and global variables
Viewed 0 times
globaltextadventurevariablesand
Problem
I'm working on this little text based game. Up to this point, I've gotten it to work, however I've heard I shouldn't be using global like I am. I have this set up to modify the global variables because I want the player to revisit a place and all the events have still been triggered.
I've just started programming, so I don't have knowledge of lots of concepts. The tutorials I've been reading include Learning Python the Hard Way.
I've just started programming, so I don't have knowledge of lots of concepts. The tutorials I've been reading include Learning Python the Hard Way.
print "You are a lone adventurer with the bounty to clear out the crypt."
print "You come with nothing but your sword and a compass."
print "You enter the crypt."
print "To the north you have a gated portaculas and rooms to the west and east."
print "What do you do?"
gate = False
lever = False
def entrance():
global gate
while True:
choice = raw_input('> ')
if choice == 'west':
guard_shack()
elif choice == 'east':
lever_rm()
elif choice == 'north' and gate == False:
print "The gate is still closed and locked."
entrance()
elif choice == 'north' and gate == True:
fountain_rm()
else:
entrance(gate)
def lever_rm():
global lever
global gate
print "You enter the room."
print "What do you do"
while True:
choice = raw_input('> ')
if 'search' in choice:
print "You look around the room and notice a lever on the opposite wall."
elif "pull lever" in choice:
print "You pull the lever."
lever = True
gate = True
elif choice == 'west':
entrance()
else:
print '> '
def fountain_rm():
print "you're in the lever room!"
entrance()Solution
It will make sense to use OOP in your implementation.
You can model the crypt as a
there
and
Transforming your implementation is quite easy, follow this pattern:
To learn more about classes and OOP in Python,
I recommend the official tutorial.
It's not a good practice to run code in the global namespace,
because if you try to import this module from another module,
all that code will be executed immediately.
The right way to do this in Python is to add this conditional:
This condition will only be true when the file is executed as a Python script.
Don't use the
Instead of
in case of
This looks like a bug:
Because in your declaration the
You can model the crypt as a
Crypt class,there
gate and lever can be attributes,and
entrance, lever_room can be methods.Transforming your implementation is quite easy, follow this pattern:
class Crypt(object):
def __init__(self):
self.gate = False
self.lever = False
def entrance(self):
while True:
choice = raw_input('> ')
if choice == 'west':
self.guard_shack()
elif choice == 'east':
self.lever_rm()
elif choice == 'north' and not self.gate:
print "The gate is still closed and locked."
self.entrance()
elif choice == 'north' and self.gate:
self.fountain_rm()
else:
self.entrance()
# ...
def main():
crypt = Crypt()
crypt.welcome()
crypt.entrance()To learn more about classes and OOP in Python,
I recommend the official tutorial.
It's not a good practice to run code in the global namespace,
because if you try to import this module from another module,
all that code will be executed immediately.
The right way to do this in Python is to add this conditional:
if __name__ == '__main__':
main()This condition will only be true when the file is executed as a Python script.
Don't use the
== operator to check the value of boolean variables and expressions like this:elif choice == 'north' and gate == False:
print "The gate is still closed and locked."
entrance()
elif choice == 'north' and gate == True:
fountain_rm()Instead of
x == True you can use the boolean variable directly,in case of
x != False you should use not x, for example:elif choice == 'north' and not gate:
print "The gate is still closed and locked."
entrance()
elif choice == 'north' and gate:
fountain_rm()This looks like a bug:
else:
entrance(gate)Because in your declaration the
entrance method doesn't take any parameters.Code Snippets
class Crypt(object):
def __init__(self):
self.gate = False
self.lever = False
def entrance(self):
while True:
choice = raw_input('> ')
if choice == 'west':
self.guard_shack()
elif choice == 'east':
self.lever_rm()
elif choice == 'north' and not self.gate:
print "The gate is still closed and locked."
self.entrance()
elif choice == 'north' and self.gate:
self.fountain_rm()
else:
self.entrance()
# ...
def main():
crypt = Crypt()
crypt.welcome()
crypt.entrance()if __name__ == '__main__':
main()elif choice == 'north' and gate == False:
print "The gate is still closed and locked."
entrance()
elif choice == 'north' and gate == True:
fountain_rm()elif choice == 'north' and not gate:
print "The gate is still closed and locked."
entrance()
elif choice == 'north' and gate:
fountain_rm()else:
entrance(gate)Context
StackExchange Code Review Q#64932, answer score: 4
Revisions (0)
No revisions yet.