1

I am trying to remove the black background from this sprite taken from a sprite sheet. As you'll see in the following is the code and suggested by this post (Transparent spritesheet has black background), I've tried to use the pygame.SRCALPHA flag but this does not seem to work. I've even tried to use the same sprite sheet after converting it with a transparent background and I get the same black border around the Mario sprite.

import pygame

pygame.init()

def imgcolorkey(image, colorkey):
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0, 0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image

class SpriteSheet(object):
    sprite_sheet = None
    def __init__(self, file_name):
        #self.sprite_sheet = pygame.image.load(file_name).convert()
        self.sprite_sheet = pygame.image.load(file_name,pygame.SRCALPHA)
    #self.sheet = load_image(filename)

    def imgat(self, rect, colorkey = None):
        rect = pygame.Rect(rect)
        #image = pygame.Surface(rect.size).convert()
        #image = pygame.Surface(rect.size, pygame.SRCALPHA ).convert()
        image = pygame.Surface(rect.size, pygame.SRCALPHA)
        #image.fill(transColor)
        image.blit(self.sprite_sheet, (0, 0), rect)
        return imgcolorkey(image, colorkey)

    def imgsat(self, rects, colorkey = None):
        imgs = []
        for rect in rects:
            imgs.append(self.imgat(rect, colorkey))
        return imgs

enter image description here

Eric
  • 363
  • 2
  • 9
  • 24

1 Answers1

0

Found the problem.

I was not sending the colorkey argument of -1 when building the image in my main class.

I changed:

mario_image = SPRITE_SHEET.imgsat(MARIO_COORD)

to:

images = SPRITE_SHEET.imgsat(MARIO_COORD,-1)
Eric
  • 363
  • 2
  • 9
  • 24