-
Notifications
You must be signed in to change notification settings - Fork 2
/
menus.py
60 lines (44 loc) · 2.34 KB
/
menus.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
'''
Copyright (C) 2018 Stefano Peris <[email protected]>
Github repository: <https://github.com/XenonLab-Studio/The_Dungeon_of_Sin.git>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import libtcodpy as libtcod
def menu(con, header, options, width, screen_width, screen_height):
if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.')
# calculate total height for the header (after auto-wrap) and one line per option
header_height = libtcod.console_get_height_rect(con, 0, 0, width, screen_height, header)
height = len(options) + header_height
# create an off-screen console that represents the menu's window
window = libtcod.console_new(width, height)
# print the header, with auto-wrap
libtcod.console_set_default_foreground(window, libtcod.white)
libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)
# print all the options
y = header_height
letter_index = ord('a')
for option_text in options:
text = '(' + chr(letter_index) + ') ' + option_text
libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text)
y += 1
letter_index += 1
# blit the contents of "window" to the root console
x = int(screen_width / 2 - width / 2)
y = int(screen_height / 2 - height / 2)
libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)
def inventory_menu(con, header, inventory, inventory_width, screen_width, screen_height):
# show a menu with each item of the inventory as an option
if len(inventory.items) == 0:
options = ['Inventory is empty.']
else:
options = [item.name for item in inventory.items]
menu(con, header, options, inventory_width, screen_width, screen_height)