Skip to content
Home » pygame detect click

pygame detect click

Let’s start the discussion about pygame detect click in following Approaches.

When you click the mouse button, the MOUSEBUTTONDOWN event occurs once, and the MOUSEBUTTONUP event occurs once when you release the mouse button. Two elements of the pygame.event.Event() object offer information about the mouse event. pos is a tuple that stores the clicked position. The button that was clicked is saved. A value is assigned to each mouse button. For example, the values of the characteristics for the left mouse button, middle mouse button, right mouse button, mouse wheel up and mouse wheel down are 1, 2, 3, 4, 5. Multiple mouse button events occur when multiple keys are pushed. Further information can be found in the pygame.event module documentation.

pygame detect click ?

while ... # your main loop
  # get all events
  ev = pygame.event.get()

  # proceed events
  for event in ev:

    # handle MOUSEBUTTONUP
    if event.type == pygame.MOUSEBUTTONUP:
      pos = pygame.mouse.get_pos()

      # get a list of all sprites that are under the mouse cursor
      clicked_sprites = [s for s in sprites if s.rect.collidepoint(pos)]
      # do something with the clicked sprites...

I hope the strategies listed above work for you. Happy coding and come back again.

Similar Post : How to remove button decoration in CSS