2012-06-17 96 views
0

美好的一天,试图创建一组按钮精灵

我喜欢15个图像我需要是按钮。我有按钮与框()(框 - 看起来像这样)

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 

我想使按钮与图像精灵一起工作。所以,我试图复制框类风格和我的图标做同样的..代码看起来像这样...

class Icons(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.image = pygame.image.load("images/airbrushIC.gif").convert() 
     self.rect = self.image.get_rect() 
     self.rect.x = 25 
     self.rect.y = 550 

在main()

rect = image.get_rect() 
rect.x = 25 
rect.y = 550 
ic1 = Icons((screen.get_rect().x, screen.get_rect().y)) 
screen.blit(ic1.image, ic1.rect) 
pygame.display.update() 

此代码的代码产生一个位置(接受1个参数,但有2个)错误或图像未被引用错误(在Icon类中)。

我不确定这是否正确的方式去反正这..反正我知道我需要加载所有的图像(如精灵)...将它们存储在一个数组中...然后让我的鼠标检查它是否使用for循环单击数组中的某个项目。

谢谢。

+0

如果你要问一个问题,让另外一个问题。不要编辑,并期望人们回答,这不是如何工作。我将编辑你的额外问题。 –

回答

2

您正在试图将参数传递到Icons(),但你的__init__()方法不带任何参数。如果你想通过那些到Sprite()构造函数,那么你可能想是这样的:

class Icons(pygame.sprite.Sprite): 
    def __init__(self, *args): 
     pygame.sprite.Sprite.__init__(self, *args) 
     ... 

此接受任意数量的使用星运营商额外的参数(*args),然后传递他们到精灵的构造。

+0

我可以问为什么Box不需要这些参数?它正在做同样的事情。唯一真正的区别是一个是一个图像,一个是draw.rect。 – user1449653

+1

因为在构建“Box”时没有将参数传递给“Box”构造函数。当你构造一个'Sprite'时,你没有将参数传递给'Sprite'构造函数。 –