1

So I tried to find it but this is wrong I don't understand what event.pos means from the place I found this code. When I click on the image it doesn't print "clicked" and this is because it thinks that the image starts at x and y 0...

I never used Stack Overflow so hope I get an answer.

if event.type == pygame.MOUSEBUTTONDOWN:
    x,y=event.pos
    if playbutton.get_rect().collidepoint(x, y):
        print("clicked")
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Mr. Raptor
  • 17
  • 6

1 Answers1

0

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. A Surface is blit at a position on the screen. The position of the rectangle can be specified by a keyword argument. For example, the top leftof the rectangle can be specified with the keyword argument topleft. These keyword argument are applied to the attributes of the pygame.Rect before it is returned (see pygame.Rect for a full list of the keyword arguments).

if event.type == pygame.MOUSEBUTTONDOWN:

    x, y = event.pos
    button_rect = playbutton.get_rect(topleft = (button_x, button_y))

    if button_rect .collidepoint(x, y):
        print("clicked")
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Do I place the button's x and y on the button_x and button_y place? Also what topleft mean there...Sorry for too many questions. – Mr. Raptor May 08 '21 at 09:45
  • @Mr.Raptor (`button_x`, `button_y`) is the top left corner of the button. It is the location where you `blit` the button. – Rabbid76 May 08 '21 at 09:47