Trying to create a group of button sprites

Posted by user1449653 on Stack Overflow See other posts from Stack Overflow or by user1449653
Published on 2012-06-17T02:09:03Z Indexed on 2012/06/17 3:16 UTC
Read the original article Hit count: 160

Filed under:
|

Good day,

I have like 15 images I need to be buttons. I have buttons working with a Box() (Box - looks like this)

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

I am trying to make the buttons work with image sprites. So I attempted to copy the class style of the box and do the same for my Icons.. code looks like this...

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

the code in the 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()

This code produces a positional (accepts 1 argument but 2 are there) error or an image is not referenced error (inside the Icon class).

I'm unsure if this is the right way to go about this anyways.. I know for sure that I need to load all the images (as sprites)... store them in an array... and then have my mouse check if it is clicking one of the items in the array using a for loop.

Thanks.

EDIT QUESTION 2:

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

    self.image = pygame.image.load("images/airbrushIC.gif").convert()
    self.rect = self.image.get_rect()
    ic1 = self.image
    self.rect.x = 10
    self.rect.y = 490

    self.image = pygame.image.load("images/fillIC.gif").convert()
    self.rect = self.image.get_rect()
    ic2 = self.image
    self.rect.x = 10
    self.rect.y = 540

Thanks to your help I got the Icons class loading ONE image. Its not loading both. Obviously because its being overwritten by the second one. It seems that "class" for this purpose isn't what I need. Which begs the question how I make sprites outside of a class.. If there is a way to make the class work please let me know.

© Stack Overflow or respective owner

Related posts about python

Related posts about pygame