Tenouchi
Tenouchi

Reputation: 13

Pygame sprites overlapping not being transparent

So i have been creating a game with sprites and groups. Everything is going really well aside from that one thing that keeps bugging me. A picture says more than a thousand words: http://i.imgur.com/S3fsone.png

As you can see, the background image is shown in the crosshair, just like it should. The ship, however, is getting cut off by rectangular white edges. Both the crosshair and ship are sprites, and the images used in them are transparent PNG images.

I have no idea how to remove this. Currently i am experimenting on the crosshair, so with that in mind, i'll post the relevant code:

#Ship class
class Ship(pygame.sprite.Sprite):

    def __init__(self, position):
        pygame.sprite.Sprite.__init__(self)
        self.imageoriginal = pygame.image.load("ship.png")
        self.image = self.imageoriginal
        self.rect = self.image.get_rect()
        self.rect.center = position

#Crosshair class
class Pointer(pygame.sprite.Sprite):

    def __init__(self, image):
        super(Pointer, self).__init__()
        self.image = image.convert_alpha()
        self.rect = self.image.get_rect()

    def update(self):
        self.rect.center = pygame.mouse.get_pos()


def main():
    pygame.init()

    #Window and background
    window = pygame.display.set_mode((1000, 600), SRCALPHA)
    background = pygame.image.load("background.png")
    window.blit(background, (0, 0))

    #Ship and ship group
    ship = Ship((490, 500))
    theship = pygame.sprite.Group(ship)

    #Crosshair image and group
    crosshairimage = pygame.image.load("images\\crosshair.png")
    pointer = pygame.sprite.Group()

    #Game loop
    running = True
    while running:
        clock.tick(60)

        #Cut out from my event loop. Creates the crosshair that follows the mouse
        for e in pygame.event.get():
            elif e.key == K_4:
                pointer.add(Pointer(crosshairimage))

        #Update and redraw methods for both
        theship.clear(window, background)
        theship.update(window)
        theship.draw(window)
        pointer.clear(window, background)
        pointer.update()
        pointer.draw(window)
        pygame.display.flip()

And that should be it. The crosshair follows the mouse perfectly, and the non-red part of it is transparent on the background. It is not transparent over other sprites however, and that is the ghist of the problem. Other sprites moving over other sprites also cause this problem.

I can guess that it is the rect in the sprites that causes this, but i wouldn't know how to fix it. Tried using a mask but i kind of need the rects to be able to move my sprites don't i? Found no equal to the "self.rect.center = position" method in the mask class.

Much appreciated if anyone could tell me what i can do about this, it's been a headache for a long time now.

Upvotes: 1

Views: 1126

Answers (1)

codythecoder
codythecoder

Reputation: 452

I think the problem is in your drawing section. Instead of having each group clear and draw separately, clear both groups, then update them, then draw them, so instead of:

    #Update and redraw methods for both
    theship.clear(window, background)
    theship.update(window)
    theship.draw(window)
    pointer.clear(window, background)
    pointer.update()
    pointer.draw(window)
    pygame.display.flip()

have:

    #Update and redraw methods for both
    theship.clear(window, background)
    pointer.clear(window, background)
    theship.update(window)
    pointer.update()
    theship.draw(window)
    pointer.draw(window)
    pygame.display.flip()

I hope this helps.

Upvotes: 0

Related Questions