Python鼠标悬停图像不起作用

2024-04-20 01:12:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着检测一个精灵什么时候被鼠标悬停过。不管它是不是工作,它从来没有检测到鼠标在上面。你知道吗

瓷砖代码:

#!/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'

Tags: 代码inrectimageselfeventimgfor
1条回答
网友
1楼 · 发布于 2024-04-20 01:12:23

您正在检查您的鼠标是否位于图像矩形上。由于image.get_rect()返回的rect总是以(0,0,width,height)的形式出现,因此只有当磁贴的位置在(0,0)时,您的检查才起作用。你知道吗

要解决这个问题,您可以将位置保留在rect中,或者创建一个新的rect来描述实际位置。你知道吗

您还可以筛选事件,并且只检查MOUSEMOTION事件类型。你知道吗

相关问题 更多 >