2011-12-09 94 views
2

我無法找出一種方法來檢測玩家是否在Pygame的矩形內點擊過。我試圖在Python中點擊矩形的測試

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: '元組' 對象有沒有屬性 'colliderect'。我究竟做錯了什麼?

回答

0

您正在爲self.mouserect分配一個元組,而不是Rect。解決的辦法是換一個Rect它周圍:

self.mouserect=pygame.Rect(pygame.mouse.get_pos(), (8,8)) 
+0

這給了我TypeError:參數必須是矩形樣式對象... – Alex

+0

關於什麼行到底? – Ikke

1

你嘗試使用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()):