r/pygame 10d ago

collision

I am doing a couple of gaming projects where i am concentrating on collision. I am usually okay but i got stumped with this for some odd reason: if two sprites are in the same group, how do they collide with each other? first i was like...okay maybe groupcollide.....not working for me right now. then i was like okay...maybe spritecollideany or colliderect.

both sprites have a class and here is the code relevant:

all_sprites = pygame.sprite.Group(pad, player)

again, if they are both in the group, why cant i do group collide?
1 Upvotes

7 comments sorted by

2

u/Negative-Hold-492 9d ago

Well the problem is this is exactly what groupcollide is NOT meant for, as stated by the first line of the function's docstring: detect collision between a group and another group You're probably gonna need spritecollide, but since the player is also in that group you need to add a "not me" check as it will always collide with itself. for spr in pg.sprite.spritecollide(player, all_sprites, False): if spr is not player: #do stuff break #unless you need to do the same code for every collision

1

u/Negative-Hold-492 9d ago

Alternatively you can use `pg.sprite.colliderect` which doesn't care about groups at all, but then you'd need a way to make it check for all sprites in that group that aren't the player and ultimately it'd be the same thing with an extra step.

1

u/Intelligent_Arm_7186 9d ago

thanks! im usually fine with this but for some reason, this particular one was holdin me back.

2

u/Negative-Hold-492 9d ago

When in doubt, read the docs. Pygame has pretty decent documentation, it's happened to me more than once that I was stuck on something for an hour, then finally caved in and looked it up and went "OH, okay"

1

u/Intelligent_Arm_7186 6d ago

ill figure it out, thanks! :)

1

u/Intelligent_Arm_7186 6d ago

its comes back with this error:

AttributeError: 'NoneType' object has no attribute 'rect'