嵌套 python 循环选择二维列表的完整列而不是给定索引
Nested python loop selects complete column of 2d list instead of given index
我正在编写一个小关卡编辑器,它可以生成一个文本瓦片地图以用于另一个程序。我是这样想的:
如果我将鼠标悬停在列表中的某个图块上并单击,则将二维列表的索引替换为当前选定的图块。相反,它会用选定的图块替换整个列。
我的代码片段负责检查是否发生这种情况:
import pygame
#Normal pygame window setup, class declaration and variable init
while True:
#Mouse position
m_x, m_y = pygame.mouse.get_pos()
events = pygame.event.get()
#Current tile the mouse is hovering over
self.tile_m_x = m_x // int(self.grid_params['size']) * int(self.grid_params['size'])
self.tile_m_y = m_y // int(self.grid_params['size']) * int(self.grid_params['size'])
#The part where it checks if the tile should be converted
for y, row in enumerate(self.tiles[:]):
for x, tile in enumerate(row):
if x == self.tile_m_x/int(self.grid_params['size']) and y == self.tile_m_y/int(self.grid_params['size']):
if m_btns[0]:
self.tiles[y][x] = self.current_selected_tile_type
我尝试在循环外、循环内、循环内、循环之上做所有事情,.....你明白我基本上已经尽了一切努力。
for y, row in enumerate(self.tiles[:]):
for x, tile in enumerate(row):
if x == self.tile_m_x/int(self.grid_params['size']) and y == self.tile_m_y/int(self.grid_params['size']):
if m_btns[0]:
self.tiles.loc[x,y] = self.current_selected_tile_type
尝试“=”而不是“==”,影响结果
我正在编写一个小关卡编辑器,它可以生成一个文本瓦片地图以用于另一个程序。我是这样想的:
如果我将鼠标悬停在列表中的某个图块上并单击,则将二维列表的索引替换为当前选定的图块。相反,它会用选定的图块替换整个列。
我的代码片段负责检查是否发生这种情况:
import pygame
#Normal pygame window setup, class declaration and variable init
while True:
#Mouse position
m_x, m_y = pygame.mouse.get_pos()
events = pygame.event.get()
#Current tile the mouse is hovering over
self.tile_m_x = m_x // int(self.grid_params['size']) * int(self.grid_params['size'])
self.tile_m_y = m_y // int(self.grid_params['size']) * int(self.grid_params['size'])
#The part where it checks if the tile should be converted
for y, row in enumerate(self.tiles[:]):
for x, tile in enumerate(row):
if x == self.tile_m_x/int(self.grid_params['size']) and y == self.tile_m_y/int(self.grid_params['size']):
if m_btns[0]:
self.tiles[y][x] = self.current_selected_tile_type
我尝试在循环外、循环内、循环内、循环之上做所有事情,.....你明白我基本上已经尽了一切努力。
for y, row in enumerate(self.tiles[:]):
for x, tile in enumerate(row):
if x == self.tile_m_x/int(self.grid_params['size']) and y == self.tile_m_y/int(self.grid_params['size']):
if m_btns[0]:
self.tiles.loc[x,y] = self.current_selected_tile_type
尝试“=”而不是“==”,影响结果