Skip to main content
 首页 » 编程设计

python之测试在 Python 中点击一个矩形

2025年05月04日13zhengyun_ustc

我想不出一种方法来检测玩家是否在 Pygame 的矩形内单击。我试过了

    self.mouserect=(pygame.mouse.get_pos(), 8,8) 

后来

    if self.click: #(this is true if mouse button is down) 
        if self.mouserect.colliderect(self.a_thing_to_click_on.rect): 
            do_stuff 

但这给了我一个 AttributeError: 'tuple' object has no attribute 'colliderect'。我做错了什么?

请您参考如下方法:

您是否尝试使用 rect.collidepoint() ?

if self.click: #(this is true if mouse button is down) 
   if self.a_thing_to_click_on.rect.collidepoint(pygame.mouse.get_pos()):