Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
16 changes: 5 additions & 11 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
26 changes: 6 additions & 20 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
Loading