Skip to content

update Scene to return SpriteList #2689

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

Merged
merged 1 commit into from
May 20, 2025
Merged
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
25 changes: 18 additions & 7 deletions arcade/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

from collections.abc import Iterable
from typing import TypeVar
from warnings import warn

from arcade import Sprite, SpriteList
Expand All @@ -20,6 +21,8 @@

__all__ = ["Scene", "SceneKeyError"]

_S = TypeVar("_S", bound=Sprite)


class SceneKeyError(KeyError):
"""
Expand Down Expand Up @@ -151,7 +154,7 @@ def __getitem__(self, key: str) -> SpriteList:

raise SceneKeyError(key)

def add_sprite(self, name: str, sprite: Sprite) -> None:
def add_sprite(self, name: str, sprite: _S) -> _S:
"""
Add a Sprite to the SpriteList with the specified name.

Expand All @@ -177,12 +180,14 @@ def add_sprite(self, name: str, sprite: Sprite) -> None:
new_list.append(sprite)
self.add_sprite_list(name=name, sprite_list=new_list)

return sprite

def add_sprite_list(
self,
name: str,
use_spatial_hash: bool = False,
sprite_list: SpriteList | None = None,
) -> None:
) -> SpriteList:
"""
Add a SpriteList to the scene with the specified name.

Expand All @@ -207,14 +212,15 @@ def add_sprite_list(
)
self._name_mapping[name] = sprite_list
self._sprite_lists.append(sprite_list)
return sprite_list

def add_sprite_list_before(
self,
name: str,
before: str,
use_spatial_hash: bool = False,
sprite_list: SpriteList | None = None,
) -> None:
) -> SpriteList:
"""
Add a sprite list to the scene with the specified name before another SpriteList.

Expand Down Expand Up @@ -244,6 +250,7 @@ def add_sprite_list_before(
before_list = self._name_mapping[before]
index = self._sprite_lists.index(before_list)
self._sprite_lists.insert(index, sprite_list)
return sprite_list

def move_sprite_list_before(
self,
Expand Down Expand Up @@ -279,7 +286,7 @@ def add_sprite_list_after(
after: str,
use_spatial_hash: bool = False,
sprite_list: SpriteList | None = None,
) -> None:
) -> SpriteList:
"""
Add a SpriteList to the scene with the specified name after a specific SpriteList.

Expand Down Expand Up @@ -309,6 +316,7 @@ def add_sprite_list_after(
after_list = self._name_mapping[after]
index = self._sprite_lists.index(after_list) + 1
self._sprite_lists.insert(index, sprite_list)
return sprite_list

def move_sprite_list_after(
self,
Expand Down Expand Up @@ -338,19 +346,21 @@ def move_sprite_list_after(
old_index = self._sprite_lists.index(name_list)
self._sprite_lists.insert(new_index, self._sprite_lists.pop(old_index))

def remove_sprite_list_by_index(self, index: int) -> None:
def remove_sprite_list_by_index(self, index: int) -> SpriteList:
"""
Remove a layer from the scene by its index in the draw order.

Args:
index: The index of the sprite list to remove.
"""
self.remove_sprite_list_by_object(self._sprite_lists[index])
sprite_list = self._sprite_lists[index]
self.remove_sprite_list_by_object(sprite_list)
return sprite_list

def remove_sprite_list_by_name(
self,
name: str,
) -> None:
) -> SpriteList:
"""
Remove a layer from the scene by its name.

Expand All @@ -363,6 +373,7 @@ def remove_sprite_list_by_name(
sprite_list = self._name_mapping[name]
self._sprite_lists.remove(sprite_list)
del self._name_mapping[name]
return sprite_list

def remove_sprite_list_by_object(self, sprite_list: SpriteList) -> None:
"""
Expand Down
Loading