Skip to content

Commit

Permalink
Improve spritecollideany function
Browse files Browse the repository at this point in the history
  • Loading branch information
aatle committed Nov 3, 2024
1 parent 09e70e1 commit 97f3bb9
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src_py/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1751,19 +1751,16 @@ def spritecollideany(sprite, group, collided=None):
sprites must have a "rect" value, which is a rectangle of the sprite area,
which will be used to calculate the collision.
"""
# pull the default collision function in as a local variable outside
# the loop as this makes the loop run faster
default_sprite_collide_func = sprite.rect.colliderect

if collided is not None:
for group_sprite in group:
if collided(sprite, group_sprite):
return group_sprite
else:
# Special case old behaviour for speed.
# pull the default collision function in as a local variable outside
# the loop as this makes the loop run faster
sprite_rect_collide = sprite.rect.colliderect
for group_sprite in group:
if default_sprite_collide_func(group_sprite.rect):
if sprite_rect_collide(group_sprite.rect):
return group_sprite
return None

0 comments on commit 97f3bb9

Please sign in to comment.