2012-06-16 43 views
0

我想要一个按钮打印支票并运行我的铅笔功能。此刻,如果我将鼠标悬停在Box Sprite上,它将运行打印和铅笔功能。它应该是ONCLICK它运行那些2.任何人都可以帮我吗?谢谢! (这应该是所有相关的代码,如果你需要更多信息,请让我知道:)pygame按钮不会在按下/只在悬停时工作

class Box(pygame.sprite.Sprite): 
def __init__(self): 
    pygame.sprite.Sprite.__init__(self) 
    self.image = pygame.Surface((35, 30)) 
    self.image = self.image.convert() 
    self.image.fill((255, 0, 0)) 
    self.rect = self.image.get_rect() 
    self.rect.centerx = 25 
    self.rect.centery = 505 
    self.dx = 10 
    self.dy = 10 

while keepGoing: 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      keepGoing = False 
     box = Box() 
     allSprites = pygame.sprite.Group(box) 
     allSprites.draw(screen) 

     if event.type == MOUSEMOTION: 
      x,y = event.pos 
      if box.rect.collidepoint(x,y) and pygame.MOUSEBUTTONUP:      
       print("collide works") 
       pencil(background,clock,keepGoing,screen) 
     pygame.display.flip() 

回答

4

您的代码不检查鼠标的点击,而鼠标移动。

如果你想被测试为你的箱子点击,改变条件检查MOUSEBUTTONDOWNMOUSEBUTTONUP事件(这取决于你想反应是什么的点击一部分),而不是MOUSEMOTION事件。

虽然您的代码还存在其他一些问题。例如,你在每个事件之后创建你的Box和Group。在进入游戏循环之前,可能你只想创建一次它们(这样做会更有意义并且性能更好)。

+0

叹息...谢谢。我现在感觉非常愚蠢。所有的例子/教程等我见过..让你看起来像你检查鼠标的动作..然后检查发生的鼠标事件(即mousedown)。再次感谢。 – user1449653