【问题标题】:Print part of a 2D list打印二维列表的一部分
【发布时间】:2019-03-14 12:41:16
【问题描述】:

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

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)

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

【问题讨论】:

  • 你能分享你的游戏吗?想玩:)
  • 我在这里添加了代码:link。我用的是 Jupyter Notebook,所以你可以把代码复制到新的笔记本上玩。

标签: python list maze


【解决方案1】:

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

这将获取您的二维数组level,创建一个 NumPy 数组np_level,然后从[y-2, y+3)[x-2, x+3) 范围内打印它的一部分(使用区间表示法)。这意味着它将数组从y-2 切片到y+2 和从x-2x+2 inclusive

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

注意这需要安装 NumPy 模块。如果尚未安装,请在命令行/终端中输入python -m pip install numpy 进行安装。

【讨论】:

  • 谢谢 - 这很有帮助。但是,当我尝试实现它时,它只给了我[ ] 作为输出...我用你上面写的替换了我的代码中的print_level(level)
  • 我还稍微编辑了代码,因为我意识到level 是我的特定关卡,而在主游戏循环中我们需要maze。当我调用该函数时,我输入了level
  • @MOA "但是,当我尝试实现它时,它只给了我 [ ] 作为输出" -- 嗯...也许是因为该位置位于起始位置(1, 0)?我刚刚添加了一段关于边界检查的内容。如果您还没有,请尝试实施。
  • 我认为 numpy 数组的开销比 Python 中的嵌套列表要少,因为它们在脚本和内存之间的中间层更少
  • 哦,我找到了,它是 print_level 而不是 print
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多