pygame-ce icon indicating copy to clipboard operation
pygame-ce copied to clipboard

Rewrite sprite collision functions

Open aatle opened this issue 1 year ago • 2 comments
trafficstars

The current sprite collision functions code has some code repetition. This implementation structures it differently, which has a few effects.

  • If dokill is true in spritecollide or groupcollide, 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 over group itself, 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 dokill is 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 dokill is False)
  • It is now easy to implement an ignore_self parameter, e.g., with the new structure

Performance tested in 3.13.0, no regression (similar performance). Renamed a few local variables for clarity.

aatle avatar Nov 03 '24 03:11 aatle