user1449653
user1449653

Reputation: 73

Trying to create a group of button sprites

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.

Upvotes: 0

Views: 139

Answers (1)

Gareth Latty
Gareth Latty

Reputation: 89017

You are trying to pass an argument into Icons(), but your __init__() method takes no arguments. If you wanted to pass those onto the Sprite() constructor, then you probably wanted something like:

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

This accepts any number of extra arguments (*args) using the star operator, then passes them into the sprite constructor.

Upvotes: 2

Related Questions