Skip to content

Improve scene.py #1748

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 10 commits into from
May 18, 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
8 changes: 8 additions & 0 deletions arcade/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,11 @@ def draw_hit_boxes(

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

def __bool__(self) -> bool:
"""Returns whether or not `_sprite_lists` contains anything"""
return bool(self._sprite_lists)

def __contains__(self, item: Union[str, SpriteList]) -> bool:
"""True when `item` is in `_sprite_lists` or is a value in `_name_mapping`"""
return item in self._sprite_lists or item in self._name_mapping
67 changes: 67 additions & 0 deletions tests/unit/scene/test_scene_dunder_methods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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")
scene.add_sprite_list("Coins")
del scene["Player"]

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

del scene[scene._sprite_lists[0]]

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

del scene[0]

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


def test_bool():
scene = arcade.Scene()
assert not scene

scene.add_sprite_list("Walls")
assert scene


def test_contains():
scene = arcade.Scene()
assert "Walls" not in scene
assert None not in scene

walls_spriteList = arcade.SpriteList()
scene.add_sprite_list("Walls", use_spatial_hash=True, sprite_list=walls_spriteList)
assert "Walls" in scene

test_sprite = arcade.Sprite()
walls_spriteList.append(test_sprite)
assert walls_spriteList in scene

coins_spriteList = arcade.SpriteList()
scene.add_sprite_list("Coins", use_spatial_hash=True, sprite_list=coins_spriteList)
assert "Coins" in scene

test_sprite = arcade.Sprite()
coins_spriteList.append(test_sprite)
assert coins_spriteList in scene

assert None not in scene
34 changes: 0 additions & 34 deletions tests/unit/scene/test_scene_len_delitem.py

This file was deleted.