/
StartGame.py
553 lines (456 loc) · 22.5 KB
/
StartGame.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# TODO доделать кнопки меню интерфейса. Сделать значки <-- button --> при нажатии энтер
from bearlibterminal import terminal
import Config, Game, Localization
# Цвет фона
bgcolor = Config.menu_bgcolor
# Цвет подсвеченного фона
lighted_bgcolor = Config.menu_lighted_bgcolor
# Ширина меню (то, что будет подсвечиваться)
menu_width = Config.menu_width
# Ширина экрана
screen_width = Config.screen_width
# Высота экрана
screen_height = Config.screen_height
# Положение верхней кнопки меню по вертикали
menu_height = Config.screen_height // 2 + 3
# Начальное положение по горизонтали
start_position_x = (screen_width - menu_width) // 2
# Используемые в этом меню кнопки клавиатуры
suitable_keys = Config.comand.values()
# Используемый шрифт
font_name = Config.settings['font_name']
# Размер шрифта
font_size = int(Config.settings['font_size'])
# Определяем. текст выводится с подсветкой фона (для текущего меню) или нет
def get_bgcolor(lighted = False):
color = bgcolor
if lighted:
color = lighted_bgcolor
return color
# Получение допустимой команды с клавиатуры
def get_key_code():
key = terminal.read()
while key not in suitable_keys:
key = terminal.read()
return key
####################################### Кнопки главного меню ###########################################################
# Кнопки главного меню. Номер, текст, выполняемая команда.
# Текст сразу переводится и нормализуется. Вычисляется положение по вертикали.
class Main_menu_button():
def __init__(self, number, text, make):
self.number = number
self.text = Localization.translate_text(text)
self.normilized_text = self.normilize_text()
self.make = make
self.height = menu_height + self.number
# Меню выводится по центру с шириной, указанной в конфиге. Если подсветка только текста, то получается некрасиво,
# поэтому к тексту слева и справа добавляются пробелы для получения равной ширины подсветки
def normilize_text(self):
text = self.text
length = len(text)
left_spaces_count = (menu_width - length) // 2
right_spaces_count = menu_width - length - left_spaces_count
normilized_text = ' ' * left_spaces_count + text + ' ' * right_spaces_count
return normilized_text
# Печатаем нормализованный текст кнопки, позиция по вертикали определяется номером кнопки, подсветка - передаваемым
# параметром
def print(self, lighted=False):
current_bgcolor = get_bgcolor(lighted)
height = self.height
text = self.normilized_text
terminal.bkcolor(current_bgcolor)
terminal.printf(start_position_x, height, text)
terminal.bkcolor(bgcolor)
terminal.refresh()
###################################### Главное меню ####################################################################
# Главное меню - переход к игре, настройкам, выход
class Main_menu:
def __init__(self, buttons):
self.buttons = buttons
self.length = len(self.buttons)
self.state = 0
self.time_to_exit = False
# Вывод всего меню на экран
def view(self):
terminal.clear()
self.state = 0
lighted = True
self.buttons[0].print(lighted)
for i in range(1, self.length):
self.state = i
self.buttons[i].print()
self.state = 0
terminal.refresh()
# Выбор действия по нажатию клавиши
def make(self, key):
if key == Config.comand['esc'] or key == Config.comand['close']:
self.time_to_exit = True
elif key == Config.comand['up'] or key == Config.comand['arrow up']:
self.up()
elif key == Config.comand['down'] or key == Config.comand['arrow down']:
self.down()
elif key == Config.comand['enter']:
# Последняя кнопка это выход, ее номер self.len - 1, остальные кнопки выполняют свои функции
if self.state == self.length - 1:
self.time_to_exit = True
else:
self.buttons[self.state].make()
self.view()
# Переход кнопкой выше
def up(self):
self.buttons[self.state].print()
self.state = (self.state - 1) % self.length
lighted = True
self.buttons[self.state].print(lighted)
# Переход кнопкой ниже
def down(self):
self.buttons[self.state].print()
self.state = (self.state + 1) % self.length
lighted = True
self.buttons[self.state].print(lighted)
# Запуск меню
def run(self):
self.view()
while not self.time_to_exit:
key = get_key_code()
self.make(key)
################################################ Кнопки менюшек ########################################################
# На вход подаем номер кнопки и её имя
class Sub_menu_button():
def __init__(self, number, name, hint = ''):
self.number = number
self.name = name
self.hint = self.get_hint(hint)
self.left_text = self.get_left_text()
self.right_text = self.get_right_text()
self.right_text_start_position = screen_width // 2
# Верхняя строка уходит на шапку, следующая на разделитель, а со второй строки уже идут кнопки
self.height = 2 + self.number
# Для каждого меню своя подсказка
def get_left_text(self):
text = self.name
translated_text = Localization.translate_text(text)
# добавляем справа пробелы для подсветки, 5, врде, нормально
count_of_left_spases = 5
left_spases = ' ' * count_of_left_spases
count_of_right_spases = screen_width//2 - len(translated_text) - count_of_left_spases
right_spases = ' ' * count_of_right_spases
extended_text = left_spases + translated_text + right_spases
return extended_text
# Отличается в разных меню, так что бцдет в подклассах
def get_right_text(self):
pass
# Выравниваем текст пробелами по центру
def centrify_text(self, text):
length = len(text)
# Левая часть экрана, значит берем половину. Оставшееся свободное место делим на 2 части
count_of_left_spases = (screen_width // 2 - length) // 2
# оставшееся свободное место кидаем направо
count_of_right_spases = screen_width // 2 - length - count_of_left_spases
centrified_text = ' ' * count_of_left_spases + text + ' ' * count_of_right_spases
return centrified_text
# Выводим кнопку на экран
def print(self, lighted = False):
# Подсветка текущей позиции
current_bgcolor = get_bgcolor(lighted)
terminal.bkcolor(current_bgcolor)
# Сначала левую часть
x = 0
y = self.height
text = self.left_text
terminal.printf(x, y, text)
# Потом правую
x = self.right_text_start_position
y = self.height
text = self.right_text
terminal.printf(x, y, text)
# Возвращаем подсветку, как было
terminal.bkcolor(bgcolor)
# Стираем старую подсказку и печатаем новую
self.clear_hint()
self.print_hint()
# И обновляем экран
terminal.refresh()
# Выводим на правую часть экрана сообщение
def print_message(self, text, button_with_arrows = False):
x = self.right_text_start_position
y = self.height
self.clear_right_text()
left_arrow = '<-- '
right_arrow = ' -->'
if button_with_arrows:
text = left_arrow + text + right_arrow
centrifyed_text = self.centrify_text(text)
terminal.printf(x, y, centrifyed_text)
terminal.refresh()
# Команда, выполняемая по нажатию кнопки, для каждой кнопки своя
def make(self):
pass
# Очистка правой части строки
def clear_right_text(self):
x = self.right_text_start_position
y = self.height
# Заполняем пробелами правую часть строки
text = ' ' * (screen_width//2)
terminal.printf(x, y, text)
# Печатаем посказку внизу экрана
def print_hint(self):
text = self.hint
x = 0
y = screen_height - 1
terminal.printf(x, y , text)
# Стираем подсказку
def clear_hint(self):
x = 0
y = screen_height - 1
spases = ' ' * screen_width
terminal.printf(x, y , spases)
# Наводим красоту на текст подсказки
def get_hint(self, text):
hint = Localization.translate_text(text)
length = len(hint)
count_of_left_spases = (screen_width - length)//2
left_spases = ' ' * count_of_left_spases
count_of_rightn_spases = screen_width - length - count_of_left_spases
right_spases = ' ' * count_of_rightn_spases
centrifyed_hint = left_spases + hint + right_spases
return centrifyed_hint
######################################## Кнопки настроек управления ####################################################
class Keyset_menu_button(Sub_menu_button):
def get_right_text(self):
name = self.name
text = Config.get_comand_key_from_name(name)
centrified_text = self.centrify_text(text)
return centrified_text
# Команда, выполняемая по нажатию кнопки
def make(self):
self.clear_right_text()
message = Localization.translate_text('Press any key')
self.print_message(message)
key_code = terminal.read();
key = Config.convert_code_to_key(key_code)
message = Config.set_comand(self.name, key)
translated_message = Localization.translate_text(message)
self.right_text = self.get_right_text()
self.print_message(translated_message)
if translated_message in [Localization.translate_text('This key is already busy'),
Localization.translate_text('Can not set this key')]:
terminal.read()
######################################## Кнопка настройки языка #####################################################
class Interface_menu_button_language(Sub_menu_button):
def get_right_text(self):
name = self.name
text = Config.settings[name]
centrified_text = self.centrify_text(text)
return centrified_text
# Здесь перечисляем все поддерживаемые языки
def set_list_of_languages(self):
self.languages = ['en', 'ru']
# Определяем позицию текущего языка
def get_state_of_languages(self):
self.state_of_language = self.languages.index(Config.settings['language'])
def next_lang(self):
self.state_of_language = (self.state_of_language + 1) % len(self.languages)
new_lang = self.languages[self.state_of_language]
self.print_message(new_lang, True)
def prev_lang(self):
self.state_of_language = (self.state_of_language - 1) % len(self.languages)
new_lang = self.languages[self.state_of_language]
self.print_message(new_lang, True)
# Команда, выполняемая по нажатию кнопки
def make(self):
self.set_list_of_languages()
self.get_state_of_languages()
self.print_message(Config.settings['language'], True)
key = terminal.read()
# Эти кнопки завершают ввод значения
while key not in [Config.comand['enter'], Config.comand['esc']]:
if key in [Config.comand['left'], Config.comand['arrow left']]:
self.next_lang()
if key in [Config.comand['right'], Config.comand['arrow right']]:
self.prev_lang()
key = terminal.read()
# Выход происходит по энтеру и эскейпу, если эскейп - ничего не сохраняем
if key == Config.comand['enter']:
new_language = self.languages[self.state_of_language]
Config.set_language(new_language)
self.right_text = self.get_right_text()
self.print()
######################################## Кнопка настройки размера шрифта #####################################################
class Interface_menu_button_font_size(Sub_menu_button):
def get_right_text(self):
self.font_size = self.get_font_size()
text = str(self.font_size)
centrified_text = self.centrify_text(text)
return centrified_text
# font_size это число, а right_text это текст. Число нужно для увеличения - уменьшенияБ а текст для вывода
# причем текст уже отформатирован по центру
def get_font_size(self):
font_size = int(Config.settings['font_size'])
return font_size
def make(self):
self.font_size = self.get_font_size()
text = str(self.font_size)
self.print_message(text, True)
key = terminal.read()
while key not in [Config.comand['enter'], Config.comand['esc']]:
if key in [Config.comand['left'], Config.comand['arrow left']]:
self.decrease_font_size()
if key in [Config.comand['right'], Config.comand['arrow right']]:
self.increase_font_size()
key = terminal.read()
if key == Config.comand['enter']:
new_font_size = str(self.font_size)
Config.set_font_size(new_font_size)
self.right_text = self.get_right_text()
self.print()
def decrease_font_size(self):
if self.font_size > 0:
self.font_size -= 1
text = str(self.font_size)
self.print_message(text, True)
def increase_font_size(self):
self.font_size += 1
text = str(self.font_size)
self.print_message(text, True)
############################################### Меню настроек ##########################################################
class Sub_menu():
def __init__(self, name, buttons):
# Заголовок меню
translated_name = Localization.translate_text(name)
normilized_name = '| ' + translated_name + ' |'
self.name = normilized_name
# Кнопки меню
self.buttons = buttons
# Начальная позиция
self.state = 0
# Количество кнопок
self.length = len(buttons)
def print(self):
for button in self.buttons:
# Если номер кнопки совпадает с номером активной позиции меню, то подсвечиваем
button.print(self.state == button.number)
def up(self):
button = self.buttons[self.state]
button.print()
self.state = (self.state - 1) % self.length
lighted = True
button = self.buttons[self.state]
button.print(lighted)
def down(self):
button = self.buttons[self.state]
button.print()
self.state = (self.state + 1) % self.length
lighted = True
button = self.buttons[self.state]
button.print(lighted)
# Нажатую клавишу получаем из Game_menu, откуда и запускается это меню
def run(self):
self.print()
key = get_key_code()
while not key in [Config.comand['esc'], Config.comand['close'], Config.comand['left'],
Config.comand['arrow left'], Config.comand['right'], Config.comand['arrow right']]:
self.make(key)
key = get_key_code()
return key
def make(self, key):
if key == Config.comand['up'] or key == Config.comand['arrow up']:
self.up()
elif key == Config.comand['down'] or key == Config.comand['arrow down']:
self.down()
elif key == Config.comand['enter']:
self.buttons[self.state].make()
self.print()
######################################## Меню настроек игры ############################################################
# Включает в себя все меню настроек: управления, экрана и т.д.
class Game_menu():
def __init__(self, menus):
# Начальное положение
self.state = 0
self.border = '-' * screen_width
self.menus = menus
self.length = len(self.menus)
self.menus_names = self.get_menus_names()
self.menus_names_start_position = self.get_menus_names_start_positions()
def get_menus_names(self):
menus_names = []
for menu in self.menus:
new_name = menu.name
menus_names.append(new_name)
return menus_names
def get_menus_names_start_positions(self):
menus_names = self.menus_names
menus_start_position = []
position = 0
for name in menus_names:
menus_start_position.append(position)
position += len(name)
return menus_start_position
def print(self):
terminal.clear()
x = 0
y = 0
for i in range(self.length):
x = self.menus_names_start_position[i]
y = 0
text = self.menus_names[i]
current_bgcolor = get_bgcolor(i == self.state)
terminal.bkcolor(current_bgcolor)
terminal.printf(x, y, text)
terminal.bkcolor(bgcolor)
# Вторая строка сверху отделяем заголовки меню от кнопок
x = 0
y = 1
terminal.printf(x, y, self.border)
# Вторая строка снизу - отделяем подсказку от кнопок
x = 0
y = screen_height - 2
terminal.printf(x, y, self.border)
terminal.refresh()
def run(self):
key = None
while key not in [Config.comand['esc'], Config.comand['close']]:
current_menu = self.menus[self.state]
self.print()
key = current_menu.run()
if key == Config.comand['right'] or key == Config.comand['arrow right']:
self.right()
key = None
if key == Config.comand['left'] or key == Config.comand['arrow left']:
self.left()
key = None
def right(self):
self.state = (self.state + 1) % self.length
def left(self):
self.state = (self.state - 1) % self.length
########################################################################################################################
# Открываем консоль, задаем шрифт и его размер.
terminal.open()
terminal.set('font: %s, size=%d;' % (font_name, font_size))
# Создаем кнопки настроек управления: налево, направо, вверх, вниз
keyset_menu_button_left = Keyset_menu_button(0, 'left', 'Press enter')
keyset_menu_button_right = Keyset_menu_button(1, 'right', 'Press enter')
keyset_menu_button_up = Keyset_menu_button(2, 'up', 'Press enter')
keyset_menu_button_down = Keyset_menu_button(3, 'down', 'Press enter')
interface_menu_button_language = Interface_menu_button_language(0, 'language', 'Press enter and left/right (need restart)')
interface_menu_button_font_size = Interface_menu_button_font_size(1, 'font size', 'Press enter and left/right (need restart)')
# Собираем их вместе
keyset_menu_buttons = [keyset_menu_button_left, keyset_menu_button_right, keyset_menu_button_up, keyset_menu_button_down]
game_menu_buttons = [interface_menu_button_language, interface_menu_button_font_size]
# Создаем подменюшки
keyset_menu = Sub_menu('Keyset menu', keyset_menu_buttons)
interface_menu = Sub_menu('Interface settings', game_menu_buttons)
# Собираем все подменюшки вместе
sub_menus = [keyset_menu, interface_menu]
# Создаем игровое меню (настройки)
game_menu = Game_menu(sub_menus)
# Задаем кнопки главного меню (стартовый экран)
main_menu_button_start_game = Main_menu_button(0, 'Start game', Game.run_game)
main_menu_button_settings = Main_menu_button(1, 'Settings', game_menu.run)
main_menu_button_exit = Main_menu_button(2, 'Exit', None)
# Собираем их вместе
main_menu_buttons = [main_menu_button_start_game, main_menu_button_settings, main_menu_button_exit]
# Создаем само главное меню
main_menu = Main_menu(main_menu_buttons)
# Запускаем
main_menu.run()