Чтобы решить эту проблему, вам действительно нужно хранить координаты местоположения игрока в переменной.Вы правы, что ваши петли 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])