I am trying to make a simple game with pygame, and I am running into the following general issue.
Suppose we have two sprites, call them sprite1
and sprite2
, each with their corresponding rect
and image
attributes. These sprites are "clickable", in the sense that when the user clicks on them something should happen. I usually implement this by adding the following into the game loop:
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
if spritex.collidepoint(mouse_pos):
(whatever clicking on spritex triggers)
In my game sprite1
and sprite2
can occasionally overlap, and let's suppose that I always want to draw sprite2
on top of sprite1
. I understand that I can achieve this by having screen.blit(sprite1.image, sprite1.rect)
before screen.blit(sprite2.image, sprite2.rect)
. However, I would like that whenever sprite1
and sprite2
are overlapping, and the user clicks on a point that is both above sprite1.rect
and sprite2.rect
only the action of sprite2
should trigger.
In this simple example, I understand that one could do this with
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
if sprite2.collidepoint(mouse_pos):
(whatever clicking on spritex triggers)
elif sprite1.collidepoint(mouse_pos):
(whatever clicking on sprite1 triggers)
but this solution does not generalize easily to more complicated situations (more sprites, more types of sprites, pygame_gui elements, etc.).
I wonder: is there a more simple and elegant solution to get around problems like these?