Reputation: 3
I'm just starting out with Python 2 and programming in general and decided to make a text adventure for a bit of practice, but I'm completely stuck on title.
This is the code so far. I'm pretty much completely guessing the whole True/False stuff, but what I'm trying to work out is: when you enter the "viewing_room"
you come across a locked door, but if you go to the "lab_room"
and get the keycard from the body you can open it.
I am trying to make it so that the locked door is False, but when you collect the keycard, it changes to True and the door becomes unlocked. I appreciate any help, thanks!
prompt = "> "
decision = "What do you do?"
not_assigned = "Say wa?"
def engine_room():
print "You are in a dark room with the sound of moaning engines."
print "You see a corridor to your left and one to your right and an elevator straight ahead of you."
print decision
choice = raw_input(prompt)
if choice == "go left":
viewing_room()
elif choice == "go right":
right_corridor_dead_end()
elif choice == "use elevator":
print "you get in the elevator and go up."
main_hallway()
else:
print not_assigned
engine_room()
def right_corridor_dead_end():
print "You walk down the corridor only to be blocked by a collapsed ceiling."
print decision
choice = raw_input(prompt)
if choice == "go back":
engine_room()
else:
print not_assigned
def viewing_room():
print "You walk down the corridor and enter and a large room with a window covering the size of the wall."
print "Straight ahead is another door"
print decision
choice = raw_input(prompt)
if search_body() == False:
if choice == "open door":
print "The door is locked"
viewing_room()
elif choice == "go back":
engine_room()
else:
print not_assigned
if search_body() == True:
if choice == "open door":
print "The door opens you walk through"
storage_room()
else:
print not_assigned
viewing_room()
def main_hallway():
print "You enter a large brightly lit room with 3 rooms connected to it and another elevator straight ahead."
print "The rooms are named, the two on the left are the armoury and lab rooms and to the right are the cabins."
print decision
choice = raw_input(prompt)
if choice == "go to lab room":
lab_room()
elif choice == "go back":
engine_room()
def lab_room():
print "You enter the lab room which is cluttered with unexplainable machines."
print "To the back of the room you see the dead body of a man with no obvious cause"
print "He might have something useful on him"
print decision
choice = raw_input(prompt)
if choice == "search body":
search_body()
elif choice == "go back":
main_hallway()
def search_body():
print "You find a keycard that says 'storage' on it."
return True
lab_room()
engine_room()
Upvotes: 0
Views: 575
Reputation: 5405
You should take a look into classes. If you make games Object oriented programming is a better way to do it.
When you get used to classes, it gets easier to make games.
Here is an example of your game, as a class object
prompt = "> "
decision = "What do you do?"
not_assigned = "Say wa?"
class Game:
def __init__(self):
self.bodySearched = False
self.engine_room()
def engine_room(self):
print "You are in a dark room with the sound of moaning engines."
print "You see a corridor to your left and one to your right and an elevator straight ahead of you."
print decision
self.choice = raw_input(prompt)
if self.choice == "go left":
self.viewing_room()
elif self.choice == "go right":
self.right_corridor_dead_end()
elif self.choice == "use elevator":
print "you get in the elevator and go up."
self.main_hallway()
else:
print not_assigned
self.engine_room()
def right_corridor_dead_end(self):
print "You walk down the corridor only to be blocked by a collapsed ceiling."
print decision
self.choice = raw_input(prompt)
if choice == "go back":
self.engine_room()
else:
print not_assigned
def viewing_room(self):
print "You walk down the corridor and enter and a large room with a window covering the size of the wall."
print "Straight ahead is another door"
print decision
self.choice = raw_input(prompt)
if not self.bodySearched:
if self.choice == "open door":
print "The door is locked"
self.viewing_room()
elif self.choice == "go back":
self.engine_room()
else:
print not_assigned
if self.bodySearched:
if self.choice == "open door":
print "The door opens you walk through"
self.storage_room()
else:
print not_assigned
self.viewing_room()
def main_hallway(self):
print "You enter a large brightly lit room with 3 rooms connected to it and another elevator straight ahead."
print "The rooms are named, the two on the left are the armoury and lab rooms and to the right are the cabins."
print decision
self.choice = raw_input(prompt)
if choice == "go to lab room":
self.lab_room()
elif choice == "go back":
self.engine_room()
def lab_room(self):
print "You enter the lab room which is cluttered with unexplainable machines."
print "To the back of the room you see the dead body of a man with no obvious cause"
print "He might have something useful on him"
print decision
self.choice = raw_input(prompt)
if self.choice == "search body":
self.bodySearched = True
print "You find a keycard that says 'storage' on it."
self.lab_room()
elif self.choice == "go back":
self.main_hallway()
newGame = Game()
Now newGame is an object of the type Game, and will remember its states. The def init(self) is a method that will be called when ever you create a new object of that class. And you can create as many Game objects as you want, the name newGame or whatever you call the new object will replace self. So player1.searchedBody might be true while player2.searchedBody might be false. This is very handy.
Upvotes: 0
Reputation: 3663
Well, it seems to me this is not a Python question but rather a general programming question. You need some way to store your program's state - specifically, whether the body has been searched before or not. You should read a little bit about state machines, or look into passing stateful objects around between your different functions (maybe a "Player" or "Character" object which maintains some kind of state).
BTW when comparing to a boolean value in if, you can simply do:
if search_body():
pass
or:
if not search_body():
pass
Rather than comparing to True
or False
.
Upvotes: 1