How do you use circular collision with group collision methods in Pygame?

Looking through the documentation and various tutorial sites, I still cannot understand how you are modifying the sprite.collide method with anything other than detecting collisions of rectangular borders.

I have a program that should detect a collision between the hook sprite and any of several 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 bounding rectangles, but I want to use circles or masks.

The documentation states that I can use:

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

where "collided" is a callback function. But I can’t understand what this means. Just write:

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

causes an error.

Can someone help, or did I misunderstand how it should work?

+6
source share
1 answer

I think you almost have this - the problem is that you are 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 is the lack of parentheses. What pygame is required for the collided parameter is a function that takes two sprites and returns a boolean indicating whether they collided or not, so you can pass any function that collides with two sprites, even the regular one.

+4
source

Source: https://habr.com/ru/post/914408/


All Articles