Reputation: 51
I have been learning python for the last few months and right now I am trying to create a text adventure game. The requirements for the program/setup are as follows:
Create a text adventure game with a total of 4 levels based on choices made by the user.
Each level must have three different choices based on choices you have made before.
So for example, level 1 has 3 choices, with each taking you to level 2. Based on the choice you made in level 1, you will now have 3 more choices resulting in level 1 having 3 choices, level 2 having 9, level 3 having 27, and level 4 having 81.
In my code I have a basic setup for creating the prompts and choices, but I'm struggling to find a way to associate each specific prompt with three choices.
Here is my code so far:
# every prompt has an associated 3 choices with it, and every
choice made has a prompt with three more choices to be made
associated with it.
def print_segment(prompt, choice1, choice2, choice3):
print(prompt)
print("[A] " + choice1)
print("[B] " + choice2)
print("[C] " + choice3)
answer = input().lower()
if answer == "A": # if answer is A, print the next prompt associated with A
if answer == "B": # if answer is B, print the next prompt associated with B
if answer == "C": # if answer is C, print the next prompt associated with C
# level 1 choices
level1_choice1 = "Go to the scene of the most recent murder."
level1_choice2 = "Go to the scene of the first murder."
level1_choice3 = "Wait a few hours and see how the situation develops."
# level 1 prompts
level1_prompt1 = '''You are a murder investigator and you are tasked on the case of a mysterious string of killings
that have happened in the past few weeks. How do you start?'''
# level 2 prompts
level2_prompt1 = "You arrive at the scene of the most recent murder. What would you like to do first?"
level2_prompt2 = "You arrive at the scene of the first murder. What would you like to do first?"
level3_prompt3 = "You receive a letter from an unknown source saying that you should meet them at a specific location. What do you do?"
print_segment(level1_prompt1, level1_choice1, level1_choice2, level1_choice3)
I'm trying to be as thorough as possible with this explanation so things don't get confused, but I'm looking for help mostly in my print_segment function. The comments describe the issue I'm facing and I'm wondering how to store all of the prompt and choice data. Would it be best to create a dictionary of prompts with three choices? If I do that, how would I go about associating level1_choice1 with level2_prompt1?
Let me know if something isn't clear.
Thanks a lot!
Upvotes: 1
Views: 87
Reputation: 5394
Is this the structure you're after?
choices = {
1 : { 'prompt' : {
'prompt' : 'Level 1 prompt',
'A' : 'Choice A',
'B' : 'Choice B',
'C' : 'Choice C' },
},
2 : { 'promptA' : {
'prompt' : 'Level 2 prompt A',
'A' : 'A Choice A',
'B' : 'A Choice B',
'C' : 'A Choice C' },
'promptB' : {
'prompt' : 'Level 2 prompt B',
'A' : 'B Choice A',
'B' : 'B Choice B',
'C' : 'B Choice C' },
'promptC' : {
'prompt' : 'Level 2 prompt C',
'A' : 'C Choice A',
'B' : 'C Choice B',
'C' : 'C Choice C' },
},
3 : { 'promptA' : {
'prompt' : 'Level 3 prompt A',
'A' : 'A Choice A',
'B' : 'A Choice B',
'C' : 'A Choice C' },
'promptB' : {
'prompt' : 'Level 3 prompt B',
'A' : 'B Choice A',
'B' : 'B Choice B',
'C' : 'B Choice C' },
'promptC' : {
'prompt' : 'Level 3 prompt C',
'A' : 'C Choice A',
'B' : 'C Choice B',
'C' : 'C Choice C' },
}
}
def print_segment(level, prev_choice = ''):
d = choices.get(level).get('prompt' + prev_choice)
print(d.get('prompt'))
for c in 'ABC':
print("[{}] {}".format(c, d.get(c)))
# Output
>>> print_segment(1)
Level 1 prompt
[A] Choice A
[B] Choice B
[C] Choice C
>>> print_segment(2, 'A')
Level 2 prompt A
[A] A Choice A
[B] A Choice B
[C] A Choice C
>>> print_segment(3, 'B')
Level 3 prompt B
[A] B Choice A
[B] B Choice B
[C] B Choice C
Upvotes: 1
Reputation: 1867
It looks like the best way to handle this would be two have two two-dimensional arrays -- one for the prompts and one for the choices. The first index would specify the level, and the next would specify which prompt/choice it is. It would look something like this:
prompts = [["Go to the scene of the most recent murder.",
"Go to the scene of the first murder.",
"Wait a few hours and see how the situation develops."],
["You arrive at the scene of the most recent murder. What would you like to do first?",
"..."]]
Then, to access say the second prompt for the first level, you would just access prompt[0][1]
. You should then easily just be able to keep track of the indices to pick which prompts/choices you want to show the user.
Upvotes: 1