Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
]
if key.vk in (libtcod.KEY_ENTER, libtcod.KEY_KPENTER):
line_bk_flag += 1
if (line_bk_flag & 0xff) > libtcod.BKGND_ALPH:
line_bk_flag=libtcod.BKGND_NONE
alpha = 0.0
if (line_bk_flag & 0xff) == libtcod.BKGND_ALPH:
# for the alpha mode, update alpha every frame
alpha = (1.0 + math.cos(libtcod.sys_elapsed_seconds() * 2)) / 2.0
line_bk_flag = libtcod.BKGND_ALPHA(alpha)
elif (line_bk_flag & 0xff) == libtcod.BKGND_ADDA:
# for the add alpha mode, update alpha every frame
alpha = (1.0 + math.cos(libtcod.sys_elapsed_seconds() * 2)) / 2.0
line_bk_flag = libtcod.BKGND_ADDALPHA(alpha)
if not line_init:
line_bk = libtcod.console_new(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT)
# initialize the colored background
for x in range(SAMPLE_SCREEN_WIDTH):
for y in range(SAMPLE_SCREEN_HEIGHT):
col = libtcod.Color(x * 255 // (SAMPLE_SCREEN_WIDTH - 1),
(x + y) * 255 // (SAMPLE_SCREEN_WIDTH - 1 +
SAMPLE_SCREEN_HEIGHT - 1),
y * 255 // (SAMPLE_SCREEN_HEIGHT-1))
libtcod.console_set_char_background(line_bk, x, y, col, libtcod.BKGND_SET)
line_init = True
if first:
libtcod.sys_set_fps(30)
libtcod.console_set_default_foreground(sample_console, libtcod.white)
libtcod.console_blit(line_bk, 0, 0, SAMPLE_SCREEN_WIDTH,
SAMPLE_SCREEN_HEIGHT, sample_console, 0, 0)
recty = int((SAMPLE_SCREEN_HEIGHT - 2) * ((1.0 +
math.cos(libtcod.sys_elapsed_seconds())) / 2.0))
def render_offscreen(first, key, mouse):
global oc_secondary, oc_screenshot
global oc_counter, oc_x, oc_y, oc_init, oc_xdir, oc_ydir
if not oc_init:
oc_init = True
oc_secondary = libtcod.console_new(SAMPLE_SCREEN_WIDTH // 2,
SAMPLE_SCREEN_HEIGHT // 2)
oc_screenshot = libtcod.console_new(SAMPLE_SCREEN_WIDTH,
SAMPLE_SCREEN_HEIGHT)
libtcod.console_print_frame(oc_secondary, 0, 0, SAMPLE_SCREEN_WIDTH // 2,
SAMPLE_SCREEN_HEIGHT // 2, False, libtcod.BKGND_NONE,
b'Offscreen console')
libtcod.console_print_rect_ex(oc_secondary, SAMPLE_SCREEN_WIDTH // 4,
2, SAMPLE_SCREEN_WIDTH // 2 - 2,
SAMPLE_SCREEN_HEIGHT // 2,
libtcod.BKGND_NONE, libtcod.CENTER,
b"You can render to an offscreen "
b"console and blit in on another "
b"one, simulating alpha "
b"transparency.")
if first:
libtcod.sys_set_fps(30)
# get a "screenshot" of the current sample screen
libtcod.console_blit(sample_console, 0, 0, SAMPLE_SCREEN_WIDTH,
def menu(con, header, options, width, screen_width, screen_height, background = 'light', foreground = 'dark'):
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) + 2
height = len(options) + header_height + 1
# 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, colors.get(foreground))
libtcod.console_set_default_background(window, colors.get(background))
libtcod.console_clear(window)
libtcod.console_print_rect_ex(window, 1, 1, width, height, libtcod.BKGND_SET, 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, 1, y, libtcod.BKGND_SET, libtcod.LEFT, text)
y += 1
letter_index += 1
libtcod.console_print_ex(window, 1, y, libtcod.BKGND_SET, libtcod.LEFT, " ")
libtcod.sys_set_fps(30)
# Animate
anim_time = libtcod.sys_elapsed_milli()
anim_frame = 0
libtcod.console_set_custom_font('sprite-font.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD, 32, 48)
libtcod.console_init_root(constants['screen_width'], constants['screen_height'], constants['window_title'], False)
libtcod.console_set_default_background(0, colors.get('dark'))
load_customfont()
con = libtcod.console_new(constants['map_width']*3, constants['map_height']*2)
panel = libtcod.console_new(constants['panel_width'], constants['screen_height'])
tooltip = libtcod.console_new(constants['screen_width'], 1)
messages_pane = libtcod.console_new(constants['message_width'], 1000)
inventory_pane = libtcod.console_new(constants['message_width'], 40)
player = None
entities = []
game_map = None
message_log = None
game_state = None
show_main_menu = True
show_load_error_message = False
main_menu_background_image_0 = libtcod.image_load('menu_background_0.png')
main_menu_background_image_1 = libtcod.image_load('menu_background_1.png')
main_menu_background_image_2 = libtcod.image_load('menu_background_2.png')
main_menu_background_image_3 = libtcod.image_load('menu_background_3.png')
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 = tcod.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 = tcod.console_new(width, height)
# print the header, with auto-wrap
tcod.console_set_default_foreground(window, tcod.white)
tcod.console_print_rect_ex(window, 0, 0, width, height, tcod.BKGND_NONE, tcod.LEFT, header)
# print all the options
y = header_height
letter_index = ord('a')
for option_text in options:
text = '(' + chr(letter_index) + ') ' + option_text
tcod.console_print_ex(window, 0, y, tcod.BKGND_NONE, tcod.LEFT, text)
y += 1
letter_index += 1
# blit the contents of "window" to the root console
x = int(screen_width / 2 - width / 2)
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 = tcod.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 = tcod.console_new(width, height)
# print the header, with auto-wrap
tcod.console_set_default_foreground(window, tcod.white)
tcod.console_print_rect_ex(
window, 0, 0, width, height, tcod.BKGND_NONE, tcod.LEFT, header
)
# print all the options
y = header_height
letter_index = ord("a")
for option_text in options:
if option_text == "Inventory is empty.":
text = f"( ) {option_text}"
else:
text = f"({chr(letter_index)}) {option_text}"
tcod.console_print_ex(window, 0, y, tcod.BKGND_NONE, tcod.LEFT, text)
def get_data(path: str) -> str:
"""Return the path to a resource in the libtcod data directory,"""
SCRIPT_DIR = os.path.dirname(__file__)
DATA_DIR = os.path.join(SCRIPT_DIR, "../libtcod/data")
return os.path.join(DATA_DIR, path)
SAMPLE_SCREEN_WIDTH = 46
SAMPLE_SCREEN_HEIGHT = 20
SAMPLE_SCREEN_X = 20
SAMPLE_SCREEN_Y = 10
font = get_data("fonts/dejavu10x10_gs_tc.png")
libtcod.console_set_custom_font(font, libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(80, 50, b'libtcod python sample', False)
sample_console = libtcod.console_new(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT)
#############################################
# parser unit test
#############################################
# parser declaration
if True:
print ('***** File Parser test *****')
parser=libtcod.parser_new()
struct=libtcod.parser_new_struct(parser, b'myStruct')
libtcod.struct_add_property(struct, b'bool_field', libtcod.TYPE_BOOL, True)
libtcod.struct_add_property(struct, b'char_field', libtcod.TYPE_CHAR, True)
libtcod.struct_add_property(struct, b'int_field', libtcod.TYPE_INT, True)
libtcod.struct_add_property(struct, b'float_field', libtcod.TYPE_FLOAT, True)
libtcod.struct_add_property(struct, b'color_field', libtcod.TYPE_COLOR, True)
libtcod.struct_add_property(struct, b'dice_field', libtcod.TYPE_DICE, True)
libtcod.struct_add_property(struct, b'string_field', libtcod.TYPE_STRING,
constants = get_constants()
tcod.console_set_custom_font(
CUSTOM_FONT, tcod.FONT_TYPE_GRAYSCALE | tcod.FONT_LAYOUT_TCOD
)
tcod.console_init_root(
constants["screen_width"],
constants["screen_height"],
constants["window_title"],
constants["full_screen"],
constants["renderer"],
"F",
True,
)
con = tcod.console_new(constants["screen_width"], constants["screen_height"])
panel = tcod.console_new(constants["screen_width"], constants["panel_height"])
player = None
entities = []
game_map = None
message_log = None
game_state = None
show_main_menu = True
show_load_error_message = False
main_menu_background_image = tcod.image_load(MENU_BACKGROUND)
key = tcod.Key()
mouse = tcod.Mouse()
window_width = SETTINGS.getSetting("WindowWidth")
window_height = SETTINGS.getSetting("WindowHeight")
screen_names = SETTINGS.getSetting("Screens")
# Initiate the Display
display = DisplayObject()
# Puts all screen settings into a dictionary
for screen_name in screen_names:
screen_data = SETTINGS.getSetting(screen_name)
display.panelInfo[screen_name] = screen_data
# Create the displays:
map_console = libtcod.console_new(
display.panelInfo["MapScreen"]["ScreenWidth"], display.panelInfo["MapScreen"]["ScreenHeight"])
hud_console = libtcod.console_new(
display.panelInfo["HUDScreen"]["ScreenWidth"], display.panelInfo["HUDScreen"]["ScreenHeight"])
quest_console = libtcod.console_new(
display.panelInfo["QuestScreen"]["ScreenWidth"], display.panelInfo["QuestScreen"]["ScreenHeight"])
toast_console = libtcod.console_new(
display.panelInfo["ToastScreen"]["ScreenWidth"], display.panelInfo["ToastScreen"]["ScreenHeight"])
# Set the font
libtcod.console_set_custom_font(SETTINGS.getResource("FontFile"),
libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
# Initialize display
libtcod.console_init_root(
window_width, window_height, 'Roguelike Me', False)
# Create the game object:
Game = GameObject(SETTINGS, debug)