-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
200 lines (188 loc) · 7.14 KB
/
user.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
Adam La Fleur 3/12/2020
"""
from .player import Direction
from .config import Config
from time import sleep
from string import capwords
import crayons
import readchar
class User:
def __init__(self, window: tuple, config: Config, debug: bool):
self.width = window[0]
self.height = window[1]
self.middle = self.height // 2
self.store = config.store
self.debug = debug
self.start = 0
# Function used to update the store based on config changes
def update_store(self, config, debug):
self.store = config.store
self.debug = debug
self.start = 0
# Function used to space out printouts
def __spacer(self, buffer: int):
for i in range(buffer):
if self.debug:
print("+ " + str(i))
else:
print()
# Function that gets the config string from config and prints it out fancy
def __print_config_menu(self, config: Config):
sp = config.print_config()
buffer = (self.height - len(sp)) // 2
self.__spacer(buffer)
for i in range(len(sp)):
if sp[i] == '^':
print()
else:
print(sp[i].center(self.width))
self.__spacer(buffer)
# Menu that takes keyboard input and determines which key is pressed to edit part of the config
def config_menu(self, config: Config):
self.__print_config_menu(config)
key = readchar.readkey().lower()
if key == 'p':
config.change_stats()
self.config_menu(config)
elif key == 's':
config.change_size()
config.change_style()
config.jewel_change()
self.config_menu(config)
elif key == 't':
config.change_tile()
self.config_menu(config)
elif key == 'd':
config.reset_config()
self.config_menu(config)
elif key == 'b':
config.change_bin()
self.config_menu(config)
elif key == 'r':
config.create_config()
else:
self.config_menu(config)
# Menu that takes keyboard input and gives the player the option to start game, edit config, or exit the game
def main_menu(self, config: Config):
self.__spacer(self.middle - 4)
print(crayons.green("The Game of Frupal!".center(self.width)))
print()
print()
print(crayons.yellow("(Press S) Start Game!".center(self.width)))
print()
print(crayons.yellow("(Press C) Configuration?".center(self.width)))
print()
print(crayons.yellow("(Press Q) Exit Game!".center(self.width)))
self.__spacer(self.middle - 4)
key = readchar.readkey()
if key == 's' or key == '\n':
return True
if key == 'c':
self.config_menu(config)
return self.main_menu(config)
if key == 'q' or key == '\033':
return False
else:
return self.main_menu(config)
# Function that allows the player to access the store menu generated by the config, If the player has the item
# it will be removed from the store except for the energy boosts
def store_menu(self, player):
while True:
buffer = (self.height - (len(self.store) + 6)) // 2
self.__spacer(buffer)
print("Welcome to the Store!".center(self.width))
print("Your Money: ${}".format(player.get_money()).center(self.width))
print()
print("Items Available For Purchase: ".center(self.width))
index = 1
keys = []
for key in self.store:
if not player.has_item(key):
keys.append(key)
print(("Enter " + str(index) + " to Buy: " + capwords(str(key).replace("_", " ")) +
" @ Price: $" + str(self.store[key])).center(self.width))
index += 1
print()
print("Enter 0 to Leave the Store!".center(self.width))
self.__spacer(buffer + self.start)
try:
choice = int(input("What Do You Want to Buy: "))
except ValueError:
continue
choice -= 1
if choice == -1:
return
if choice < len(keys):
if player.add_inv(keys[choice], self.store[keys[choice]]):
print(("Added: " + capwords(str(keys[choice].replace("_", " "))) +
" to Inventory!").center(self.width))
if keys[choice] != "+10 energy" and keys[choice] != "+25 energy":
self.start += 1
sleep(1)
else:
print("Not Enough Money to Purchase Item!".center(self.width))
sleep(1)
# Function that prints out the keybinding for the game of frupal
def key_menu(self):
key_binds = ["Use keys on the keyboard to navigate and control the game and its menus!",
"^",
"Movements: ",
"(W) Move North!",
"(A) Move West! (D) Move East!",
"(S) Move South!",
"^",
"Utilities: ",
"(B) Store!",
"(C) Cheat!",
"(K) Keybindings!",
"(Q) Quit!"]
buffer = self.middle - (len(key_binds) // 2)
self.__spacer(buffer)
for i in range(len(key_binds)):
if key_binds[i] == '^':
print()
else:
print(key_binds[i].center(self.width))
self.__spacer(buffer)
print("(Press R) Return!".center(self.width))
key = readchar.readkey()
if key != 'r':
self.key_menu()
else:
return
# Functions that controls the game as the player plays, sends ints back to main where each int means different
# results
def control(self, player, game_map):
if player.has_item('jewels'):
game_map.map_reveal()
return 3
if player.get_energy() == 0:
game_map.map_reveal()
return 2
# end of game win conditions
key = readchar.readkey()
if key == 'w':
player.move(Direction.NORTH, game_map)
return 1
elif key == 'a':
player.move(Direction.WEST, game_map)
return 1
elif key == 'd':
player.move(Direction.EAST, game_map)
return 1
elif key == 's':
player.move(Direction.SOUTH, game_map)
return 1
elif key == 'c':
game_map.map_reveal()
return 3
elif key == 'k':
self.key_menu()
elif key == 'b':
self.store_menu(player)
return 1
elif key == 'q' or key == '\033':
return 0
else:
return 1