pygame-ce
pygame-ce copied to clipboard
Rewrite sprite collision functions
trafficstars
The current sprite collision functions code has some code repetition. This implementation structures it differently, which has a few effects.
- If
dokillis true inspritecollideorgroupcollide, the sprites are killed after all colliding sprites have been determined, instead of during iteration (should be unnoticeable) - As a consequence of the previous thing, in these cases, iteration over
group.sprites()is replaced by iteration overgroupitself, resulting in the following:- Avoids a list copy of the group, improving performance slightly in around 1/8 of possible cases
- It is no longer an error to use a non-group iterable of sprites here (already no error if
dokillis False)- I advocate for typing the sprite collide functions as supporting all iterables of sprites, instead of just sprite groups
- "Old-style" groups that don't support iteration raise error now here (already errors if
dokillis False)
- It is now easy to implement an
ignore_selfparameter, e.g., with the new structure
Performance tested in 3.13.0, no regression (similar performance). Renamed a few local variables for clarity.