From 8608ce346fdcac66dda919937ef5b37aba13d458 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sat, 12 Dec 2020 16:50:01 +0100 Subject: [PATCH] Add a small bomb exploding animation, fixes #31 --- squirrelbattle/display/texturepack.py | 2 ++ squirrelbattle/entities/items.py | 24 ++++++++++++++++++++++++ squirrelbattle/tests/entities_test.py | 17 ++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/squirrelbattle/display/texturepack.py b/squirrelbattle/display/texturepack.py index a6ec3af..f72cd97 100644 --- a/squirrelbattle/display/texturepack.py +++ b/squirrelbattle/display/texturepack.py @@ -62,6 +62,7 @@ TexturePack.ASCII_PACK = TexturePack( BODY_SNATCH_POTION='S', BOMB='o', EMPTY=' ', + EXPLOSION='%', FLOOR='.', HAZELNUT='ยค', HEART='โค', @@ -87,6 +88,7 @@ TexturePack.SQUIRREL_PACK = TexturePack( BODY_SNATCH_POTION='๐Ÿ”€', BOMB='๐Ÿ’ฃ', EMPTY=' ', + EXPLOSION='๐Ÿ’ฅ', FLOOR='โ–ˆโ–ˆ', HAZELNUT='๐ŸŒฐ', HEART='๐Ÿ’œ', diff --git a/squirrelbattle/entities/items.py b/squirrelbattle/entities/items.py index b115f8d..865a703 100644 --- a/squirrelbattle/entities/items.py +++ b/squirrelbattle/entities/items.py @@ -158,6 +158,10 @@ class Bomb(Item): m.logs.add_message(log_message) m.entities.remove(self) + # Add sparkles where the bomb exploded. + explosion = Explosion(y=self.y, x=self.x) + self.map.add_entity(explosion) + def save_state(self) -> dict: """ Saves the state of the bomb into a dictionary @@ -168,6 +172,26 @@ class Bomb(Item): return d +class Explosion(Item): + """ + When a bomb explodes, the explosion is displayed. + """ + def __init__(self, *args, **kwargs): + super().__init__(name="explosion", *args, **kwargs) + + def act(self, m: Map) -> None: + """ + The explosion instant dies. + """ + m.remove_entity(self) + + def hold(self, player: InventoryHolder) -> None: + """ + The player can't hold any explosion. + """ + pass + + class Weapon(Item): """ Non-throwable items that improve player damage diff --git a/squirrelbattle/tests/entities_test.py b/squirrelbattle/tests/entities_test.py index 2c72abd..70e3748 100644 --- a/squirrelbattle/tests/entities_test.py +++ b/squirrelbattle/tests/entities_test.py @@ -3,7 +3,8 @@ import unittest -from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart, Item +from squirrelbattle.entities.items import BodySnatchPotion, Bomb, Heart, Item, \ + Explosion from squirrelbattle.entities.monsters import Tiger, Hedgehog, Rabbit, TeddyBear from squirrelbattle.entities.player import Player from squirrelbattle.interfaces import Entity, Map @@ -138,6 +139,20 @@ class TestEntities(unittest.TestCase): self.assertTrue(teddy_bear.dead) bomb_state = item.save_state() self.assertEqual(bomb_state["damage"], item.damage) + explosions = self.map.find_entities(Explosion) + self.assertTrue(explosions) + explosion = explosions[0] + self.assertEqual(explosion.y, item.y) + self.assertEqual(explosion.x, item.x) + + # The player can't hold the explosion + explosion.hold(self.player) + self.assertNotIn(explosion, self.player.inventory) + self.assertFalse(explosion.held) + + # The explosion disappears after one tick + explosion.act(self.map) + self.assertNotIn(explosion, self.map.entities) def test_hearts(self) -> None: """