I have an image:
newGameButton = pygame.image.load("images/newGameButton.png").convert_alpha()
I then display it on the screen:
screen.blit(newGameButton, (0,0))
How do I detect if the mouse is touching the image?
I have an image:
newGameButton = pygame.image.load("images/newGameButton.png").convert_alpha()
I then display it on the screen:
screen.blit(newGameButton, (0,0))
How do I detect if the mouse is touching the image?
Use
Surface.get_rect
to get aRect
describing the bounds of yourSurface
, then use.collidepoint()
to check if the mouse cursor is inside thisRect
.Example:
I'm sure there are more pythonic ways to do this, but here is a simple example:
Read more on the mouse in pygame, as well as surfaces in pygame.
Also here is an example closely related to this.