python - 如何在 Python 列表中指定一般 (x,y) 位置?
问题描述
我必须制作一个以矩阵为迷宫的迷宫游戏。我希望能够向上/向下/向右/向左移动播放器 (X)。为此,我必须定义数组的坐标 x,y 以便相应地移动它们。如何指定数组中的一般位置?
这是我的迷宫(“1”代表一堵墙,“X”代表玩家):
level = [
["1","X","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],
["1"," "," ","1","1","1","1","1","1","1"," "," "," "," "," "," "," "," "," "," ","1","1","1","1","1"],
["1"," "," ","1","1","1","1","1","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," "," "," "," "," "," ","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," "," "," "," "," "," ","1","1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," "," "," ","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1","1","1"," "," "," "," "," "," ","1"],
["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"," ","1"]
]
start_maze = level[0][1]
end_maze = level[9][23]
print(start_maze)
print(end_maze)
for bla in level:
print(' '.join(str(n) for n in bla))
所以迷宫的输出是: Maze
我想要做的是能够说玩家处于“bla”位置。如果玩家选择向上移动,这意味着位置现在在 x 方向变为-1,在 y 方向保持不变......我希望我足够清楚......我是 Python 新手。(使用 Python 3 顺便说一句)
这是我最初尝试过的,但没有奏效(这是为了向上移动):
#--MOVE = UP--
if move == "UP":
print(move)
for y in range(0,len(level)):
for x in range(0,len(level[y])):
if level[y][x] == " ":
level[y][x] = level[y-1][x]
level[y][x] = "X"
print(level)
else:
print('Oups - there is a wall there.')
try_again = input('Try Again? Y for Yes, N for No: ')
try_again = try_again.upper()
if try_again == 'Y':
continue
else:
start = False #to exit the loop and quit the program
解决方案
要解决这个问题,您确实需要将玩家位置的坐标保存在一个变量中。你是正确的,你的for
循环是不必要的。这是完成这项工作的一种方法,希望评论能帮助您理解。它可能会变得更流畅,但应该涵盖您需要的大部分功能。
level = [
["1"," ","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"],
["1"," "," ","1","1","1","1","1","1","1"," "," "," "," "," "," "," "," "," "," ","1","1","1","1","1"],
["1"," "," ","1","1","1","1","1","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," "," "," "," "," "," ","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," "," "," "," "," "," ","1","1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," ","1","1","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," ","1","1","1","1"," "," ","1","1"," "," "," "," ","1","1","1","1"," "," ","1","1","1","1","1"],
["1"," "," ","1","1","1"," "," "," "," "," "," "," "," ","1","1","1","1"," "," "," "," "," "," ","1"],
["1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1"," ","1"]
]
def print_level(level):
""" Print level row-wise so that it retains its 2D shape """
for row in level:
print(row)
# Object to store the player coords
player = {'y': 0, 'x': 1}
level[player['y']][player['x']] = 'X'
print_level(level)
# Translate keywords into coordinate changes
move_modifications = {'UP': {'y': -1, 'x': 0},
'DOWN': {'y': 1, 'x': 0},
'LEFT': {'y':0, 'x': -1},
'RIGHT': {'y': 0, 'x': 1}}
# Main game loop
while True:
move = input("Which direction?")
# Give them the option to quit
if move.lower() == 'exit':
break
if not move_modifications.get(move):
print("Invalid input")
continue
coords = move_modifications[move]
new_y = player['y'] + coords['y']
new_x = player['x'] + coords['x']
# Catch them if they try to leave the map
try:
maze_position = level[new_y][new_x]
except IndexError:
print("Not on map")
continue
if maze_position != '1':
# Move on the map
level[player['y']][player['x']] = ' '
level[new_y][new_x] = 'X'
# Update player coords
player['y'] = new_y
player['x'] = new_x
# Print result
print_level(level)
else:
print("Oops, there's a wall")
如果您只想打印播放器周围的区域,那么您可以使用类似下面的函数来使用列表切片。这只是一个示例方法,您可以将其用作起始基础。
def print_window(level, player_y, player_x, window_size=2):
""" Print only the immediate surroundings of the player """
min_y = max(0, player_y - window_size)
max_y = min(len(level), player_y + window_size)
min_x = max(0, player_x - window_size)
max_x = min(len(level[0]), player_x + window_size)
for row in level[min_y:max_y]:
print(row[min_x:max_x])