Sign in to follow this  

pyGame Colorkey

This topic is 3780 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

As suggested I created a new thread for this question: I have tried to make the gray on my character's background dissapear (set colorkey). I have tried many different things including finding his RGB value, using a pyGame function to find it, to using the function to set his value. None of these worked so I was wondering if you all had any ideas? The current image load I am using is from an example, so I expected it to integrate fairly flawlessly with my code, but it did not solve my problem. Code: http://rafb.net/p/5olbDe29.html Specifically: 21-33, 48-54

Share this post


Link to post
Share on other sites
I don't think that pygame color key is what you are looking for. I think that pygame color key sets the rgb of rects so that they are matching to a plain background. For instance if your entire background was black, and your drew a circle in a rect, you would want the rect to be black to match the background. From the looks of it, you aren't using a background that is entirely one rgb color. What you want to do is make a .gif picture that has a transparent background. Or thats at least what I think.



Joe

Share this post


Link to post
Share on other sites
I see you're using PNGs. This may seem counterintuitive, but check that your PNGs do NOT have an alpha channel.
if (Surface.get_masks()[3]!=0, you have alpha)
If a surface has an alpha channel the alpha overrides the colorkeying.

Or you can go the other route, actually use that alpha channel instead of colorkeying. Just set the backgrounds to transparent. This may not be as fast. (I haven't benchedmarked though)

Share this post


Link to post
Share on other sites
Well, color-key blitting is a method to mark which pixels of a bitmap should not be rendered (hence fully transparent) by specifying a color value as a color key, there are two types of them, source color keys and destination color keys, color-key blitting is also supported by hardware in 99% (if not 100%) of the graphics cards that are used today, so you can definitely use it to show transparency.
As for pyGame, I haven't tried it, but reading through the documentation:
Quote:

Surface.set_colorkey

Set the transparent colorkey
Surface.set_colorkey(Color, flags=0): return None
Surface.set_colorkey(None): return None

Set the current color key for the Surface. When blitting this Surface onto a destination, and pixels that have the same color as the colorkey will be transparent. The color can be an RGB color or a mapped color integer. If None is passed, the colorkey will be unset.

The colorkey will be ignored if the Surface is formatted to use per pixel alpha values. The colorkey can be mixed with the full Surface alpha value.

The optional flags argument can be set to pygame.RLEACCEL to provide better performance on non accelerated displays. An RLEACCEL Surface will be slower to modify, but quicker to blit as a source.

And looking at your code, I see that you are using a png, make sure it doesn't have an alpha channel in it which would disable color-keying.

[EDIT: Oops, Popcorn Mariachi got it before I do, sorry.)

Share this post


Link to post
Share on other sites
I tried the following:

1. Create BMP or JPEGs and use colorkey. This failed, with the code I used before but with the file names changed.

2. Create transparent GIF without colorkey. This worked, so even if it may load slower I am going with this (first game anyways, load times won't matter for now).

Thanks for everyone's help, I am going to read more into PNGs and such in the next few days. :D

Share this post


Link to post
Share on other sites
JPEG is a lossy format, it can (and will) loss pixel information because of the way it compresses images.
So, it is not a good choice for color keying.
However, I don't know how BMP can fail.
As for GIF, the problem with it is it's limited colors, but for 2D games, that may actually be desired.
;)

Share this post


Link to post
Share on other sites

This topic is 3780 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this