簡體   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