This class is responsible for running the shop and creating the Item() objects.
Constructor:
def __init__(self):
self.items = []
f = open("items.csv")
fReader = csv.reader(f)
for line in fReader:
if line == []:
continue
self.items.append(Item(line[2], line[0], int(line[1]), int(line[3])))
f.close()
When the class is called the class list items are filled with Item() objects created by opening items.csv and taking the data in there to instantiate the items. Then we close the file.
WhoIsShopping():
def whoIsShopping(self, squad :list):
counter = 0
choices = []
print("Who is shopping?")
for member in squad:
if member.level > 0:
print(str(counter) + ": " + member.displayName)
choices.append(counter)
counter += 1
selection = inputAndCheck("Selection: ", choices)
return squad[selection]
This function does what it says, it prompts for and receives the selection of a playableCharacter() for shopping. It returns the specified playableCharacter().
ShopDisplay():
def shopDisplay(self, member :PlayableCharacter):
print("Stingy Man: welcome traveler, as long as you have gold your welcome! What are you looking for today?")
counter = 0
choices = []
for item in self.items:
print(str(counter) + ": " + item.name + "(" + str(item.cost) + " gold)")
choices.append(counter)
counter += 1
choices.append(counter)
print(str(counter) + ": Leave")
return inputAndCheck(member.displayName + "'s action: ", choices)
This function displays all the items available in the
store, by iterating over items.
Buy():
def buy(self, item :Item, gold, user :PlayableCharacter):
if item.cost <= gold:
if item.effect == "AtkPhy":
if user.energyName != "Mana":
return item
else:
return user.displayName + " can't use that(press enter to continue)"
elif item.effect == "AtkMag":
if user.energyName == "Mana":
return item
else:
return user.displayName + " can't use that(press enter to continue)"
else:
return item
else:
return "insufficient gold"
This function simply checks if the player has sufficient
gold, and ensures that a physical attack item isn’t bought for a magical
character, and a magical attack item isn’t bought by a physical attack character. (magical
characters have “mana” as there stamina)
StoreRun():
def storeRun(self, squad :list, gold :int):
done = False
while not done:
shopper = self.whoIsShopping(squad)
os.system("cls")
print("gold: " + str(gold))
action = self.shopDisplay(shopper)
if action >= len(self.items):
pass
else:
result = self.buy(self.items[action], gold, shopper)
if isinstance(result, Item):
input("Stingy man: here you go! thankyou for your business!(press enter to continue)")
shopper.inventory.append(result)
if result.usage != "hp":
shopper = result.effectHandler(shopper)
gold = gold - result.cost
else:
input(result)
result = inputAndCheck("are you done shopping?(0: no, 1: yes)", [0, 1])
counter = 0
for ally in squad:
if ally.displayName == shopper.displayName:
squad[counter] = shopper
counter += 1
if result == 1:
done = True
return [squad, gold]
This is the function that implements all of the previously mentioned functions. I loops through all the function calls so that the player can shop as many times as they want, until they decide to leave. At the end of each purchase, the player is asked if they want to leave and if they respond yes (1) then done is set to true, thus ending the loop.
Comments
Post a Comment