Jump to content
  • Advertisement
Sign in to follow this  
Clobslee

SpriteSheet Class won't correctly display transparent images

This topic is 1294 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is the class:

# This class handles sprite sheets
# This was taken from www.scriptefun.com/transcript-2-using
# sprite-sheets-and-drawing-the-background
# I've added some code to fail if the file wasn't found..
# Note: When calling images_at the rect is the format:
# (x, y, x + offset, y + offset)
class SpriteSheet(object):
    def __init__(self, filename):
        try:
            self.sheet = pygame.image.load(filename).convert_alpha()
        except pygame.error, message:
            print 'Unable to load spritesheet image:', filename
            raise SystemExit, message
    # Load a specific image from a specific rectangle
    def image_at(self, rectangle, colorkey = None):
        "Loads image from x,y,x+offset,y+offset"
        rect = pygame.Rect(rectangle)
        image = pygame.Surface(rect.size)
        image.blit(self.sheet, (0, 0), rect)
        if colorkey is not None:
            if colorkey is -1:
                colorkey = image.get_at((0,0))
            image.set_colorkey(colorkey, pygame.RLEACCEL)
        return image
    # Load a whole bunch of images and return them as a list
    def images_at(self, rects, colorkey = None):
        "Loads multiple images, supply a list of coordinates"
        return [self.image_at(rect, colorkey) for rect in rects]
    # Load a whole strip of images
    def load_strip(self, rect, image_count, colorkey = None):
        "Loads a strip of images and returns them as a list"
        tups = [(rect[0]+rect[2]*x, rect[1], rect[2], rect[3])
                for x in range(image_count)]
        return self.images_at(tups, colorkey)

Am I the only one who is having trouble with colorkeys? My .png files are already transparent, but when loading them with this, all transparent pixels are drawn black. Any help!? Thanks a bunch!

Share this post


Link to post
Share on other sites
Advertisement

Knowing nothing about Pygame and almost as little about Python in general...

 

The method "image_at" seems to create a new surface, copy a sprite from the sheet to that surface, and then set the colorkey value for that surface to either the provided colour key or the value of the pixel at top-left of that single sprite. Is that all correct?

 

Does the created surface support colour keying? Does it support alpha? Does the blit method respect transparency? These are all things I'd start by verifying.

 

Also, if your PNG is already transparent, not all transparent pixels are equal unless you are using premultiplied alpha (which seems like something you'd have to explicitly invoke, unless that's a function of the .convert_alpha() method). Two pixels with alpha=0 can have different colour values, and that might be confusing your colorkey selection.

Share this post


Link to post
Share on other sites

I don't think you can have a image with both alpha and color key. Your code should be:

        rect = pygame.Rect(rectangle)
        image = pygame.Surface(rect.size, pygame.SRCALPHA)
        image.fill((0,0,0,0))
        image.blit(self.sheet, (0, 0), rect)
        return image

what this code does is first give the image a alpha channel by using the flag SRCALPHA. Then you are making sure it is fully transparent, since its black by default, and then write your image over it like you normally would.

 

The main important part is to decide whether you want to use the extra processing power and memory to use alpha mapped images, or just use color keys, that look worse. You are trying to do both, which doesn't really work.

 

edit:

I found the file you based this on on the pygame wiki. The only thing you changed was the convert_alpha. Now your sprite sheet has a alpha channel, but not your sprites. To get sprites with alpha, use SRCALPHA

Edited by mousetail

Share this post


Link to post
Share on other sites

I don't think you can have a image with both alpha and color key. Your code should be:

        rect = pygame.Rect(rectangle)
        image = pygame.Surface(rect.size, pygame.SRCALPHA)
        image.fill((0,0,0,0))
        image.blit(self.sheet, (0, 0), rect)
        return image

what this code does is first give the image a alpha channel by using the flag SRCALPHA. Then you are making sure it is fully transparent, since its black by default, and then write your image over it like you normally would.

 

The main important part is to decide whether you want to use the extra processing power and memory to use alpha mapped images, or just use color keys, that look worse. You are trying to do both, which doesn't really work.

 

edit:

I found the file you based this on on the pygame wiki. The only thing you changed was the convert_alpha. Now your sprite sheet has a alpha channel, but not your sprites. To get sprites with alpha, use SRCALPHA

 

Thank you very much! This made sense and worked like a charm. I have to admit a few beers had been consumed the night prior and I wasn't functioning 100% mentally. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!