[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] today's little update



hello martin. well it's true that sometimes you just need to do
pixel-level collision and there is no substitute :]

in the latest cvs i've added some functions to get some nice
Numeric arrays based on stuff like alpha and colorkeys. 
combining this with the code peter nicolai put up, we can
make a python function that should do a pretty good job of it.

assuming all the code to find the overlapping area between
the two sprites works (peter nicolai?) this code should do
the job

try:
    from Numeric import *
    import pygame.surfarray as surfarray
except ImportError:
    raise ImportError, "collide_sprite requires Numeric python"


def sub_array(array, rect):
    return array[rect.left:rect.right+1,rect.top:rect2.bottom+1]


def collide_sprite(surf1, offset1, surf2, offset2):
    "two images and the 'screen-space' position of each"

    rect1 = surface1.get_rect().move(offset1)
    rect2 = surface2.get_rect().move(offset2)
    array1 = surface1.array_colorkey(surf1)
    array2 = surface2.array_colorkey(surf2)
    
    overlap_rect = rect1.clip(rect2)
    if overlap_rect:
        mini_rect1 = overlap_rect.move(-array(offset1))
        mini_rect2 = overlap_rect.move(-array(offset2))

        mini_array1 = sub_array(array1, mini_rect1)     
        mini_array2 = sub_array(array2, mini_rect2)

        if mini_array1 & mini_array2:
            return 1
    return 0