在Pygame中将鼠标悬停在图像上 [英] Detect mouseover an image in Pygame

查看:225
本文介绍了在Pygame中将鼠标悬停在图像上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张图片:

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?

推荐答案

使用 Surface.get_rect 以获得描述边界的 Rect 您的 Surface ,然后使用

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.

示例:

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

这篇关于在Pygame中将鼠标悬停在图像上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆