Reputation: 5
I am trying to detect when a Sprite has been hovered over by the mouse. However it is not working, it is never detecting the mouse over.
Code for Tile:
#!/usr/bin/python
import pygame
class Tile(pygame.sprite.Sprite):
def __init__(self, img_sprite, init_position):
pygame.sprite.Sprite.__init__(self)
self.position=init_position
self.image=pygame.image.load(img_sprite)
self.rect = self.image.get_rect()
Code for event
for event in pygame.event.get():
for tile in tiles:
if tile.image.get_rect().collidepoint(pygame.mouse.get_pos()):
print 'tile hovered'
Upvotes: 0
Views: 117
Reputation: 11170
You are checking if your mouse is over the rect that is the image rect. Since the rect returned by image.get_rect()
is always in the form (0,0,width,height)
, then your check only works if the position of your tile is at (0,0).
To fix this your can keep the position in the rect, or create a new rect that will describe the actual position.
You can also filter the events, and only check for a MOUSEMOTION
event type.
Upvotes: 1