Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use circle-based collision with group collision methods in Pygame?

Tags:

python

pygame

Having scoured the documentation and various tutorial sites, I still can't get my head around the way you modify the sprite.collide method with anything other than rectangular bounding-box collision detection.

I have a program which needs to detect collision between a sprite "Hook" and any one of a number of fish, stored in a sprite group called "fishies"

I can use:

for hit in pygame.sprite.spritecollide(self, self.fishies)

to return a list of colliding sprites using the bounding rectangles, but I want to use circles or masks.

The documentation says I can use:

pygame.sprite.spritecollide(self, self.fishies, False, collided = None)

where "collided" is a callback function. But I can't work out what that means. Simply writing:

 pygame.sprite.spritecollide(sprite, group, dokill, pygame.sprite.collide_circle())

produces an error.

Can anyone help, or have I misunderstood how it is supposed to work?

like image 764
Steve Paget Avatar asked Apr 28 '12 10:04

Steve Paget


1 Answers

I think you almost have it -- the problem is you're calling collide_circle instead of passing the function itself. Try something like this:

pygame.sprite.spritecollide(hook, fish, False, pygame.sprite.collide_circle)

The only difference in syntax is leaving off the parentheses. What pygame requires for the collided parameter is a function that takes two sprites and returns a boolean indicating whether or not they collided, so you can pass any function that collides two sprites, even a custom one.

like image 73
0eggxactly Avatar answered Sep 27 '22 20:09

0eggxactly