This commit is contained in:
wittmannzl103469 committed 2026-11-05 15:08:42 +01:00
1 parent de41a02749
commit 9af09cebd3
764 files changed
+54218 -78859

No files matched your search

@@ -1,4 +1,4 @@
# pygame-ce - Python Game Library
# pygame - Python Game Library
# Copyright (C) 2000-2003, 2007 Pete Shinners
# (C) 2004 Joe Wreschnig
# This library is free software; you can redistribute it and/or
@@ -35,7 +35,8 @@ The basic Sprite class can draw the Sprites it contains to a Surface. The
Group.draw() method requires that each Sprite have a Surface.image attribute
and a Surface.rect. The Group.clear() method requires these same attributes
and can be used to erase all the Sprites with background. There are also
more advanced Groups: pygame.sprite.RenderUpdates().
more advanced Groups: pygame.sprite.RenderUpdates() and
pygame.sprite.OrderedUpdates().
Lastly, this module contains several collision functions. These help find
sprites inside multiple groups that have intersecting bounding rectangles.
@@ -83,14 +84,16 @@ Sprites are not thread safe, so lock them yourself if using threads.
# should handle just about every need, but perhaps more optimized
# specific ones that aren't quite so general but fit into common
# specialized cases.
from typing import Generic, TypeVar
import types
from weakref import WeakSet
from warnings import warn
import pygame
from pygame.mask import from_surface
from pygame.rect import Rect
from pygame.time import get_ticks
from pygame.mask import from_surface
class Sprite:
@@ -109,28 +112,10 @@ class Sprite:
"""
def __init__(self, *groups):
self.__g = {} # The groups the sprite is in
self.__image: pygame.surface.Surface | None = None
self.__rect: pygame.rect.Rect | None = None
self.__g = set() # The groups the sprite is in
if groups:
self.add(*groups)
@property
def image(self):
return self.__image
@image.setter
def image(self, value: pygame.surface.Surface | None):
self.__image = value
@property
def rect(self):
return self.__rect
@rect.setter
def rect(self, value: pygame.rect.Rect | None):
self.__rect = value
def add(self, *groups):
"""add the sprite to groups
@@ -173,7 +158,7 @@ class Sprite:
:param group: The group we are adding to.
"""
self.__g[group] = 0
self.__g.add(group)
def remove_internal(self, group):
"""
@@ -181,7 +166,7 @@ class Sprite:
:param group: The group we are removing from.
"""
del self.__g[group]
self.__g.remove(group)
def update(self, *args, **kwargs):
"""method to control sprite behavior
@@ -248,12 +233,12 @@ class Sprite:
:return: layer as an int, or raise AttributeError.
"""
return getattr(self, "_layer")
return self._layer
@layer.setter
def layer(self, value):
if not self.alive():
setattr(self, "_layer", value)
self._layer = value
else:
raise AttributeError(
"Can't set layer directly after "
@@ -263,6 +248,17 @@ class Sprite:
)
class WeakSprite(Sprite):
"""A subclass of Sprite that references its Groups weakly. This
means that any group this belongs to that is not referenced anywhere
else is garbage collected automatically.
"""
def __init__(self, *groups):
super().__init__(*groups)
self.__dict__["_Sprite__g"] = WeakSet(self._Sprite__g)
class DirtySprite(Sprite):
"""a more featureful subclass of Sprite with more attributes
@@ -290,7 +286,7 @@ class DirtySprite(Sprite):
the screen.)
_layer = 0
0 is the default value but this can be set differently
0 is the default value but this is able to be set differently
when subclassing.
"""
@@ -358,7 +354,13 @@ class DirtySprite(Sprite):
)
class AbstractGroup:
class WeakDirtySprite(WeakSprite, DirtySprite):
"""A subclass of WeakSprite and DirtySprite that combines the benefits
of both classes.
"""
class AbstractGroup(Generic[TypeVar("T")]):
"""base class for containers of sprites
AbstractGroup does everything needed to behave as a normal group. You can
@@ -370,9 +372,7 @@ class AbstractGroup:
"""
__class_getitem__ = classmethod(types.GenericAlias)
# protected identifier value to identify sprite groups, and avoid infinite recursion
# dummy val to identify sprite groups, and avoid infinite recursion
_spritegroup = True
def __init__(self):
@@ -382,7 +382,7 @@ class AbstractGroup:
def sprites(self):
"""get a list of sprites in the group
Group.sprite(): return list
Group.sprites(): return list
Returns an object that can be looped over with a 'for' loop. (For now,
it is always a list, but this could change in a future version of
@@ -411,7 +411,8 @@ class AbstractGroup:
:param sprite: The sprite we are removing.
"""
if lost_rect := self.spritedict[sprite]:
lost_rect = self.spritedict[sprite]
if lost_rect:
self.lostsprites.append(lost_rect)
del self.spritedict[sprite]
@@ -554,10 +555,12 @@ class AbstractGroup:
for sprite in self.sprites():
sprite.update(*args, **kwargs)
def draw(self, surface, bgd=None, special_flags=0): # noqa pylint: disable=unused-argument; bgd arg used in LayeredDirty
def draw(
self, surface, bgsurf=None, special_flags=0
): # noqa pylint: disable=unused-argument; bgsurf arg used in LayeredDirty
"""draw all sprites onto the surface
Group.draw(surface, bgd=None, special_flags=0): return Rect_list
Group.draw(surface, special_flags=0): return Rect_list
Draws all of the member sprites onto the given surface.
@@ -661,24 +664,8 @@ class Group(AbstractGroup):
self.add(*sprites)
class RenderPlain(Group):
def __init__(self, *sprites):
super().__init__(*sprites)
warn(
"This class is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)
class RenderClear(Group):
def __init__(self, *sprites):
super().__init__(*sprites)
warn(
"This class is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)
RenderPlain = Group
RenderClear = Group
class RenderUpdates(Group):
@@ -691,7 +678,7 @@ class RenderUpdates(Group):
"""
def draw(self, surface, bgd=None, special_flags=0):
def draw(self, surface, bgsurf=None, special_flags=0):
surface_blit = surface.blit
dirty = self.lostsprites
self.lostsprites = []
@@ -712,19 +699,35 @@ class RenderUpdates(Group):
class OrderedUpdates(RenderUpdates):
"""RenderUpdates class that draws Sprites in order of addition
pygame.sprite.OrderedUpdates(*sprites): return OrderedUpdates
This class derives from pygame.sprite.RenderUpdates(). It maintains
the order in which the Sprites were added to the Group for rendering.
This makes adding and removing Sprites from the Group a little
slower than regular Groups.
"""
def __init__(self, *sprites):
super().__init__(*sprites)
warn(
"OrderedUpdates is now just an alias to RenderUpdates, order of "
"sprites is now maintained in all sprite Group classes. This "
"class is deprecated and will be removed in a future version.",
DeprecationWarning,
stacklevel=2,
)
self._spritelist = []
RenderUpdates.__init__(self, *sprites)
def sprites(self):
return self._spritelist.copy()
def add_internal(self, sprite, layer=None):
RenderUpdates.add_internal(self, sprite)
self._spritelist.append(sprite)
def remove_internal(self, sprite):
RenderUpdates.remove_internal(self, sprite)
self._spritelist.remove(sprite)
class LayeredUpdates(AbstractGroup):
"""LayeredUpdates Group handles layers, which are drawn like RenderUpdates
"""LayeredUpdates Group handles layers, which are drawn like OrderedUpdates
pygame.sprite.LayeredUpdates(*sprites, **kwargs): return LayeredUpdates
@@ -853,17 +856,17 @@ class LayeredUpdates(AbstractGroup):
del self._spritelayers[sprite]
def sprites(self):
"""return an ordered list of sprites (first back, last top).
"""return a ordered list of sprites (first back, last top).
LayeredUpdates.sprites(): return sprites
"""
return self._spritelist.copy()
def draw(self, surface, bgd=None, special_flags=0):
def draw(self, surface, bgsurf=None, special_flags=0):
"""draw all sprites in the right order onto the passed surface
LayeredUpdates.draw(surface, bgd=None, special_flags=0): return Rect_list
LayeredUpdates.draw(surface, special_flags=0): return Rect_list
"""
spritedict = self.spritedict
@@ -1020,13 +1023,13 @@ class LayeredUpdates(AbstractGroup):
return self._spritelist[-1]
def get_sprites_from_layer(self, layer):
"""return all sprites from a layer ordered as they were added
"""return all sprites from a layer ordered as they where added
LayeredUpdates.get_sprites_from_layer(layer): return sprites
Returns all sprites from a layer. The sprites are ordered in the
sequence that they were added. (The sprites are not removed from the
layer).
sequence that they where added. (The sprites are not removed from the
layer.
"""
sprites = []
@@ -1068,7 +1071,7 @@ class LayeredDirty(LayeredUpdates):
It uses the dirty flag technique and is therefore faster than
pygame.sprite.RenderUpdates if you have many static sprites. It
also switches automatically between dirty rect updating and full
screen drawing, so you do not have to worry which would be faster.
screen drawing, so you do no have to worry which would be faster.
As with the pygame.sprite.Group, you can specify some additional attributes
through kwargs:
@@ -1133,16 +1136,16 @@ class LayeredDirty(LayeredUpdates):
LayeredUpdates.add_internal(self, sprite, layer)
def draw(self, surface, bgd=None, special_flags=None):
def draw(self, surface, bgsurf=None, special_flags=None):
"""draw all sprites in the right order onto the given surface
LayeredDirty.draw(surface, bgd=None, special_flags=0): return Rect_list
LayeredDirty.draw(surface, bgsurf=None, special_flags=None): return Rect_list
You can pass the background too. If a self.bgd is already set to some
value that is not None, then the bgd argument has no effect.
value that is not None, then the bgsurf argument has no effect.
Passing a value to special_flags will pass that value as the
special_flags argument to all Surface.blit calls, overriding
the sprite.blendmode attribute.
special_flags argument to pass to all Surface.blit calls, overriding
the sprite.blendmode attribute
"""
# functions and classes assigned locally to speed up loops
@@ -1157,8 +1160,8 @@ class LayeredDirty(LayeredUpdates):
rect_type = Rect
surf_blit_func = surface.blit
if bgd is not None:
self._bgd = bgd
if bgsurf is not None:
self._bgd = bgsurf
local_bgd = self._bgd
surface.set_clip(latest_clip)
@@ -1565,7 +1568,7 @@ def collide_circle(left, right):
except AttributeError:
leftrect = left.rect
# approximating the radius of a square by using half of the diagonal,
# might give false positives (especially if it's a long small rect)
# might give false positives (especially if its a long small rect)
leftradius = 0.5 * ((leftrect.width**2 + leftrect.height**2) ** 0.5)
# store the radius on the sprite for next time
left.radius = leftradius
@@ -1575,7 +1578,7 @@ def collide_circle(left, right):
except AttributeError:
rightrect = right.rect
# approximating the radius of a square by using half of the diagonal
# might give false positives (especially if it's a long small rect)
# might give false positives (especially if its a long small rect)
rightradius = 0.5 * ((rightrect.width**2 + rightrect.height**2) ** 0.5)
# store the radius on the sprite for next time
right.radius = rightradius
@@ -1706,21 +1709,36 @@ def spritecollide(sprite, group, dokill, collided=None):
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 dokill:
crashed = []
append = crashed.append
for group_sprite in group.sprites():
if collided is not None:
if collided(sprite, group_sprite):
group_sprite.kill()
append(group_sprite)
else:
if default_sprite_collide_func(group_sprite.rect):
group_sprite.kill()
append(group_sprite)
return crashed
if collided is not None:
collided_sprites = [
return [
group_sprite for group_sprite in group if collided(sprite, group_sprite)
]
else:
sprite_rect_collide = sprite.rect.colliderect
collided_sprites = [
group_sprite
for group_sprite in group
if sprite_rect_collide(group_sprite.rect)
]
if dokill:
for group_sprite in collided_sprites:
group_sprite.kill()
return collided_sprites
return [
group_sprite
for group_sprite in group
if default_sprite_collide_func(group_sprite.rect)
]
def groupcollide(groupa, groupb, dokilla, dokillb, collided=None):
@@ -1741,18 +1759,22 @@ def groupcollide(groupa, groupb, dokilla, dokillb, collided=None):
that will be used to calculate the collision.
"""
collided_sprites = {}
crashed = {}
# pull the collision function in as a local variable outside
# the loop as this makes the loop run faster
sprite_collide_func = spritecollide
for group_a_sprite in groupa:
collisions = sprite_collide_func(group_a_sprite, groupb, dokillb, collided)
if collisions:
collided_sprites[group_a_sprite] = collisions
if dokilla:
for group_a_sprite in collided_sprites:
group_a_sprite.kill()
return collided_sprites
for group_a_sprite in groupa.sprites():
collision = sprite_collide_func(group_a_sprite, groupb, dokillb, collided)
if collision:
crashed[group_a_sprite] = collision
group_a_sprite.kill()
else:
for group_a_sprite in groupa:
collision = sprite_collide_func(group_a_sprite, groupb, dokillb, collided)
if collision:
crashed[group_a_sprite] = collision
return crashed
def spritecollideany(sprite, group, collided=None):
@@ -1773,16 +1795,19 @@ 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:
# 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
# Special case old behaviour for speed.
for group_sprite in group:
if sprite_rect_collide(group_sprite.rect):
if default_sprite_collide_func(group_sprite.rect):
return group_sprite
return None