我在试着检测一个精灵什么时候被鼠标悬停过。不管它是不是工作,它从来没有检测到鼠标在上面。你知道吗
瓷砖代码:
#!/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()
事件代码
for event in pygame.event.get():
for tile in tiles:
if tile.image.get_rect().collidepoint(pygame.mouse.get_pos()):
print 'tile hovered'
您正在检查您的鼠标是否位于图像矩形上。由于
image.get_rect()
返回的rect总是以(0,0,width,height)
的形式出现,因此只有当磁贴的位置在(0,0)时,您的检查才起作用。你知道吗要解决这个问题,您可以将位置保留在rect中,或者创建一个新的rect来描述实际位置。你知道吗
您还可以筛选事件,并且只检查
MOUSEMOTION
事件类型。你知道吗相关问题 更多 >
编程相关推荐