From ca2ae15117ef3db12d78bc2e1fad25660fb11de0 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sat, 5 Dec 2020 13:42:33 +0100 Subject: [PATCH] Stack items in the inventory, closes #29 --- squirrelbattle/display/statsdisplay.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/squirrelbattle/display/statsdisplay.py b/squirrelbattle/display/statsdisplay.py index da9213f..ac1a89c 100644 --- a/squirrelbattle/display/statsdisplay.py +++ b/squirrelbattle/display/statsdisplay.py @@ -31,8 +31,19 @@ class StatsDisplay(Display): self.player.dexterity, self.player.constitution) self.addstr(self.pad, 3, 0, string3) - inventory_str = _("Inventory:") + " " + "".join( - self.pack[item.name.upper()] for item in self.player.inventory) + inventory_str = _("Inventory:") + " " + # Stack items by type instead of displaying each item + item_types = [item.name for item in self.player.inventory] + item_types.sort(key=item_types.count, reverse=True) + printed_items = [] + for item in item_types: + if item in printed_items: + continue + count = item_types.count(item) + inventory_str += self.pack[item.upper()] + if count > 1: + inventory_str += f"x{count} " + printed_items.append(item) self.addstr(self.pad, 8, 0, inventory_str) if self.player.dead: