簡體   English   中英

Python矩陣填充不應該填充的值

[英]Python matrix filling in values that shouldn't be filled in

我正在嘗試通過使用 pygame 制作類似胭脂的游戲來提高我的 Python 技能。 我開始嘗試制作一個簡單的關卡生成器,但我遇到了一個問題,即我的地圖矩陣中的值被填充的比應有的多。

以下代碼是我嘗試將所有相鄰元素添加到列表中,以便稍后隨機選擇一個添加到地圖中。

def generate(self):
    print(f'generating a new map (difficulty: {self.diff})')

    next_room = (random.randint(0, self.size-1), random.randint(0, self.size-1))
    print(next_room)

    room_options = []

    room_options.append(next_room)

    # left
    if next_room[0] != 0:
        room_options.append( (next_room[0]-1, next_room[1]) )
    # right
    if next_room[0] < self.size-1:
        room_options.append( (next_room[0]+1, next_room[1]) )
    # top
    if next_room[1] != 0:
        room_options.append( (next_room[0], next_room[1]-1) )
    # botttom
    if next_room[1] < self.size-1:
        room_options.append( (next_room[0], next_room[1]+1) )
    # top left
    if next_room[0] != 0 and next_room[1] != 0:
        room_options.append( (next_room[0]-1, next_room[1]-1) )
    # top right
    if next_room[0] != 0 and next_room[1] != self.size-1:
        room_options.append( (next_room[0]-1, next_room[1]+1) )
    # bottom left
    if next_room[0] != self.size-1 and next_room[1] != 0:
        room_options.append( (next_room[0]+1, next_room[1]-1) )
    # bottom right
    if next_room[0] != self.size-1 and next_room[1] != self.size-1:
        room_options.append( (next_room[0]+1, next_room[1]+1) )

    for coords in room_options:
        print(coords)
        x = coords[0]
        y = coords[1]
        self.map[x][y] = "O"

例如,當我打印出所有坐標時,我會得到有意義的值

(7, 2) (6, 2) (8, 2) (7, 1) (7, 3) (6, 1) (6, 3) (8, 1) (8, 3)

但是當我嘗試將這個位置的默認值從 '#' 更改為 'O' 以使用numpy.matrix(self.map)對其進行可視化時,我得到了如下所示的內容

[['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']]

更新:我一直在嘗試診斷這個問題,但仍然沒有弄清楚到底是哪里出了問題。 然而,我對正在發生的事情有一個更好的了解。 每次向某個位置寫入內容時,它都會覆蓋整個列。 我只是完全不知道為什么。 這里有更多的調試輸出來顯示這一點。

(6, 1)[0]
[['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']]
(5, 1)[1]
[['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']]
(7, 1)[2]
[['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']]
(6, 0)[3]
[['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']]
(6, 2)[4]
[['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']]
(5, 0)[5]
[['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']]
(5, 2)[6]
[['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']]
(7, 0)[7]
[['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']]
(7, 2)[8]
[['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']]

錯誤是Level類的構造函數,而不是它的generate()方法。

我最初用self.map = [["#"] * size] * size初始化了self.map ,這使得每一行都是它自己的副本。 我應該使用self.map = [["#"] * size for _ in range(size)]

感謝這個問題,我剛剛找到了解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM