We can now buy items to merchants, closes #47

This commit is contained in:
Yohann D'ANELLO 2020-12-18 01:05:50 +01:00
parent a5890a341d
commit 9a556ba669
5 changed files with 27 additions and 16 deletions

View File

@ -34,6 +34,7 @@ class Game:
""" """
self.state = GameMode.MAINMENU self.state = GameMode.MAINMENU
self.waiting_for_friendly_key = False self.waiting_for_friendly_key = False
self.is_in_store_menu = True
self.settings = Settings() self.settings = Settings()
self.settings.load_settings() self.settings.load_settings()
self.settings.write_settings() self.settings.write_settings()
@ -191,22 +192,31 @@ class Game:
""" """
In a store menu, we can buy items or close the menu. 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 self.state = GameMode.PLAY
elif key == KeyValues.UP: elif key == KeyValues.UP:
self.store_menu.go_up() menu.go_up()
elif key == KeyValues.DOWN: elif key == KeyValues.DOWN:
self.store_menu.go_down() menu.go_down()
if self.store_menu.values and not self.player.dead: 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: if key == KeyValues.ENTER:
item = self.store_menu.validate() item = menu.validate()
flag = item.be_sold(self.player, self.store_menu.merchant) 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: 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) self.display_actions(DisplayActions.UPDATE)
# Ensure that the cursor has a good position # Ensure that the cursor has a good position
self.store_menu.position = min(self.store_menu.position, menu.position = min(menu.position, len(menu.values) - 1)
len(self.store_menu.values) - 1)
def handle_key_pressed_main_menu(self, key: KeyValues) -> None: def handle_key_pressed_main_menu(self, key: KeyValues) -> None:
""" """

View File

@ -59,8 +59,8 @@ msgid "{player} exchanged its body with {entity}."
msgstr "{player} täuscht seinem Körper mit {entity} aus." msgstr "{player} täuscht seinem Körper mit {entity} aus."
#: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573 #: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573
msgid "You do not have enough money" msgid "The buyer does not have enough money"
msgstr "Sie haben nicht genug Geld" msgstr "Der Kaufer hat nicht genug Geld"
#: squirrelbattle/game.py:249 #: squirrelbattle/game.py:249
msgid "" msgid ""

View File

@ -58,8 +58,8 @@ msgid "{player} exchanged its body with {entity}."
msgstr "{player} intercambió su cuerpo con {entity}." msgstr "{player} intercambió su cuerpo con {entity}."
#: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573 #: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573
msgid "You do not have enough money" msgid "The buyer does not have enough money"
msgstr "No tienes suficiente dinero" msgstr "El comprador no tiene suficiente dinero"
#: squirrelbattle/game.py:249 #: squirrelbattle/game.py:249
msgid "" msgid ""

View File

@ -59,8 +59,8 @@ msgid "{player} exchanged its body with {entity}."
msgstr "{player} a échangé son corps avec {entity}." msgstr "{player} a échangé son corps avec {entity}."
#: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573 #: squirrelbattle/game.py:205 squirrelbattle/tests/game_test.py:573
msgid "You do not have enough money" msgid "The buyer does not have enough money"
msgstr "Vous n'avez pas assez d'argent" msgstr "L'acheteur n'a pas assez d'argent"
#: squirrelbattle/game.py:249 #: squirrelbattle/game.py:249
msgid "" msgid ""

View File

@ -579,7 +579,8 @@ class TestGame(unittest.TestCase):
self.game.handle_key_pressed(KeyValues.ENTER) self.game.handle_key_pressed(KeyValues.ENTER)
self.assertNotIn(item, self.game.player.inventory) self.assertNotIn(item, self.game.player.inventory)
self.assertIn(item, merchant.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) self.game.handle_key_pressed(KeyValues.ENTER)
# Exit the menu # Exit the menu