Reputation: 21
Our job in class is to write a text based game that is interactive. I am basing mine off of the Stanley parable, which is a real video game. The problem is that when an if and else statement is bypass, I get an error. The whole point is not to have every question asked each time. It should only ask them in the order that the player chooses. This is my code. I was wondering if there is a way to get navigate through all the questions. P.S. I didn't finish some of the if and else statements so I just told them to print goose.
This is my code:
print ("Stanley lives in a world where he is always told what to do. He goes to work, he pushes the buttons that he is told to push over his intercom, and then he goes home and eats the food that he is given by the overseer. One day, stanley goes to work and discovers that nobody is there. He waits for commands to come over on his intercom but they fail to do so.")
begin = input ("does stanley leave his office?")
if begin == "yes":
q2 = input ("Stanley left his office and decided to go to the break room to see where everybody went, it was the first decision he ever made and it made him feel proud. Stanley got to the break room but nobody was there. He had noticed that in the break room, there were two doors that he had never seen before. Which door did he choose, right or left?")
else:
print ("So stanley stayed in his office where he never recieved another command, never recieved anymore food and died of starvation and depression.")
if q2 == "left":
q3 = input("Stanley decided to walk throught the left door where he found a red door and a blue door, but which color did he choose?")
else:
q4 = input ("Stanley decided to go through the right door which brought him into a room with a green door and an orange door. Which did he choose?")
if q3 == "blue":
q5 = input ("So after a little bit of puzzling over, Stanley decided to go through the blue door where found an elevator and a gun. Does he pick up the gun.")
else:
print ("So Stanley walked throught the red door where a gunsman opened the door and shot him, he fell to the ground and died.")
if q5 == "yes":
q9 = input ("Stanley wondered what this gun was doing here and he knew that things were getting pretty strange. He though to himself that in the case that his work mates had become zombies and were hiding amongst this maze of doors, he could protect himself. Now that he has a gun, does he decide to use the elevator?")
if q4 == "orange":
print ("Stanley decided to go through the orange door. As he was walking through the hallway beyond the orange door, the floor beneath him collapsed which led to his demise.")
else:
q6 = input ("Stanley decided to go through the green door where he was confronted with a narrow hallway. He continued to walk through the hallway until he came to a split in which there were two branches of this hallway for him to choose. Did he choose the left or the right?")
if q9 == "yes":
q7 = input ("Stanley decided to use the elevator. He got into the elevator and there were two buttons. Up or Down?")
else:
print("Goose")
if q7 == "down":
q8 = input ("Stanley decided to do down the elevator. He waited until he had reached the bottom and the doors had opened up. The elevator doors had opened to a huge room full of chairs. It was almost as if a meeting were to be held there but nobody showed up. All of the sudden stanley hears some noise. He sees one of his workmates run out from underneath one of the chairs and start running toward him. His workmates name if Philip. Philip is covered in blood and has a missing arm. Does Stanley shoot philip? ")
else:
print ("Stanley just killed someone for the first time and he is going into shock. He curls up on the ground where a creature named Zinyak greets him and tosses him around like a ragdoll until he is dead.")
if q8 == "no":
q10 = input("Philip runs to you, he starts telling you about how there had been a huge creature unlike any creature he had ever seen. He said his name was Zinyak and that he claimed to rule the zen empire. Philip told Zinyak that earth was not part of the zen empire and that he needed to leave him alone. Zinyak tore his arm off and threw him across the room which apparently isn't too far from where stanley is right now. Does Stanley choose to confront Zinyak?")
else:
print("Goose")
if q10 == "yes":
q11 = input ("So stanley takes off his shirt and ties it tightly over philips arm. He then goes through the door where he sees Zinyak. Zinyak begins to charge at him. Does Stanley shoot zinyak?")
else:
print("Goose")
if q5 == "no":
print ("Stanley reached for his gun only to find that he had never picked it up and now he must run. Zinyak being about 10 feet tall, easily catches up to Stanley and rips him limb from limb.")
else:
print("Stanley decides to shoot Zinyak. He empties his entire clip on Zinyak but it doesn't even seem to bother him, its as if Zinyaks Skin is made of Armor. Zinyak than grabs Stanley tries to run away but it seems as if he cannot move. He is than awoken by his wife telling he had a nightmare.")
Upvotes: 2
Views: 60
Reputation: 77837
You didn't explain what you want, or what error you get. However, I've written and/or debugged enough that I believe I understand your paradigm.
You need to add a state variable. For now, keep it simple; let it be a number or simple label that indicates the room the player is in. Then your central code looks something like this:
alive = True
while alive:
# Print the text you get on entering the room
print room_script[state]
# Ask for an action decision
reply = input (room_question[state])
# Move to the next room based on answer
if reply.lower[0] = 'y':
state = move_on_yes[state]
else:
state = move_on_no[state]
# Did the decision end the game?
alive = room_is_safe[state]
Now, you populate the lists with your game script and transitions
For instance, a very simple game might look like this:
room_script = [
"You are in the first room.",
"Good choice! You win!",
"Did you really think that was going to work?"
]
room_question = [
"There is a lethal-looking potion here. Do you drink it?",
"Do you want to start over?",
"Do you want to start over?"
]
move_on_yes = [2, 0, -1]
move_on_no = [1, -1, -1]
room_is_safe = [True, True, False]
From here, you really need to work on reading and playing similar simple games from other people. It's an amusing genre, but there are a lot of considerations for a full-fledged game.
Upvotes: 0
Reputation: 199
You may assign all q?? variables before input.
q1=''
q2=''
...
for example, if q9 answer 'no', and q7 didn't initialize, and if q7 == down; will throw error, initialized variables can prevent this.
if q9 == "yes":
q7 = input ("Stanley decided to use the elevator. He got into the elevator and there were two buttons. Up or Down?")
else:
print("Goose")
if q7 == "down":
q8 = input ("Stanley decided to do down the elevator. He waited until he had reached the bottom and the doors had opened up. The elevator doors had opened to a huge room full of chairs. It was almost as if a meeting were to be held there but nobody showed up. All of the sudden stanley hears some noise. He sees one of his workmates run out from underneath one of the chairs and start running toward him. His workmates name if Philip. Philip is covered in blood and has a missing arm. Does Stanley shoot philip? ")
Upvotes: 0
Reputation: 361585
You'll want to nest the if
checks inside of each other. After you ask a question, put the relevant if
/else
block indented 4 spaces.
For instance, put the if q2
check right after the q2 = ...
question.
begin = input ("does stanley leave his office?")
if begin == "yes":
q2 = input ("Stanley left his office and decided to go to the break room to see where everybody went, it was the first decision he ever made and it made him feel proud. Stanley got to the break room but nobody was there. He had noticed that in the break room, there were two doors that he had never seen before. Which door did he choose, right or left?")
if q2 == "left":
q3 = input("Stanley decided to walk throught the left door where he found a red door and a blue door, but which color did he choose?")
else:
q4 = input ("Stanley decided to go through the right door which brought him into a room with a green door and an orange door. Which did he choose?")
else:
print ("So stanley stayed in his office where he never recieved another command, never recieved anymore food and died of starvation and depression.")
This will continue for each question. Put if q3
after q3 = ...
, and so on. You're going to have to keep adding levels of indentation. That's expected.
Upvotes: 1