From 3613ca45dd0acd121a13bdff794f33b34f5d62c3 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Tue, 11 May 2021 18:35:04 -0400 Subject: [PATCH] player can add items to inventory --- main.py | 1 + obj/gameManager.py | 45 +++++++++++++++++++++++++++++++++++++++------ obj/item.py | 1 + obj/tradeHub.py | 2 +- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 2ab6231..0351fd7 100644 --- a/main.py +++ b/main.py @@ -15,6 +15,7 @@ def titleScreen(): def mainGame(): tools.clearScreen() + #load items tradeableItems = [ obj.Item("flour", 0.5), obj.Item("Rice Cakes", 1), diff --git a/obj/gameManager.py b/obj/gameManager.py index 87c043c..51ee4bb 100644 --- a/obj/gameManager.py +++ b/obj/gameManager.py @@ -1,3 +1,4 @@ +from time import sleep from .player import Player import tools @@ -24,21 +25,53 @@ class GameManager: self.player.printInventory() print() + def getTradeHubItems(self): + return self.planets[ + self.player.getLocation()].getTradeHub().getItems() + def buyMenu(self): - print() + tools.clearScreen() + tradeHubItems = self.getTradeHubItems() while True: + tools.clearScreen() i=0 - for item in self.planets[self.player.getLocation()].getTradeHub().getItems().getInventory(): + for item in tradeHubItems: print(f"{i}, {item.getName()}") i+=1 userInput = input("Which Item would you like to buy? (e)xit \n>> ") if userInput == "e": break - #choose number - #ask for quantity - #Confirm - #add item with quantity to player inv + + try: + itemNumber = int(userInput) + except ValueError: + continue + + if itemNumber < i and itemNumber > -1: + print("You have choosen: " + tradeHubItems[itemNumber].getName()) + userInput = input("How many would you like?\n>> ") + try: + itemQuantity = int(userInput) + except ValueError: + continue + else: + continue + + if itemQuantity == 0: + print("NO ITEMS ADDED!") + break + + userInput = input(f"Are you sure you want {itemQuantity} {tradeHubItems[itemNumber].getName()}? (y/n)\n>> ").lower() + + if userInput == "y": + self.player.addItem(tradeHubItems[itemNumber], itemQuantity) + else: + print("NO ITEMS ADDED!") + sleep(1) + + break + def enterTradeHub(self): tools.clearScreen() diff --git a/obj/item.py b/obj/item.py index 7217417..7f1494d 100644 --- a/obj/item.py +++ b/obj/item.py @@ -8,3 +8,4 @@ class Item: return self.name def getPrice(self): return self.price + diff --git a/obj/tradeHub.py b/obj/tradeHub.py index c77835a..10cc432 100644 --- a/obj/tradeHub.py +++ b/obj/tradeHub.py @@ -13,7 +13,7 @@ class TradeHub: self.items.addItem(item, 100) def getItems(self): - return self.items + return self.items.getInventory() def printItems(self): print(f"{'Name' : <15}{'- - - - - | - - - - -' : ^10}{'Cost' : >6}") print()