Skip to content

Add remove_sprite_list_by_index to Scene #1723

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 4 commits into from
Apr 24, 2023
Merged
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
33 changes: 28 additions & 5 deletions arcade/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ def __len__(self) -> int:
"""
return len(self._sprite_lists)

def __delitem__(self, sprite_list: Union[str, SpriteList]) -> None:
def __delitem__(self, sprite_list: Union[int, str, SpriteList]) -> None:
"""
Remove the SpriteList from `_sprite_lists` and `_name_mapping`.

:param Union[str, SpriteList] sprite_list: which SpriteList to delete - can
be either the name of the SpriteList or the SpriteList object.
:param Union[int, str, SpriteList] sprite_list: which SpriteList to delete - can
be the index of the SpriteList in `_sprite_lists`, the name of the SpriteList or
the SpriteList object.
"""
if isinstance(sprite_list, str):
if isinstance(sprite_list, int):
self.remove_sprite_list_by_index(sprite_list)
elif isinstance(sprite_list, str):
self.remove_sprite_list_by_name(sprite_list)
else:
self.remove_sprite_list_by_object(sprite_list)
Expand Down Expand Up @@ -244,12 +247,25 @@ 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:
"""
Remove a SpriteList by its index.

This function serves to completely remove the SpriteList from the Scene.

:param int index: The index of the SpriteList to remove.
"""
self.remove_sprite_list_by_object(self._sprite_lists[index])

def remove_sprite_list_by_name(
self,
name: str,
) -> None:
"""
Remove a SpriteList by it's name.
Remove a SpriteList by its name.

This function serves to completely remove the SpriteList from the Scene.

Expand All @@ -260,6 +276,13 @@ def remove_sprite_list_by_name(
del self._name_mapping[name]

def remove_sprite_list_by_object(self, sprite_list: SpriteList) -> None:
"""
Remove a SpriteList by object.

This function serves to completely remove the SpriteList from the Scene.

:param SpriteList sprite_list: The SpriteList to remove.
"""
self._sprite_lists.remove(sprite_list)
self._name_mapping = {
key: val for key, val in self._name_mapping.items() if val != sprite_list
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/scene/test_scene_len_delitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_delitem():
scene = arcade.Scene()
scene.add_sprite_list("Walls")
scene.add_sprite_list("Player")
scene.add_sprite_list("Coins")
del scene["Player"]

with pytest.raises(KeyError):
Expand All @@ -26,3 +27,8 @@ def test_delitem():

with pytest.raises(KeyError):
scene["Walls"]

del scene[0]

with pytest.raises(KeyError):
scene["Coins"]
29 changes: 29 additions & 0 deletions tests/unit/scene/test_scene_remove_sprite_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import arcade
import pytest

def test_remove_sprite_list_by_index():
scene = arcade.Scene()
scene.add_sprite_list("Player")

scene.remove_sprite_list_by_index(0)

with pytest.raises(KeyError):
scene["Player"]

def test_remove_sprite_list_by_name():
scene = arcade.Scene()
scene.add_sprite_list("Walls")

scene.remove_sprite_list_by_name("Walls")

with pytest.raises(KeyError):
scene["Walls"]

def test_remove_sprite_list_by_object():
scene = arcade.Scene()
scene.add_sprite_list("Coins")

scene.remove_sprite_list_by_object(scene.get_sprite_list("Coins"))

with pytest.raises(KeyError):
scene["Coins"]