Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect mouseover an image in Pygame

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?

like image 787
mattjegan Avatar asked Aug 07 '12 12:08

mattjegan


1 Answers

Use Surface.get_rect to get a Rect describing the bounds of your Surface, then use .collidepoint() to check if the mouse cursor is inside this Rect.


Example:

if newGameButton.get_rect().collidepoint(pygame.mouse.get_pos()):
    print "mouse is over 'newGameButton'"
like image 127
sloth Avatar answered Sep 30 '22 11:09

sloth