Home > Software design >  Nested python loop selects complete column of 2d list instead of given index
Nested python loop selects complete column of 2d list instead of given index

Time:04-04

I am writing a little level editor which generates a text tilemap to use in another program. I thought like this:

If I hover my mouse over a tile in a list and click, replace that index of the 2d list with the currently selected tile. Instead it replaces the entire column with the selected tile.

My code snippet responsible for checking if this happens:

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

I tried doing everything outside the loop, with the loop, inside the loop, above the loop, ..... You get the point i basically tried everything i could.

My code: https://github.com/clashes4d-gamedev/pb_test_editor

CodePudding user response:

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

try "=" not"==", to affect results

  • Related