-
-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sprite group collide tweaks #3197
base: main
Are you sure you want to change the base?
Changes from 3 commits
ff76268
a304f84
2b6f846
3d50280
7866505
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1664,10 +1664,10 @@ def collide_mask(left, right): | |||||
return leftmask.overlap(rightmask, (xoffset, yoffset)) | ||||||
|
||||||
|
||||||
def spritecollide(sprite, group, dokill, collided=None): | ||||||
def spritecollide(sprite, group, dokill, collided=None, ignore_self=False): | ||||||
"""find Sprites in a Group that intersect another Sprite | ||||||
|
||||||
pygame.sprite.spritecollide(sprite, group, dokill, collided=None): | ||||||
pygame.sprite.spritecollide(sprite, group, dokill, collided=None, ignore_self=False): | ||||||
return Sprite_list | ||||||
|
||||||
Return a list containing all Sprites in a Group that intersect with another | ||||||
|
@@ -1683,6 +1683,9 @@ def spritecollide(sprite, group, dokill, collided=None): | |||||
sprites must have a "rect" value, which is a rectangle of the sprite area, | ||||||
which will be used to calculate the collision. | ||||||
|
||||||
The ignore_self argument is a bool. If set to True, the sprite will not register | ||||||
a collision with itself, even if it is contained within the group being tested. | ||||||
|
||||||
""" | ||||||
# pull the default collision function in as a local variable outside | ||||||
# the loop as this makes the loop run faster | ||||||
|
@@ -1695,25 +1698,39 @@ def spritecollide(sprite, group, dokill, collided=None): | |||||
for group_sprite in group.sprites(): | ||||||
if collided is not None: | ||||||
if collided(sprite, group_sprite): | ||||||
group_sprite.kill() | ||||||
append(group_sprite) | ||||||
if not ignore_self or sprite != group_sprite: | ||||||
Comment on lines
1700
to
+1701
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As indicated below you should probably do the if check for self first and then the collision check as the inner if as the self check will be quick to do and the collision relatively slower. |
||||||
group_sprite.kill() | ||||||
append(group_sprite) | ||||||
else: | ||||||
if default_sprite_collide_func(group_sprite.rect): | ||||||
group_sprite.kill() | ||||||
append(group_sprite) | ||||||
if not ignore_self or sprite != group_sprite: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another place to swap the order of the collision check with the self check. |
||||||
group_sprite.kill() | ||||||
append(group_sprite) | ||||||
|
||||||
return crashed | ||||||
|
||||||
if collided is not None: | ||||||
if ignore_self: | ||||||
return [ | ||||||
group_sprite for group_sprite in group if collided(sprite, group_sprite) and sprite != group_sprite | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think it will be ever so slightly faster to do the self check first as short circuit evaluation will cause it to skip the collision check if the left hand side of the and fails and the collision check is generally going to be slower. See: |
||||||
] | ||||||
else: | ||||||
return [ | ||||||
group_sprite for group_sprite in group if collided(sprite, group_sprite) | ||||||
] | ||||||
|
||||||
if ignore_self: | ||||||
return [ | ||||||
group_sprite for group_sprite in group if collided(sprite, group_sprite) | ||||||
group_sprite | ||||||
for group_sprite in group | ||||||
if default_sprite_collide_func(group_sprite.rect) and sprite != group_sprite | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
] | ||||||
else: | ||||||
return [ | ||||||
group_sprite | ||||||
for group_sprite in group | ||||||
if default_sprite_collide_func(group_sprite.rect) | ||||||
] | ||||||
|
||||||
return [ | ||||||
group_sprite | ||||||
for group_sprite in group | ||||||
if default_sprite_collide_func(group_sprite.rect) | ||||||
] | ||||||
|
||||||
|
||||||
def groupcollide(groupa, groupb, dokilla, dokillb, collided=None): | ||||||
|
@@ -1752,7 +1769,7 @@ def groupcollide(groupa, groupb, dokilla, dokillb, collided=None): | |||||
return crashed | ||||||
|
||||||
|
||||||
def spritecollideany(sprite, group, collided=None): | ||||||
def spritecollideany(sprite, group, collided=None, ignore_self=False): | ||||||
"""finds any sprites in a group that collide with the given sprite | ||||||
|
||||||
pygame.sprite.spritecollideany(sprite, group): return sprite | ||||||
|
@@ -1770,6 +1787,8 @@ 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. | ||||||
|
||||||
The ignore_self argument is a bool. If set to True, the sprite will not register | ||||||
a collision with itself, even if it is contained within the group being tested. | ||||||
|
||||||
""" | ||||||
# pull the default collision function in as a local variable outside | ||||||
|
@@ -1779,10 +1798,12 @@ def spritecollideany(sprite, group, collided=None): | |||||
if collided is not None: | ||||||
for group_sprite in group: | ||||||
if collided(sprite, group_sprite): | ||||||
return group_sprite | ||||||
if not ignore_self or sprite != group_sprite: | ||||||
return group_sprite | ||||||
else: | ||||||
# Special case old behaviour for speed. | ||||||
for group_sprite in group: | ||||||
if default_sprite_collide_func(group_sprite.rect): | ||||||
return group_sprite | ||||||
if not ignore_self or sprite != group_sprite: | ||||||
Comment on lines
+1801
to
+1807
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And again, two more places to do the self check first - then the collision check. |
||||||
return group_sprite | ||||||
return None |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 505: is this an ok way to test against an empty returned list? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.