-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathshop.py
106 lines (86 loc) · 3.92 KB
/
shop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import keyboard
import os
from style import get_graphics, set_cursor, set_cursor_str, COLORS, graphics as g
class FishInventory():
def __init__(self):
self.caughtfish = {"Carp": 1, "Bass": 0, "Salmon": 0}
def getinventory(self):
return self.caughtfish
def addfish(self, fish):
self.caughtfish[fish] += 1
return self.caughtfish
def removefish(self, fish):
self.caughtfish[fish] -= 1
return self.caughtfish
# need function for viewing inventory
class Shop():
# TODO : Shop needs to reference the players own inventory
def __init__(self, inventory): #pass in an inventory object that shop can access
self.inventory = inventory
self.fishprices = {"Carp": 5, "Bass": 8, "Salmon": 12}
self.__pictures = []
def display_shop(self, selected_index):
"""
Display the shop interface with the current selection highlighted.
Only called once at the start of the shop interface.
"""
print(g.get('shop'))
retval = ""
y = 6
retval += set_cursor_str(33, 3) + "=== Welcome to the Shop ==="
retval += set_cursor_str(33, 4) + "Use W/S to navigate and Enter to select."
for i, price in enumerate(self.fishprices.keys()):
if i == selected_index:
retval += set_cursor_str(43, y) + f"> {price}: ${self.fishprices[price]}"
else:
retval += set_cursor_str(43, y) + f" {price}: ${self.fishprices[price]}"
y += 1
y += 1
retval += set_cursor_str(45, y) + "Your inventory: "
for fish in testfishinventory.caughtfish.keys():
if testfishinventory.caughtfish[fish] > 0:
retval += set_cursor_str(45, y+1) + f"{fish} x{testfishinventory.caughtfish[fish]} "
print(retval)
def sellfish(self, fish):
if self.inventory.getinventory()[fish] > 0:
self.inventory.removefish(fish)
print(f"Sold {fish} for ${self.fishprices[fish]}")
else:
print(f"No {fish} to sell")
# TODO: update shop screen in response to input
def shop_interface(self):
selected_index = 0
shopping = True
self.display_shop(selected_index)
while shopping:
key = keyboard.read_event()
if key.event_type == "down":
if key.name == "w": # Move up
selected_index = (selected_index - 1) % len(self.fishprices)
elif key.name == "up":
selected_index = (selected_index - 1) % len(self.fishprices)
elif key.name == "s": # Move down
selected_index = (selected_index + 1) % len(self.fishprices)
elif key.name == "down":
selected_index = (selected_index + 1) % len(self.fishprices)
elif key.name == "enter": # Select item
os.system('cls' if os.name == 'nt' else 'clear')
selected_fish = self.fishprices[list(self.fishprices.keys())[selected_index]]
keyboard.read_event()
elif key.name == "q": # Quit shop
shopping = False
y = 6
for i, price in enumerate(self.fishprices.keys()):
if i == selected_index:
print(set_cursor_str(43, y) + f"> {price}: ${self.fishprices[price]}")
else:
print(set_cursor_str(43, y) + f" {price}: ${self.fishprices[price]}")
y += 1
os.system('cls' if os.name == 'nt' else 'clear')
testfishinventory = FishInventory()
def main():
os.system('cls' if os.name == 'nt' else 'clear')
shop = Shop([testfishinventory])
shop.shop_interface()
if __name__ == '__main__':
main()