[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [pygame] Determining whether a Pixel in a Sprite is Transparent?
On Tue, 2002-06-18 at 16:07, Pete Shinners wrote:
> Steven M. Castellotti wrote:
> > I want to take a user's mouse click coordinates, and use it to
> > determine whether or not a user clicked on a particular image, taking
> > transparency at the corners into mind.
>
> this won't be difficult. you'll need to deal with the fact pygame
> can do transparancy with colorkeys or with per pixel alpha. let's
> see if i can come up with some mojo..
>
>
> def collide_surface(image, pos):
> 'return true if pixel at pos is solid'
> try:
> pix = image.get_at(pos)
> except IndexError:
> return 0
> if pix == image.get_colorkey():
> return 1
> return pix[3] > 4
>
>
> this should take into account nearly any type of settings for a
> surface. it will even handle cases where "pos" is outside the image.
> that should save you from even needing to check the boundingbox :]
This code seems to work great, thank you, but I do seem to be having a
small problem. I've created a few PNG images with the gimp, and saved
them with the options:
Interacing (Adam7) - Off
Save background color - On
Save Gamma - Off
Save Layer Offset - Off
Save Resolution - On
Save Creation Time - On
Compression Level: 6
When I blit these images on top of my graphic area, I get black in all
of the transparent areas of the image. (Turning Save Gamma on has the
same effect). This is verfied when I make the get_at() call in this
routine, as the routine will return:
(0,0,0,255) (with the 255 signifying opaque, correct?)
If I save the same image as a .gif file, I do not get the black border,
but clicking on the "clear" part of the image returns:
(255, 255, 255, 255)
The result therefore is that colorkey returns None when the image is
saved as a PNG, and clicking in the transparent area (displayed as
black) is detected as a collision by the function. Saving the image as a
gif and clicking in the transparent area is also determined to be a
collision, as get_colorkey and get_at return the same values.
As if to make it that much more confusing, I have a routine that allows
me to display a graphic in place of a mouse pointer, blitting the
previous surface before blitting the mouse graphic in the new position,
and updating the rects of the changes through a call to display_update.
If the mouse pointer is a PNG with transparency, it gets displayed
correctly. Strangest thing.
Can you tell if it sounds like I'm doing something wrong, or if there's
another format/setting I should save my images in?
(and thanks again for the help!)
-Steve Castellotti
SteveC@innocent.com
http://cogengine.sourceforge.net/
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org