From 1f254ad35dbf9dba3bb655da4d43e3aa21a48582 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Tue, 11 May 2021 10:34:56 -0400 Subject: [PATCH] Seperate classes/tools into files --- .gitignore | 1 + main.py | 121 ++++++--------------------------------------- obj/__init__.py | 5 ++ obj/gameManager.py | 39 +++++++++++++++ obj/item.py | 10 ++++ obj/planet.py | 13 +++++ obj/player.py | 15 ++++++ obj/tradeHub.py | 15 ++++++ tools/__init__.py | 2 + tools/text.py | 4 ++ 10 files changed, 119 insertions(+), 106 deletions(-) create mode 100644 .gitignore create mode 100644 obj/__init__.py create mode 100644 obj/gameManager.py create mode 100644 obj/item.py create mode 100644 obj/planet.py create mode 100644 obj/player.py create mode 100644 obj/tradeHub.py create mode 100644 tools/__init__.py create mode 100644 tools/text.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8ebf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ \ No newline at end of file diff --git a/main.py b/main.py index 7a09c83..41e5bee 100644 --- a/main.py +++ b/main.py @@ -1,100 +1,9 @@ -import os - -PLAYER_INIT_LOCATION = "Luna" - -class Player: - - def __init__(self, name, location): - self.name = name - self.location = location - - def getName(self): - return self.name - - def setName(self, name): - self.name = name - - def getLocation(self): - return self.location - -class GameManager: - 'Manage the game' - - def __init__(self, userInput, planets): - self.gameIsRunning = True - self.player = Player(userInput, PLAYER_INIT_LOCATION) - self.planets = planets - - def isGameRunning(self): - return self.gameIsRunning - - def stopGame(self): - self.gameIsRunning = False - - def printPlayerStats(self): - clearScreen() - print("Name: ", self.player.getName()) - print("Location: ", self.player.getLocation()) - print() - - def enterTradeHub(self): - clearScreen() - while True: - userInput = input("(e)xit\n\n(l)ist items\n>> ").lower() - - if userInput == 'e': - clearScreen() - break; - - elif userInput == 'l': - clearScreen() - self.planets[self.player.getLocation()].getTradeHub().printItems() - else: - clearScreen() - -class Planet: - 'Contains trade hub' - def __init__(self, name): - self.name = name - def addTradeHub(self, tradeHub): - self.tradeHub = tradeHub - def getTradeHub(self): - return self.tradeHub - def getName(self): - return self.name - -class TradeHub: - 'Contains items to sell' - def __init__(self, name): - self.name = name - def addItems(self, items): - self.items = items - def getItems(self): - return self.items - def printItems(self): - print(f"{'Name' : <15}{'- - - - - | - - - - -' : ^10}{'Cost' : >6}") - print() - for item in self.items.values(): - print(f"{item.getName() : <15}{'* * * * * | * * * * *' : ^10}{str(item.getPrice()) : >5}") - print() - -class Item: - 'items to buy/sell' - def __init__(self, name, price): - self.name = name - self.price = price - - def getName(self): - return self.name - def getPrice(self): - return self.price - -def clearScreen(): - os.system('clear') +import obj +import tools def titleScreen(): while(True): - clearScreen() + tools.clearScreen() print("Space Game Title To Be Disclosed!!!") userInput = input("(n)ew game\n(q)uit:\n>> ").lower() @@ -105,17 +14,17 @@ def titleScreen(): mainGame() def mainGame(): - clearScreen() + tools.clearScreen() tradeableItems = { - "flour" : Item("flour", 0.5), - "Rice Cakes" : Item("Rice Cakes", 1), - "Water" : Item("Water", 25) + "flour" : obj.Item("flour", 0.5), + "Rice Cakes" : obj.Item("Rice Cakes", 1), + "Water" : obj.Item("Water", 25) } #Create TradeHubs tradeHubs = { - "The Jackel" : TradeHub("The Jackel"), - "Star Crapper" : TradeHub("Star Crapper") + "The Jackel" : obj.TradeHub("The Jackel"), + "Star Crapper" : obj.TradeHub("Star Crapper") } tradeHubs["The Jackel"].addItems(tradeableItems) @@ -127,8 +36,8 @@ def mainGame(): # Create Planets planets = { - "BoB" : Planet("BoB"), - "Luna" : Planet("Luna") + "BoB" : obj.Planet("BoB"), + "Luna" : obj.Planet("Luna") } planets["BoB"].addTradeHub(tradeHubs["Star Crapper"]) planets["Luna"].addTradeHub(tradeHubs["The Jackel"]) @@ -143,9 +52,9 @@ def mainGame(): userInput = "(none)" # Start Game - Game = GameManager(userInput, planets) + Game = obj.GameManager(userInput, planets) - clearScreen() + tools.clearScreen() while Game.isGameRunning(): userInput = "" @@ -158,11 +67,11 @@ def mainGame(): Game.printPlayerStats() elif userInput == "c": - clearScreen() + tools.clearScreen() elif userInput == "e": Game.enterTradeHub() else: - clearScreen() + tools.clearScreen() def main(): titleScreen() diff --git a/obj/__init__.py b/obj/__init__.py new file mode 100644 index 0000000..96237cd --- /dev/null +++ b/obj/__init__.py @@ -0,0 +1,5 @@ +from .tradeHub import TradeHub +from .planet import Planet +from .item import Item +from .gameManager import GameManager +#from .player import Player diff --git a/obj/gameManager.py b/obj/gameManager.py new file mode 100644 index 0000000..56fdb6e --- /dev/null +++ b/obj/gameManager.py @@ -0,0 +1,39 @@ +from .player import Player +import tools + + +PLAYER_INIT_LOCATION = "Luna" +class GameManager: + 'Manage the game' + + def __init__(self, userInput, planets): + self.gameIsRunning = True + self.player = Player(userInput, PLAYER_INIT_LOCATION) + self.planets = planets + + def isGameRunning(self): + return self.gameIsRunning + + def stopGame(self): + self.gameIsRunning = False + + def printPlayerStats(self): + tools.clearScreen() + print("Name: ", self.player.getName()) + print("Location: ", self.player.getLocation()) + print() + + def enterTradeHub(self): + tools.clearScreen() + while True: + userInput = input("(e)xit\n\n(l)ist items\n>> ").lower() + + if userInput == 'e': + tools.clearScreen() + break; + + elif userInput == 'l': + tools.clearScreen() + self.planets[self.player.getLocation()].getTradeHub().printItems() + else: + tools.clearScreen() diff --git a/obj/item.py b/obj/item.py new file mode 100644 index 0000000..7217417 --- /dev/null +++ b/obj/item.py @@ -0,0 +1,10 @@ +class Item: + 'items to buy/sell' + def __init__(self, name, price): + self.name = name + self.price = price + + def getName(self): + return self.name + def getPrice(self): + return self.price diff --git a/obj/planet.py b/obj/planet.py new file mode 100644 index 0000000..4cb3d95 --- /dev/null +++ b/obj/planet.py @@ -0,0 +1,13 @@ +from .tradeHub import TradeHub + +class Planet: + 'Contains trade hub' + def __init__(self, name): + self.name = name + self.tradeHub = TradeHub + def addTradeHub(self, tradeHub): + self.tradeHub = tradeHub + def getTradeHub(self): + return self.tradeHub + def getName(self): + return self.name diff --git a/obj/player.py b/obj/player.py new file mode 100644 index 0000000..da497f9 --- /dev/null +++ b/obj/player.py @@ -0,0 +1,15 @@ +class Player: + + def __init__(self, name, location): + self.name = name + self.location = location + + def getName(self): + return self.name + + def setName(self, name): + self.name = name + + def getLocation(self): + return self.location + diff --git a/obj/tradeHub.py b/obj/tradeHub.py new file mode 100644 index 0000000..4cfed63 --- /dev/null +++ b/obj/tradeHub.py @@ -0,0 +1,15 @@ +class TradeHub: + 'Contains items to sell' + def __init__(self, name): + self.name = name + self.items = {} + def addItems(self, items): + self.items = items + def getItems(self): + return self.items + def printItems(self): + print(f"{'Name' : <15}{'- - - - - | - - - - -' : ^10}{'Cost' : >6}") + print() + for item in self.items.values(): + print(f"{item.getName() : <15}{'* * * * * | * * * * *' : ^10}{str(item.getPrice()) : >5}") + print() diff --git a/tools/__init__.py b/tools/__init__.py new file mode 100644 index 0000000..93ad52c --- /dev/null +++ b/tools/__init__.py @@ -0,0 +1,2 @@ +from .text import clearScreen + diff --git a/tools/text.py b/tools/text.py new file mode 100644 index 0000000..0e5e770 --- /dev/null +++ b/tools/text.py @@ -0,0 +1,4 @@ +import os + +def clearScreen(): + os.system('clear')