首页 文章

Pygame检查与最顶层前景rect的碰撞

提问于
浏览
0

我有10个精灵,我用不同的图像和起始位置写的主要精灵的对象 . 但它们都表现相同的方式 . 它们是主精灵的子精灵 .

我希望能够在一个矩形上按住鼠标并将其移动到屏幕上,这样可以很好地工作 . 但问题是它们都有相同的控件点击并拖动来移动它们 . 因此,如果我点击其中一个sprite rects并将其拖到另一个sprite rects上,它也会选中它 . 而且我不希望这种情况发生 .

有没有办法只检查与最顶层前景rect的碰撞,或者有人可以解释这样做的方法,以达到类似的结果 . 我看过rect文档但我找不到解决方案 .

def update(self,):
    self.move(self.rect)

def move(self,rect):

    if pygame.mouse.get_pressed() == (1, 0, 0) and the_rect.collidepoint(pygame.mouse.get_pos()):
        self.state = 1

    elif pygame.mouse.get_pressed() == (0, 0, 0) and the_rect.collidepoint(pygame.mouse.get_pos()):
        self.state = 0

    if self.state == 0:
        the_rect.centerx = the_rect.centerx
        the_rect.centery =  the_rect.centery
    elif self.state == 1:
        (the_rect.centerx, the_rect.centery) = pygame.mouse.get_pos()

1 回答

  • 0

    而不是使用pygame.mouse.get_pressed()函数,使用event queue并检查pygame.MOUSEBUTTONDOWN事件 . 只有在第一次按下按钮时才会触发一次 .

相关问题