From d978d319bcc16ba14f61ac9bd2090b6bb39a0366 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sat, 16 Jan 2021 00:40:32 +0100 Subject: [PATCH] Entities are living during two ticks, fixes #80 --- squirrelbattle/entities/items.py | 9 +++++++-- squirrelbattle/tests/entities_test.py | 3 ++- squirrelbattle/tests/game_test.py | 1 + 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/squirrelbattle/entities/items.py b/squirrelbattle/entities/items.py index a80f675..023c1a4 100644 --- a/squirrelbattle/entities/items.py +++ b/squirrelbattle/entities/items.py @@ -232,14 +232,19 @@ class Explosion(Item): """ When a bomb explodes, the explosion is displayed. """ - def __init__(self, *args, **kwargs): + living_ticks: int + + def __init__(self, living_ticks: int = 2, *args, **kwargs): super().__init__(name="explosion", *args, **kwargs) + self.living_ticks = living_ticks def act(self, m: Map) -> None: """ The bomb disappears after exploding. """ - m.remove_entity(self) + self.living_ticks -= 1 + if self.living_ticks <= 0: + m.remove_entity(self) def hold(self, player: InventoryHolder) -> None: """ diff --git a/squirrelbattle/tests/entities_test.py b/squirrelbattle/tests/entities_test.py index 9d50f58..6f059f2 100644 --- a/squirrelbattle/tests/entities_test.py +++ b/squirrelbattle/tests/entities_test.py @@ -209,7 +209,8 @@ class TestEntities(unittest.TestCase): self.assertNotIn(explosion, self.player.inventory) self.assertIsNone(explosion.held_by) - # The explosion disappears after one tick + # The explosion disappears after two ticks + explosion.act(self.map) explosion.act(self.map) self.assertNotIn(explosion, self.map.entities) diff --git a/squirrelbattle/tests/game_test.py b/squirrelbattle/tests/game_test.py index e5573ec..786de35 100644 --- a/squirrelbattle/tests/game_test.py +++ b/squirrelbattle/tests/game_test.py @@ -255,6 +255,7 @@ class TestGame(unittest.TestCase): self.game.map.add_entity(explosion) self.assertIn(explosion, self.game.map.entities) self.game.handle_key_pressed(KeyValues.WAIT) + self.game.handle_key_pressed(KeyValues.WAIT) self.assertNotIn(explosion, self.game.map.entities) rabbit = Rabbit()