如何解决检查鼠标位置是否在区域 pygame 中
我尝试在 rect
对象的矩阵中找到与鼠标位置碰撞但不起作用的 rect
。
def search_immagine(tab,event_pos):
for i in range(tab[0]):
for j in range(tab[0]): #matrix square
surface=tab[i][j]
surface=surface.get_rect()
if surface.collidepoint(event_pos):
return True
while not finished:
for event in pygame.event.get():
if event.type==pygame.QUIT:
cfinished=True
if event.type==pygame.MOUSEBUTTONDOWN:
search_image(image,event.pos)
解决方法
pygame.Surface.get_rect.get_rect()
返回一个具有 Surface 对象大小的矩形,但它返回一个矩形,它总是从 (0,0) 开始,因为 Surface对象没有位置。
当 Surface blit
到显示器时,它被放置在一个位置。
您必须通过关键字参数设置矩形的位置,例如:
def search_immagine(tab,event_pos):
for i in range(tab):
for j in range(tab[0]): #matrix square
x = ... # x coordinate of tab[i][j] = (i * widht)
y = ... # y coordinate of tab[i][j] = (j * height)
surface_rect = surface.get_rect(topleft = (x,y))
if surface_rect.collidepoint(event_pos):
return True
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。