[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Re: Hexagonal collision detection?



Code without comments:

if rect.collidepoint(pos):
  x = pos[0].rect.w
  y = pos[1].rect.h
  c = tuple(img.get_at((x,y))
  if c[3] > 0:
     return True
return False

/John

2009/9/15 John Eriksson <john@xxxxxxxxxxxx>:
> Hi,
>
> I allways use the alpha value of an button image to accomplish this.
>
> # img is the image of the hex button with an transparent background.
> # rect is the img rectangle positioned at the spot where the button is drawn.
> # pos is the position to check for.
>
> if rect.collidepoint(pos):             # First of all, check if we are
> inside the button image
>   x = pos[0].rect.w                   # Translate the location to a
> local location within the image.
>   y = pos[1].rect.h
>   c = tuple(img.get_at((x,y))     # Get the color for that pixel
> (this works for both pygame 1.9 and older)
>   if c[3] > 0:                            # Did we click on a none
> transparent pixel, Then we have clicked on the image.
>      return True
> return False
>
> My five cents
> /John Eriksson (wmjoers)
>
>
> 2009/9/15 René Dudfield <renesd@xxxxxxxxx>:
>> On Tue, Sep 15, 2009 at 12:05 PM, rygoody <rygoody@xxxxxxxxx> wrote:
>>> Where do I get at this polygonal collision detection? I cannot find it
>>> in the pygame docs.
>>
>> It doesn't come with pygame, you'd need to search for it.  Here's a
>> page here about it:
>>    http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/
>>
>>
>> The color method is probably best - that way you can do collision with
>> any shape at all.
>>
>>
>>>
>>> On Sep 15, 3:30 am, René Dudfield <ren...@xxxxxxxxx> wrote:
>>>> You could use polygon collision detection.
>>>>
>>>> Also you could draw your hex each with a different color, and use that
>>>> as a collision surface.  For example, you drew your 1st hex as color
>>>> (0,0,1,255) then your 2nd as (0,0,2,255) etc.  That would probably be
>>>> fastest, as it's just a lookup table - pretty easy too.
>>>>
>>>> cheers,
>>>>
>>>>
>>
>