1

I try to find in a matrix of rect objects, the rect that collide with mouse position but doesn't work.

def search_immagine(tab,event_pos):
   for i in range(tab[0]):
       for j in range(tab[0]): #matrix square
           surface=tab[i][j]
           surface=surface.get_rect()
           if surface.collidepoint(event_pos):
                   return True

while not finished:
   for event in pygame.event.get():
       if event.type==pygame.QUIT:
           cfinished=True
       if event.type==pygame.MOUSEBUTTONDOWN:
           search_image(image,event.pos)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Pjay
  • 19
  • 2

1 Answers1

0

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, but it returns a rectangle that always starts at (0, 0) since a Surface object has no position.
The Surface is placed at a position when it is blit to the display.

You've to set the location of the rectangle, either by a keyword argument, e.g:

def search_immagine(tab,event_pos):
    for i in range(tab):
        for j in range(tab[0]): #matrix square

            x = ... # x coordinate of tab[i][j] = (i * widht)
            y = ... # y coordinate of tab[i][j] = (j * height)

            surface_rect = surface.get_rect(topleft = (x, y))
            if surface_rect.collidepoint(event_pos):
                return True
Rabbid76
  • 202,892
  • 27
  • 131
  • 174