Pygame action when mouse 'click' on .rect?

后端 未结 2 905
春和景丽
春和景丽 2021-01-06 14:06

I have been writing a test function to learn how a mouse \'click\' action on a pygame.rect will result in a reponse.

So far:

def test():
    pygame.i         


        
2条回答
  •  别那么骄傲
    2021-01-06 14:23

    Well, if anyone is interested or is having a similar issue, this is what I needed to change.

    First off the, remove:

    button = button.get_rect()
    

    Then:

    screen.blit(button, (300, 200))
    

    Should be:

    b = screen.blit(button, (300, 200))
    

    This to create a Rect of the area of where the button is located on the screen.

    On to:

    if event.type == pygame.mouse.get_pressed()
    

    I changed to:

    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
    

    The pygame.mouse.get_pressed() gets the state of all three mouse buttons (MOUSEBUTTONDOWN, MOUSEBUTTONUP, or MOUSEMOTION). I also needed to add in event.button == 1 to specify that this was the 'left-mouse' button being pressed.

    Finally:

    `if button.collidepoint(pos):` 
    

    to:

    `if b.collidepoint(pos):`
    

    Using Rect b's collidepoint method

提交回复
热议问题