在Pygame中将鼠标悬停在图像上

发布于 2021-01-29 14:10:21

我有一张图片:

newGameButton = pygame.image.load("images/newGameButton.png").convert_alpha()

然后,我将其显示在屏幕上:

screen.blit(newGameButton, (0,0))

如何检测鼠标是否在触摸图像?

关注者
0
被浏览
112
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    使用Surface.get_rect获得Rect描述你的边界Surface,然后使用.collidepoint()来检查,如果鼠标光标这里面Rect


    例:

    if newGameButton.get_rect().collidepoint(pygame.mouse.get_pos()):
        print "mouse is over 'newGameButton'"
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看