Pygame action when mouse 'click' on .rect?

假装没事ソ 提交于 2019-11-30 23:13:12

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

I think rect method call collidepoint, not collide*r*point. Here is the link to documentation!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!