[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[pygame] surfarray slicing and collision detection
>to stick with the X,Y format.
Yes, it pretty weird doing arr[ypos, xpos] in C++ too, so I see that
> Numeric makes it quite easy to work with either
>with the transpose() call, which will return an array that references all
>same data as the original array, with the axis orders swapped.
I didn't know that, that sounds very nice especially for debug print
statements of the surfarrays which are really hard to read. I guess I'll
have to make myself more aquianted with Numeric.
>"transparent" and which are "opaque". it could be applied to pixel-level
>collision detection routines and that sort of fun.
Yeah I found out reading the archives and made some routines for my sprites,
they didn't seem to slow down things too much from what I've seen.
Here they are if anyone's interested. I first tried the code that Pete
posted, but I didn't understand it so I decided to
make it myself picking up some knowledge of the rect, surfarr and Surface
modules in the process (which can't be bad)
These are methods copied from the Sprite/GameObject base class that I'm
currently using to explore game writing with PyGame:
pos method returns a Rect with screen position of the object.
_surface is the surface with the current bitmap for the sprite in it
The code is supposed to be called like this:
# do stuff with objects when they hit each other, or let objects do it
def _adjustOverlapRectToSpriteCoordinates(self, other):
overlappingRect = self.pos().clip(other.pos())
return overlappingRect.move(-self.pos().left, -self.pos().top)
def _colorkeyArrayFor(self, rect):
subsurface = self._surface.subsurface(rect).convert()
def _isOverlapNonTransparent(self, other):
rect1 = self._adjustOverlapRectToSpriteCoordinates(other)
rect2 = other._adjustOverlapRectToSpriteCoordinates(self)
arr1 = self._colorkeyArrayFor(rect1)
arr2 = other._colorkeyArrayFor(rect2)
if arr1 & arr2:
def isIntersecting(self, other):
if not self.pos().colliderect(other.pos()):
I don't know why but if I don't call the convert method of the subsurface in
the method _colorkeyArrayFor I'll get an ValueError saying that the surface
is locked when I try to blit the sprite surface to the backbuffer. Anyway it
works fine for now even if I have to make the extra copy (with convert).
>good luck with your continued hacking :]
Thanks, I need that :-).
pygame mailing list