【发布时间】:2021-06-03 18:39:33
【问题描述】:
遇到了老生常谈的无限循环问题……
编写文字冒险游戏并降低游戏机制;地图和围绕它的运动。当我尝试实现其他功能时遇到无限循环问题 - 获取项目等。
下面是代码的 2 个 sn-ps,包括初始化变量、运动代码和下一阶段的内容。 “......”下面是问题所在。
所有缩进都对齐。
print("Welcome to this most intrepid" + game['name']
+ " your goal; should you dare to continue... is to" + game['goal'])
star_loc = int(game['start'])
curr_loc = star_loc
character = {'inventory': [], 'location':curr_loc}
x_loc = 1
y_loc = 2
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
while True:
command_parts = command.split()
cmd = command_parts[0]
print(cmd)
obj = command_parts[-1]
print(obj)
#move around map
if cmd == 'move' or cmd == 'go' or 'hiddenpath':
#handles movement on the X plane - East, West
elif obj == 'east':
if x_loc == int(game['xsize']): # use the x size the game dict instead, compute the x and y
curr_loc = curr_loc - 2
x_loc = 1
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
else:
curr_loc = curr_loc + 1
x_loc = x_loc + 1
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
elif obj == 'west':
if x_loc == int(game['xsize']) - 2:
curr_loc = curr_loc + 2
x_loc = x_loc = 3
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
else:
curr_loc = curr_loc - 1
x_loc = x_loc - 1
y_loc = y_loc
command = input("You are " + map[curr_loc]["desc"] + ' what next? ')
.................
if cmd == 'exit':
print('Goodbye')
break
if cmd == 'inv':
print('You are carrying')
for item in character['inventory']:
print('', item)
continue
if cmd == 'goal':
print(game['goal'])
else:
continue
if cmd == 'take':
if obj in map[curr_loc]['obj']:
print("You take the " + str(map[curr_loc]['obj'][0]))
map[curr_loc]['obj'].remove(obj)
character['inventory'].append(obj)
continue
主要拖累... └[∵┌]└[ ∵ ]┘[┐∵]┘
【问题讨论】:
标签: python if-statement while-loop infinite-loop do-while