我最近几个月一直在学习python,现在我正在尝试创建一个文本冒险游戏。程序/设置的要求如下:
根据用户的选择创建一个总共4个级别的文本冒险游戏。
根据您之前的选择,每个级别必须有三种不同的选择。
因此,例如,第1级有3个选项,每个都会将您带到2级。根据您在1级中所做的选择,您现在将有3个选择,导致1级有3个选择,2级有9个,3级有27,4级有81。
在我的代码中,我有一个用于创建提示和选择的基本设置,但我很难找到一种方法将每个特定提示与三个选项相关联。
到目前为止,这是我的代码:
# 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)
我试图尽可能彻底地解释这个问题,所以事情不要混淆,但我主要在我的print_segment函数中寻求帮助。评论描述了我面临的问题,并且我想知道如何存储所有提示和选择数据。是否最好创建一个有三个选项的提示字典?如果我这样做,我将如何将level1_choice1与level2_prompt1相关联?
如果有什么事情不清楚,请告诉我。
非常感谢!
答案 0 :(得分:1)
看起来处理这个的最好方法是两个有两个二维数组 - 一个用于提示,一个用于选择。第一个索引将指定级别,下一个索引将指定它的提示/选择。它看起来像这样:
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?",
"..."]]
然后,要访问说第一级的第二个提示,您只需访问prompt[0][1]
。然后,您应该能够轻松地跟踪索引,以选择要向用户显示哪些提示/选项。
答案 1 :(得分:1)
这是你之后的结构吗?
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