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

Re: [pygame] Collision Detection



gumuz@looze.net wrote:
I'm having a small arena where 4 or 5 'robottanks' are driving around. The
sprite-rotations are generated for each tank upfront, before the game
starts. I attached the sprite, so yu can see what it looks likes. Ofcourse
these rotated sprites are still rectangular images.

I have no experience really in writing games and I don't have a clue how
to go about creating some descent collision detection for my little battle
tanks :)
looking at your tank sprite (nice) it looks like you will be fine doing straight 'rectangle' collisions. The Rect.colliderect() method is probably the easiest way to do this, it just tests if two Rect objects overlap.

because the tanks will be rotating, it is better to keep a separate Rect in the tank object used for collisions. best to keep this Rect slightly smaller than the visible area of the tank. each time you update the tank position, you just center this collision Rect on the image Rect and you should be set to go.

class Tank:
def __init__(self, ...):
...
self.crashrect = Rect(0, 0, 20, 20)
def update(self, ...):
...
self.crashrect.center = self.rect.center

if all the objects in your game have this "crashrect" attribute, it should be easy to compare any two objects to see if they are 'touching'.
different sized objects will likely need different sized crashrects. bullets would likely only be a couple pixels.

that works good for 'projectile' style shooting. if you are going for something more like 'hitscan' shooting (as they are called in FPS) you will need to move into the realm of line intersections against the 'crashrect' attributes.


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org