-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncs.py
More file actions
270 lines (224 loc) · 8.6 KB
/
funcs.py
File metadata and controls
270 lines (224 loc) · 8.6 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import curses as c
from settings import base_exp
def init_borders(scr):
scr.border('|', '|', '-', '-', '+', '+', '+', '+')
scr.addstr(17, 0, '+' + '-' * 52 + '+' + '-' * 25 + '+')
for i in range(5):
scr.addch(18 + i, 53, '|')
scr.addch(23, 53, '+')
scr.refresh()
def show_sprite(scr: c.window, sprite: list, erase=True):
if erase:
scr.erase()
scr_y, scr_x = scr.getmaxyx()
begin_y = scr_y // 2 - len(sprite) // 2
begin_x = scr_x // 2 - len(sprite[0]) // 2
for i, s in enumerate(sprite):
scr.addstr(begin_y + i, begin_x, s)
scr.refresh()
def sprite_init(scr: c.window, text: c.window, sprite: dict, erase=True):
show_sprite(scr, sprite['image'], erase)
for group in sprite['lines']:
for i, line in enumerate(group):
text.addstr(i, 0, line)
text.getch()
text.erase()
scr.erase()
scr.refresh()
def input_str(y, x, scr: c.window, max_len=0, prompt=''):
res = ''
len_count = 0
if prompt:
scr.addstr(y, x, prompt)
if max_len > 0:
scr.addstr(y + int(bool(prompt)), x, '_' * max_len)
while True:
char = scr.get_wch()
if char == '\n':
break
elif char in ('\b', '\x7f'):
if len_count > 0:
res = res[:-1]
scr.addch(y + int(bool(prompt)), x + len_count - 1, '_')
scr.refresh()
len_count -= 1
elif len(char) == 1:
res += char
scr.addch(y + int(bool(prompt)), x + len_count, char)
scr.refresh()
len_count += 1
if 0 < max_len <= len_count:
break
return res
def choose(scr, choices: dict, *args, **kwargs):
if args:
print_text(args[0], args[1], False)
scr.erase()
line_num = 0
for key in choices:
scr.addstr(line_num, 0, "%s: " % key.upper())
for line in str(choices[key]).split('\n'):
scr.addstr(line_num, 3, line)
line_num += 1
keys = list(choices.keys())
if kwargs and "forbidden" in kwargs.keys():
for k in kwargs["forbidden"]:
if k in keys:
keys.remove(k)
while True:
key = scr.getkey()
if key in keys:
if (not kwargs) or ("erase" not in kwargs.keys()) or kwargs["erase"]:
scr.erase()
return key
def print_text(text, str_to_print, pause: int = 1):
text.erase()
line = 0
i = 0
for ch in str_to_print:
if ch == '\n':
line += 1
i = 0
else:
text.addch(line, i, ch)
i += 1
c.napms(20)
if pause == 1:
c.napms(500)
elif pause == -1:
text.getch()
def choose_cg(gfx, team: list, prompt_win=None, prompt_text=None, erase=True):
keys = ['q', 'w', 'e', 'a', 's', 'd']
choices = {}
key_to_cg = {'x': None}
for i, cg in enumerate(team):
choices[keys[i]] = ("%s%s Lv%d %s %d/%d Atk %d Def %d Spe %d" %
("FAINTED - " if not cg.alive else '',
cg.name, cg.level, get_hp_string(cg),
cg.current_hp, cg.normal_hp,
cg.current_stats[0],
cg.current_stats[1],
cg.current_stats[2]))
key_to_cg[keys[i]] = cg
choices['x'] = 'Exit'
if prompt_win and prompt_text:
key = choose(gfx, choices, prompt_win, prompt_text, erase=erase)
else:
key = choose(gfx, choices, erase=erase)
chosen_cg = key_to_cg[key]
return chosen_cg
def get_hp_string(cg):
hp_symbols = int(cg.current_hp * 12 / cg.normal_hp) \
if cg.current_hp > 0 else 0
return "HP:|%s|" % ('#' * hp_symbols + ' ' * (12 - hp_symbols))
def get_exp_string(cg):
if cg.level == 100:
return ''
exp_symbols = int((cg.exp - base_exp[cg.exp_group][cg.level - 1])
* 12 / (base_exp[cg.exp_group][cg.level]
- base_exp[cg.exp_group][cg.level - 1]))
return "EXP:%s|" % ('-' * exp_symbols + ' ' * (12 - exp_symbols))
def cg_summary(gfx, cg):
gfx.erase()
# sprite
for i, line in enumerate(cg.sprite_front):
gfx.addstr(i + 1, 1, line)
# basic info (name, types, level)
gfx.addstr(1, 27, "Name: %s (%s)" % (cg.name, cg.species))
gfx.addstr(1, 65, "Lv. %d" % cg.level)
gfx.addstr(2, 27, "Typing: %s%s" % (
cg.types[0].name, '' if len(cg.types) == 1 else '/' + cg.types[1].name
))
# HP
gfx.addstr(4, 27, get_hp_string(cg))
gfx.addstr(4, 45, "%d/%d" % (cg.current_hp, cg.normal_hp))
# EXP
if cg.level < 100:
gfx.addstr(6, 27, get_exp_string(cg))
gfx.addstr(6, 45, "To next level: %d" % (base_exp[cg.exp_group][cg.level] - cg.exp))
# other stats
gfx.addstr(8, 27, "Attack %d Defense %d Speed %d" % (
cg.normal_stats[0], cg.normal_stats[1], cg.normal_stats[2]
))
# moves
display_y = [11, 11, 13, 13]
display_x = [4, 42, 4, 42]
for i, move in enumerate(cg.move_set):
gfx.addstr(display_y[i], display_x[i], "%s (%s)" % (
move.name, move.type.name if move.typed else "STATUS"
))
gfx.refresh()
def cg_menu(graphics, text, choice, player, in_battle=False, current_battler=None):
while True:
cg = choose_cg(graphics, player.team, text, "Choose a CREAGRAM.", erase=False)
if not cg:
text.erase()
return
action = choose(choice,
{'q': "SUMMARY",
'w': ("(IN BATTLE)" if cg == current_battler else
"SWITCH IN" if cg.alive else "(CAN'T BATTLE)")
if in_battle else 'MOVE',
'e': "CANCEL"},
text, "What do you wish to do with %s?" % cg.name,
forbidden=[] if (cg.alive and cg != current_battler) or not in_battle else ['w'])
if action == 'q':
cg_summary(graphics, cg)
print_text(text, "Press any key to return...", 0)
graphics.getch()
elif action == 'w':
if in_battle:
return cg
team: list = player.team[:]
pos = team.index(cg)
graphics.erase()
key = None
print_text(text, 'Use the arrow keys to change the position of the \nselected CREAGRAM.\n' +
'Press <Q> at any time to return.')
while key != 'q':
graphics.erase()
for i, cur_cg in enumerate(team):
graphics.addstr(i, 2, "%s%s Lv%d %s %d/%d Atk %d Def %d Spe %d" %
("FAINTED - " if not cur_cg.alive else '',
cur_cg.name, cur_cg.level, get_hp_string(cur_cg),
cur_cg.current_hp, cur_cg.normal_hp,
cur_cg.current_stats[0],
cur_cg.current_stats[1],
cur_cg.current_stats[2]))
graphics.addstr(pos, 0, '>')
key = graphics.getkey()
if key == 'KEY_UP' and pos != 0:
pos -= 1
team[pos], team[pos + 1] = team[pos + 1], team[pos]
if key == 'KEY_DOWN' and pos != len(team) - 1:
pos += 1
team[pos], team[pos - 1] = team[pos - 1], team[pos]
player.team = team
elif action == 'e':
text.erase()
break
def item_menu(graphics, text, choice, player):
keys = ['q', 'w', 'e', 'r', 't', 'y', 'a', 's', 'd', 'f', 'g', 'h', 'z', 'c', 'v', 'b']
choices = {}
key_to_item = {}
for i, item in enumerate(player.items):
if player.items[item]:
choices[keys[i]] = "%s x%d" % (item.name, player.items[item])
key_to_item[keys[i]] = item
choices['x'] = 'Exit'
key_to_item['x'] = None
while True:
chosen_item = key_to_item[choose(graphics, choices, text, "Choose an ITEM.")]
if not chosen_item:
continue
action = choose(choice, {'q': 'USE', 'w': 'CANCEL'},
text, "What do you wish to do with %s?" % chosen_item.name)
if action == 'q':
break
chosen_cg = choose_cg(graphics, player.team, prompt_win=text, prompt_text="Which CG do you wish to use it on?")
if chosen_item.on_used(chosen_cg):
player.items[chosen_item] -= 1
text.erase()
return chosen_item, chosen_cg
def capitalize(a: str):
return a[0].upper() + a[1:]