From 9a556ba669d24027c2ebd381efbbbfd3e3370388 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 18 Dec 2020 01:05:50 +0100 Subject: [PATCH] We can now buy items to merchants, closes #47 --- squirrelbattle/game.py | 28 +++++++++++++------ .../locale/de/LC_MESSAGES/squirrelbattle.po | 4 +-- .../locale/es/LC_MESSAGES/squirrelbattle.po | 4 +-- .../locale/fr/LC_MESSAGES/squirrelbattle.po | 4 +-- squirrelbattle/tests/game_test.py | 3 +- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/squirrelbattle/game.py b/squirrelbattle/game.py index ed3b60f..5fc130b 100644 --- a/squirrelbattle/game.py +++ b/squirrelbattle/game.py @@ -34,6 +34,7 @@ class Game: """ self.state = GameMode.MAINMENU self.waiting_for_friendly_key = False + self.is_in_store_menu = True self.settings = Settings() self.settings.load_settings() self.settings.write_settings() @@ -191,22 +192,31 @@ class Game: """ In a store menu, we can buy items or close the menu. """ - if key == KeyValues.SPACE: + menu = self.store_menu if self.is_in_store_menu else self.inventory_menu + + if key == KeyValues.SPACE or key == KeyValues.INVENTORY: self.state = GameMode.PLAY elif key == KeyValues.UP: - self.store_menu.go_up() + menu.go_up() elif key == KeyValues.DOWN: - self.store_menu.go_down() - if self.store_menu.values and not self.player.dead: + menu.go_down() + elif key == KeyValues.LEFT: + self.is_in_store_menu = False + elif key == KeyValues.RIGHT: + self.is_in_store_menu = True + if menu.values and not self.player.dead: if key == KeyValues.ENTER: - item = self.store_menu.validate() - flag = item.be_sold(self.player, self.store_menu.merchant) + item = menu.validate() + owner = self.store_menu.merchant if self.is_in_store_menu \ + else self.player + buyer = self.player if self.is_in_store_menu \ + else self.store_menu.merchant + flag = item.be_sold(buyer, owner) if not flag: - self.message = _("You do not have enough money") + self.message = _("The buyer does not have enough money") self.display_actions(DisplayActions.UPDATE) # Ensure that the cursor has a good position - self.store_menu.position = min(self.store_menu.position, - len(self.store_menu.values) - 1) + menu.position = min(menu.position, len(menu.values) - 1) def handle_key_pressed_main_menu(self, key: KeyValues) -> None: """ diff --git a/squirrelbattle/locale/de/LC_MESSAGES/squirrelbattle.po b/squirrelbattle/locale/de/LC_MESSAGES/squirrelbattle.po index 39cfeec..29bf10e 100644 --- a/squirrelbattle/locale/de/LC_MESSAGES/squirrelbattle.po +++ b/squirrelbattle/locale/de/LC_MESSAGES/squirrelbattle.po @@ -59,8 +59,8 @@ msgid "{player} exchanged its body with {entity}." msgstr "{player} täuscht seinem Körper mit {entity} aus." #: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573 -msgid "You do not have enough money" -msgstr "Sie haben nicht genug Geld" +msgid "The buyer does not have enough money" +msgstr "Der Kaufer hat nicht genug Geld" #: squirrelbattle/game.py:249 msgid "" diff --git a/squirrelbattle/locale/es/LC_MESSAGES/squirrelbattle.po b/squirrelbattle/locale/es/LC_MESSAGES/squirrelbattle.po index f4d1c3c..8a7bc3e 100644 --- a/squirrelbattle/locale/es/LC_MESSAGES/squirrelbattle.po +++ b/squirrelbattle/locale/es/LC_MESSAGES/squirrelbattle.po @@ -58,8 +58,8 @@ msgid "{player} exchanged its body with {entity}." msgstr "{player} intercambió su cuerpo con {entity}." #: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573 -msgid "You do not have enough money" -msgstr "No tienes suficiente dinero" +msgid "The buyer does not have enough money" +msgstr "El comprador no tiene suficiente dinero" #: squirrelbattle/game.py:249 msgid "" diff --git a/squirrelbattle/locale/fr/LC_MESSAGES/squirrelbattle.po b/squirrelbattle/locale/fr/LC_MESSAGES/squirrelbattle.po index 8b1c4db..ffbfdce 100644 --- a/squirrelbattle/locale/fr/LC_MESSAGES/squirrelbattle.po +++ b/squirrelbattle/locale/fr/LC_MESSAGES/squirrelbattle.po @@ -59,8 +59,8 @@ msgid "{player} exchanged its body with {entity}." msgstr "{player} a échangé son corps avec {entity}." #: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573 -msgid "You do not have enough money" -msgstr "Vous n'avez pas assez d'argent" +msgid "The buyer does not have enough money" +msgstr "L'acheteur n'a pas assez d'argent" #: squirrelbattle/game.py:249 msgid "" diff --git a/squirrelbattle/tests/game_test.py b/squirrelbattle/tests/game_test.py index 52aeeaf..c70ca8f 100644 --- a/squirrelbattle/tests/game_test.py +++ b/squirrelbattle/tests/game_test.py @@ -579,7 +579,8 @@ class TestGame(unittest.TestCase): self.game.handle_key_pressed(KeyValues.ENTER) self.assertNotIn(item, self.game.player.inventory) self.assertIn(item, merchant.inventory) - self.assertEqual(self.game.message, _("You do not have enough money")) + self.assertEqual(self.game.message, + _("The buyer does not have enough money")) self.game.handle_key_pressed(KeyValues.ENTER) # Exit the menu