繁体   English   中英

打印2D列表的一部分

[英]Print part of a 2D list

我必须创建一个迷宫游戏,它接收来自用户的命令以便玩游戏。 我已经为迷宫游戏编写了代码。 我想要修改的是仅在将迷宫打印给用户时显示部分迷宫(移动完成后)。 这是我的迷宫:

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"]
]

start_maze = level[0][1]      #start of maze
end_maze = level[9][23]       #end of maze

输出如下:

The initial configuration of the maze is:
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

如何制作它以便玩家只能在移动时看到迷宫的一部分,而不是能够看到结束(这使得它很容易)?

所以例如要有这样的视图:

    1   1 1 1
    1     1 1 
    1   X 1 1 
    1          
    1               

也就是说他只能在每个方向看到2个其他细​​胞。

我希望我明白这个问题。 如果需要,下面是主要游戏的代码部分:

player = {'y': 0, 'x': 1}
level[player['y']][player['x']] = 'X'

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

def player_move(maze):

    # Main game loop
    play = True
    while play:
        move = input("Please enter a command (LEFT/RIGHT/UP/DOWN): ")
        move = move.upper()

        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 = maze[new_y][new_x]
        except IndexError:
            print("Not on map")
            continue

        if maze_position != '1':
            # Move on the map
            maze[player['y']][player['x']] = ' '
            maze[new_y][new_x] = 'X'

            # Update player coords
            player['y'] = new_y
            player['x'] = new_x

            # Print result
            print_level(maze)

其余的代码只是移动,它不包括任何迷宫的打印。

NumPy数组可以方便地切割二维列表。 考虑下面的代码和切片。

# load numpy from its module
import numpy as np

# create a numpy array
np_level = np.array(level)

# slice the map
slice = np_level[max(new_y-2,0) : new_y+3, \    # y slice
                 max(new_x-2,0) : new_x+3]      # x slice

# print the slice
print(slice)
# or print_level(slice), whichever one suits your case

这将获取您的2D数组level ,生成NumPy数组np_level ,然后从[y-2, y+3)[x-2, x+3)范围打印一个切片(使用区间符号)。 这意味着它将数组从y-2切换到y+2并从x-2切换到x+2 包括两者)。

y-2低于0(即为负)的情况下max(y-2, 0)存在max(y-2, 0) )。 使用负开头 -index和正结束索引进行切片将返回一个空列表(例如some_array[-1:1] ==> [] )。 max(x-2, 0) 我们还可以为最终指示添加min(y+3, <height-of-level>)min(x+3, <width-of-level>) ,但是数组切片已经处理了这些边缘情况 - 所以这一切都很好。

注意这需要安装NumPy模块。 如果还没有,请通过在命令行/终端输入python -m pip install numpy安装它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM