我正在用一些代码创建一个游戏,我从这里得到了另一个问题,在用剑杀死熊之后,我需要帮助创建另一个章节。如何以及在何处插入 goto命令以开始处理另一章。如果您有任何建议或意见,也会有所帮助。 这是代码。随意调整它并告诉我如何。
p = 30
print 'You enter a dark room with two doors. Do you want to enter door #1 or door #2?'
door = raw_input('> ')
if door == "1":
print 'Theres a giant bear eating what appears to be a human arm, though its so damaged it\'s hard to be sure'
print 'what do you want to do?'
print '#1 try to take the arm'
print '#2 scream at the bear'
print '#3 swing sword of death upon him'
bear = raw_input('> ')
if bear == "1":
print 'You approach the bear slowly, never breaking eye contact. After what feel like a thousand years you are finally face to face with the bear. \nYou grab the arm and pull. of course the bear responds by eating you face. well done!'
elif bear == "2":
print 'You scream at the bear, a decent sound as well. The bear however was not impressed it would seem \nas he insantly eats your face. Well done!'
elif bear == "3":
print ' You swing the sword with all your might chopping the bears head completely off while blood drips to the floor'
else:
print 'Well, doing %s is probably better. Bear runs away.' % bear
elif door == "2":
print 'You stare into the endless abyss of Bollofumps retina.'
print 'Oh dear, seems the insanity of Bollofumps existence had driven you quite insane.'
print '#1. drool'
print '#2. scream and drool'
print '#3. Understand the fabrics of molecula toy crafting'
insanity = raw_input('> ')
if insanity == "1" or "2":
print 'Your body survives, powered by pure insanity!'
else:
print 'your mind melts into green flavoured jello! mmm!'
else:
print 'you think for a while but suddenly a troll weilding a terrible looking knife comes through a trap door and shanks you!'
答案 0 :(得分:3)
我认为如果你继续这条路线,你就不需要在代码变得无法管理之前添加更多的房间/场景。它还将包含许多几乎重复的代码,用于显示编号选项,阅读响应以及显示结果(主要是看起来很不愉快的死亡......)。
如果您真的想继续自己动手,可以考虑一些结构化数据来描述每个响应带您到的位置,问题和下一个位置。那可能是一个包含各种元素的字典。例如描述,选择(允许每个场景选择不同数量的选项数组),next_location(一个与选择大小相同的数组,根据选择提供冒险者到达的下一个场景的位置)。然后,您需要一个字典来保存这些字典,其中键是位置的名称。试着给你一个我的意思的例子,这里是“暗室”和“无尽的深渊”的(略微减少)版本
game_dict ={}
sample_location_1 = { 'description' : 'You enter a dark room with two doors. Do you want to',
'choices' : ['enter door #1','enter door #2','dark_room_invalid'],
'next_location' :['bear_room', 'endless_abyss', 'dark_room'] }
game_dict['dark_room'] = sample_location_1
sample_location_2 = { 'description' : 'You stare into the endless abyss of Bollofumps retina.\nOh dear, seems the insanity of Bollofumps existence had driven you quite insane. What do you want to do ?',
'choices' : ['drool','scream and drool', 'Understand the fabrics of molecula toy crafting', 'endless_abyss_invalid'],
'next_location' :['death', 'death', 'death_by_troll', 'death_by_witty_reposte'] }
game_dict['endless_abyss'] = sample_location_2
然后,您需要在单个位置读取和处理数据的代码,并在采取措施后显示下一个位置:
current_location = 'dark_room'
while current_location != 'death':
print(game_dict[current_location]['description'])
for num, option in enumerate(game_dict[current_location]['choices'][:-1]):
print option
action = int(raw_input('> ')) -1
if action < len(game_dict[current_location]['choices']) -1:
current_location = game_dict[current_location]['next_location'][action]
else:
current_location = game_dict[current_location]['next_location'][-1]
print "you are now dead....... consider this."
这是一个过于简化的代码示例,您可能希望在获取用户响应时进行错误处理。可能是因为意外地指定了你还没有定义的'next_location'的错误而且你需要一本你可能想要的所有位置的怪字词典。
使用ifs和gotos编写游戏将会非常快速地失控。想想结构化数据和单个例程来处理这些数据,然后到达下一个位置,然后由同一个例程处理....
这样做,您可以研究编写例程,以帮助您以交互方式创建数据(即提示您进行描述,选择和结果)。然后可以将该daqta写入磁盘并可能从文件读入,允许您基于一个可重复使用的“引擎”进行许多冒险。
我希望它有一些用处。