Skip to content
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

Add __len__ and __delitem__ to Scene #1721

Merged
merged 2 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
23 changes: 20 additions & 3 deletions arcade/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
helper function to create a Scene directly from a TileMap object.
"""

from typing import Dict, List, Optional
from typing import Dict, List, Optional, Union

from arcade import Sprite, SpriteList
from arcade.types import Color, RGBA255
Expand All @@ -29,6 +29,24 @@ def __init__(self) -> None:
self._sprite_lists: List[SpriteList] = []
self._name_mapping: Dict[str, SpriteList] = {}

def __len__(self) -> int:
"""
Get length of `_sprite_lists`.
"""
return len(self._sprite_lists)

def __delitem__(self, sprite_list: Union[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.
"""
if isinstance(sprite_list, str):
self.remove_sprite_list_by_name(sprite_list)
Cleptomania marked this conversation as resolved.
Show resolved Hide resolved
else:
self.remove_sprite_list_by_object(sprite_list)

@classmethod
def from_tilemap(cls, tilemap: TileMap) -> "Scene":
"""
Expand Down Expand Up @@ -188,7 +206,7 @@ def add_sprite_list_after(

:param str name: The name to give the SpriteList.
:param str after: The name of the SpriteList to place this one after.
:param bool use_spatial_hash: Wether or not to use spatial hash if creating a new SpriteList.
:param bool use_spatial_hash: Whether or not to use spatial hash if creating a new SpriteList.
:param SpriteList sprite_list: The SpriteList to add, optional.
"""
if sprite_list is None:
Expand Down Expand Up @@ -355,4 +373,3 @@ def draw_hit_boxes(

for sprite_list in self._sprite_lists:
sprite_list.draw_hit_boxes(color, line_thickness)

28 changes: 28 additions & 0 deletions tests/unit/scene/test_scene_len_delitem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import arcade
import pytest


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

assert len(scene) == 1

scene.add_sprite_list("Walls")
scene.add_sprite_list("Coins")

assert len(scene) == 3

def test_delitem():
scene = arcade.Scene()
scene.add_sprite_list("Walls")
scene.add_sprite_list("Player")
del scene["Player"]

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

del scene[scene._sprite_lists[0]]

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