2011-12-09 72 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()):