2012-06-29 44 views
0

我有10個精靈,我主要精靈的對象,我寫了不同的圖像和起始位置等,但他們都表現相同的方式。他們是主精靈的副精靈。Pygame檢查碰撞與最前面最rect

我希望能夠按住鼠標點擊鼠標左鍵並將其移動到屏幕上,使其完美無缺。但問題是他們都有相同的控制點擊和拖動來移動它們。所以如果我點擊其中一個sprite rects,並將它拖拽到另一個sprite rects上,它也會選中它。我不希望這種情況發生。

有沒有辦法只檢查與最前面的矩形碰撞,或者如果有人可以解釋這樣做的方式,可以實現類似的結果。我查看了該文檔,但找不到解決方案。

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() 

回答

0

而不是使用pygame.mouse.get_pressed()函數,使用event queue並檢查一個pygame.MOUSEBUTTONDOWN事件。只有在第一次按下按鈕時它纔會被觸發一次。