How to use tcod - 10 common examples

To help you get started, we’ve selected a few tcod examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github libtcod / python-tcod / examples / samples_libtcodpy.py View on Github external
elif oc_x == -5:
            oc_xdir = 1
        if oc_y == SAMPLE_SCREEN_HEIGHT / 2 + 5:
            oc_ydir = -1
        elif oc_y == -5:
            oc_ydir = 1
    libtcod.console_blit(oc_screenshot, 0, 0, SAMPLE_SCREEN_WIDTH,
                         SAMPLE_SCREEN_HEIGHT, sample_console, 0, 0)
    libtcod.console_blit(oc_secondary, 0, 0, SAMPLE_SCREEN_WIDTH // 2,
                         SAMPLE_SCREEN_HEIGHT // 2, sample_console, oc_x, oc_y,
                         1.0,0.75)

#############################################
# line drawing sample
#############################################
line_bk = libtcod.Color()
line_init = False
line_bk_flag = libtcod.BKGND_SET

def render_lines(first, key, mouse):
    global line_bk, line_init, line_bk_flag

    flag_names=['BKGND_NONE',
                'BKGND_SET',
                'BKGND_MULTIPLY',
                'BKGND_LIGHTEN',
                'BKGND_DARKEN',
                'BKGND_SCREEN',
                'BKGND_COLOR_DODGE',
                'BKGND_COLOR_BURN',
                'BKGND_ADD',
                'BKGND_ADDALPHA',
github libtcod / python-tcod / examples / samples_libtcodpy.py View on Github external
]
    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))
github libtcod / python-tcod / examples / samples_libtcodpy.py View on Github external
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,
github libtcod / python-tcod / examples / samples_libtcodpy.py View on Github external
textColor.r = 255 - textColor.r
        textColor.g = 255 - textColor.g
        textColor.b = 255 - textColor.b
        libtcod.console_set_default_foreground(sample_console, textColor)
        for x in range(SAMPLE_SCREEN_WIDTH):
            for y in range(SAMPLE_SCREEN_HEIGHT):
                col = libtcod.console_get_char_background(sample_console, x, y)
                col = libtcod.color_lerp(col, libtcod.black, 0.5)
                c = libtcod.random_get_int(None, ord('a'), ord('z'))
                libtcod.console_set_default_foreground(sample_console, col)
                libtcod.console_put_char(sample_console, x, y, c,
                                         libtcod.BKGND_NONE)
    else:
        # same, but using the ConsoleBuffer class to speed up rendering
        buffer = libtcod.ConsoleBuffer(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT)  # initialize buffer
        c = libtcod.random_get_int(None, ord('a'), ord('z'))
        for x in range(SAMPLE_SCREEN_WIDTH):
            xcoef = float(x) / (SAMPLE_SCREEN_WIDTH - 1)
            top = libtcod.color_lerp(tc_cols[TOPLEFT], tc_cols[TOPRIGHT], xcoef)
            bottom = libtcod.color_lerp(tc_cols[BOTTOMLEFT], tc_cols[BOTTOMRIGHT], xcoef)
            for y in range(SAMPLE_SCREEN_HEIGHT):
                # for maximum speed, we avoid using any libtcod function in
                # this inner loop, except for the ConsoleBuffer's functions.
                ycoef = float(y) / (SAMPLE_SCREEN_HEIGHT - 1)
                r = int(top.r * ycoef + bottom.r * (1 - ycoef))
                g = int(top.g * ycoef + bottom.g * (1 - ycoef))
                b = int(top.b * ycoef + bottom.b * (1 - ycoef))
                c += 1
                if c > ord('z'): c = ord('a')
                # set background, foreground and char with a single function
                buffer.set(x, y, r, g, b, r // 2, g // 2, b // 2, chr(c))
        buffer.blit(sample_console)  # update console with the buffer's contents
github Wolfenswan / RoguelikeTut-TCOD / rendering / render_windows.py View on Github external
longest_string = max(all_strings, key=len)
        width = min(len(longest_string), round(cfg.SCREEN_WIDTH // 3)) + padding_x * 2
        if longest_string in options:
            width += 4 # This accounts for the listing points (e.g. (1) <option>)

    body_wrapped = dynamic_wrap(body, width - padding_x * 2) #if body != '' else []

    # Calculate window height #
    height = padding_y * 2 + len(body_wrapped)
    if options:
        height += len(options)
        if body_wrapped:
            height += 1 # gap between body-text and options

    # Create the window #
    window = tcod.console.Console(width, height)

    # Print the body to the window #
    y = padding_y
    if body_wrapped:
        for i, line in enumerate(body_wrapped):
            print_string(window, padding_x, y, line)
            y += 1
            if line.count('\n') &gt; 0:
                y += 1
        if options:
            y += 1 # add a gap between body and options, if body exists

    # Print options to the window #
    if options:
        letter_index = ord('a')
        for i, option in enumerate(options):</option>
github libtcod / python-tcod / examples / samples_libtcodpy.py View on Github external
for x in range(SAMPLE_SCREEN_WIDTH):
            xcoef = float(x) / (SAMPLE_SCREEN_WIDTH - 1)
            top = libtcod.color_lerp(tc_cols[TOPLEFT], tc_cols[TOPRIGHT], xcoef)
            bottom = libtcod.color_lerp(tc_cols[BOTTOMLEFT], tc_cols[BOTTOMRIGHT],
                                        xcoef)
            for y in range(SAMPLE_SCREEN_HEIGHT):
                ycoef = float(y) / (SAMPLE_SCREEN_HEIGHT - 1)
                curColor = libtcod.color_lerp(top, bottom, ycoef)
                libtcod.console_set_char_background(sample_console, x, y, curColor,
                                         libtcod.BKGND_SET)
        textColor = libtcod.console_get_char_background(sample_console,
                                             SAMPLE_SCREEN_WIDTH // 2, 5)
        textColor.r = 255 - textColor.r
        textColor.g = 255 - textColor.g
        textColor.b = 255 - textColor.b
        libtcod.console_set_default_foreground(sample_console, textColor)
        for x in range(SAMPLE_SCREEN_WIDTH):
            for y in range(SAMPLE_SCREEN_HEIGHT):
                col = libtcod.console_get_char_background(sample_console, x, y)
                col = libtcod.color_lerp(col, libtcod.black, 0.5)
                c = libtcod.random_get_int(None, ord('a'), ord('z'))
                libtcod.console_set_default_foreground(sample_console, col)
                libtcod.console_put_char(sample_console, x, y, c,
                                         libtcod.BKGND_NONE)
    else:
        # same, but using the ConsoleBuffer class to speed up rendering
        buffer = libtcod.ConsoleBuffer(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT)  # initialize buffer
        c = libtcod.random_get_int(None, ord('a'), ord('z'))
        for x in range(SAMPLE_SCREEN_WIDTH):
            xcoef = float(x) / (SAMPLE_SCREEN_WIDTH - 1)
            top = libtcod.color_lerp(tc_cols[TOPLEFT], tc_cols[TOPRIGHT], xcoef)
            bottom = libtcod.color_lerp(tc_cols[BOTTOMLEFT], tc_cols[BOTTOMRIGHT], xcoef)
github libtcod / python-tcod / tests / test_console.py View on Github external
def test_console_str():
    console = tcod.console.Console(10, 2)
    console.print_(0, 0, "Test")
    assert str(console) == ("")
github libtcod / python-tcod / tests / test_console.py View on Github external
def test_console_pickle():
    console = tcod.console.Console(width=12, height=10)
    console.ch[...] = ord('.')
    console.fg[...] = (10, 20, 30)
    console.bg[...] = (1, 2, 3)
    console2 = pickle.loads(pickle.dumps(console))
    assert (console.ch == console2.ch).all()
    assert (console.fg == console2.fg).all()
    assert (console.bg == console2.bg).all()
github libtcod / python-tcod / tests / test_libtcodpy.py View on Github external
def test_color_math():
    color_a = libtcodpy.Color(0, 1, 2)
    color_b = libtcodpy.Color(0, 10, 20)

    assert color_a + color_b == libtcodpy.Color(0, 11, 22)
    assert color_b - color_a == libtcodpy.Color(0, 9, 18)
    assert libtcodpy.Color(255, 255, 255) * color_a == color_a
    assert color_a * 100 == libtcodpy.Color(0, 100, 200)
github libtcod / python-tcod / tests / test_libtcodpy.py View on Github external
def test_console_set_char_background(console, bg):
    libtcodpy.console_set_char_background(console, 0, 0, bg, libtcodpy.BKGND_SET)
    assert_char(console, 0, 0, bg=bg)