Ejemplo n.º 1
0
def display_menu(screen: curses.window, menu_items, cur_index: int):
    """
    prints the menu to the console
    """

    # obtaining size of the screen
    sheight, swidth = screen.getmaxyx()

    welcome = "Welcome to Battleships"

    screen.addstr(1, swidth // 2 - len(welcome) // 2, '#' * len(welcome))
    screen.addstr(2, swidth // 2 - len(welcome) // 2, welcome)
    screen.addstr(3, swidth // 2 - len(welcome) // 2, '#' * len(welcome))

    # printing the menu in center of the screen
    for index, item in enumerate(menu_items):
        x = swidth // 2 - len(item) // 2
        y = sheight // 2 - len(menu_items) // 2 + index
        if index == cur_index:
            screen.attron(curses.color_pair(1))
            screen.addstr(y, x, item)
            screen.attroff(curses.color_pair(1))
        else:
            screen.addstr(y, x, item)

    screen.refresh()
Ejemplo n.º 2
0
def colorize(stdscr: Window, color: Color) -> Generator[Window, None, None]:
    """
    Context manager to make colorizing operations easier.
    """
    pair = color_pair(color) if has_colors() else None
    if pair is not None:
        stdscr.attron(pair)
    yield stdscr
    if pair is not None:
        stdscr.attroff(pair)