Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions buildconfig/stubs/pygame/sprite.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,21 @@ from pygame.typing import Point, RectLike
# Some sprite functions only need objects with certain attributes, not always a sprite
class _HasRect(Protocol):
@property
def rect(self) -> Optional[Union[FRect, Rect]]: ...
def rect(self) -> Union[FRect, Rect]: ...

# image in addition to rect
class _HasImageAndRect(_HasRect, Protocol):
@property
def image(self) -> Optional[Surface]: ...
def image(self) -> Surface: ...

# mask in addition to rect
class _HasMaskAndRect(_HasRect, Protocol):
@property
def mask(self) -> Mask: ...

class Sprite(_HasImageAndRect):
@property
def image(self) -> Optional[Surface]: ...
@image.setter
def image(self, value: Optional[Surface]) -> None: ...
@property
def rect(self) -> Optional[Union[FRect, Rect]]: ...
@rect.setter
def rect(self, value: Optional[Union[FRect, Rect]]) -> None: ...
image: Surface # Uses attribute annotation instead of property to satisfy stubtest
rect: Union[FRect, Rect] # Same as image, see above
@property
def layer(self) -> int: ...
@layer.setter
Expand All @@ -68,7 +62,7 @@ class Sprite(_HasImageAndRect):
class DirtySprite(Sprite):
dirty: int
blendmode: int
source_rect: Union[FRect, Rect]
source_rect: Optional[Union[FRect, Rect]]
visible: int
_layer: int

Expand Down Expand Up @@ -150,7 +144,7 @@ class LayeredDirty(LayeredUpdates[_DirtySpriteT]):
def clear(self, surface: Surface, bgd: Surface) -> None: ... # type: ignore[override]
def repaint_rect(self, screen_rect: RectLike) -> None: ...
def set_clip(self, screen_rect: Optional[RectLike] = None) -> None: ...
def get_clip(self) -> Union[FRect, Rect]: ...
def get_clip(self) -> Optional[Union[FRect, Rect]]: ...
def set_timing_threshold(self, time_ms: SupportsFloat) -> None: ...
@deprecated(
"since 2.1.1. Use `pygame.sprite.LayeredDirty.set_timing_threshold` instead"
Expand Down
28 changes: 7 additions & 21 deletions src_py/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@
# specialized cases.

import types
from typing import Optional
from typing import Union
from warnings import warn

import pygame
from pygame.mask import from_surface
from pygame.rect import Rect
from pygame.rect import FRect, Rect
from pygame.surface import Surface
from pygame.time import get_ticks


Expand All @@ -109,29 +110,14 @@ class Sprite:

"""

image: Surface
rect: Union[Rect, FRect]

def __init__(self, *groups):
self.__g = {} # The groups the sprite is in
self.__image: Optional[pygame.surface.Surface] = None
self.__rect: Optional[pygame.rect.Rect] = None
if groups:
self.add(*groups)

@property
def image(self):
return self.__image

@image.setter
def image(self, value: Optional[pygame.surface.Surface]):
self.__image = value

@property
def rect(self):
return self.__rect

@rect.setter
def rect(self, value: Optional[pygame.rect.Rect]):
self.__rect = value

def add(self, *groups):
"""add the sprite to groups

Expand Down Expand Up @@ -1337,7 +1323,7 @@ def set_clip(self, screen_rect=None):
def get_clip(self):
"""get the area where drawing will occur

LayeredDirty.get_clip(): return Rect
LayeredDirty.get_clip(): return Rect or None

"""
return self._clip
Expand Down
Loading