如何在Python列表中指定一般(x,y)位置?

时间:2018-12-11 12:11:29

标签: python arrays list indexing maze

我必须制作一个以迷宫为矩阵的迷宫游戏。我希望能够向上/向下/向右/向左移动播放器(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 btw)

这是我最初尝试的方法,但是没有用(这是向上移动的方法):

 #--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

1 个答案:

答案 0 :(得分:0)

要解决此问题,您确实需要将玩家位置的坐标保持在变量中。您是正确的,您的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])