Skip to content

Commit

Permalink
Allow iteration on Inventory object
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjdai1 committed Oct 13, 2023
1 parent 588bbdd commit 5a09f0a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ def start(self) -> None:
sword.destroy()
print(game.player.inventory, "\n")

for item in game.player.inventory:
print(itemk)

#coins.count = -42

#save(game, "game")
Expand Down
10 changes: 9 additions & 1 deletion src/inventory/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ def _on_item_destroyed(self, item) -> None:
"""Removes inputted item from the inventory"""
self._items.remove(item)

def __iter__(self):
self.iter_data = self._items[:]
return self

def __next__(self):
if not self.iter_data:
raise StopIteration
return self.iter_data.pop()

def clear(self) -> None:
"""Clears the inventory"""
self._items = []

0 comments on commit 5a09f0a

Please sign in to comment.