예제 #1
0
def sprite_init(scr: c.window, text: c.window, sprite: dict, erase=True):
    show_sprite(scr, sprite['image'], erase)
    for group in sprite['lines']:
        for i, line in enumerate(group):
            text.addstr(i, 0, line)
        text.getch()
        text.erase()
    scr.erase()
    scr.refresh()
예제 #2
0
def show_sprite(scr: c.window, sprite: list, erase=True):
    if erase:
        scr.erase()
    scr_y, scr_x = scr.getmaxyx()
    begin_y = scr_y // 2 - len(sprite) // 2
    begin_x = scr_x // 2 - len(sprite[0]) // 2

    for i, s in enumerate(sprite):
        scr.addstr(begin_y + i, begin_x, s)
    scr.refresh()
예제 #3
0
def curses_get_input(stdscr: curses.window, inputwin: curses.window,
                     msgqueue: Queue, status):
    to_send = ""
    next_char_x = 2
    next_char_y = 0
    inputwin.addstr(0, 0, ">>")
    inputwin.refresh()
    while status.run:
        curr_in = inputwin.getkey()
        with open("keys.log", "a") as f:
            f.write(f"got {curr_in}")
        if curr_in == "\n":
            msgqueue.put(to_send)
            to_send = ""
            inputwin.erase()
            next_char_x = next_char_y = 0
            inputwin.addstr(next_char_y, next_char_x, ">>")
            next_char_x += 2
        elif curr_in == 'KEY_BACKSPACE' or curr_in == curses.KEY_BACKSPACE or curr_in == "^?" or curr_in == "\b" or curr_in == '\x7f':
            with open("clientdebug.log", "a") as f:
                f.write(f"got key {curr_in} went into backspace case\n")
            if next_char_x == 2 and next_char_y == 0:
                continue  #nothing to delete
            if next_char_x == 0 and next_char_y > 0:  # at the start of a new line
                next_char_y -= 1
                next_char_x = INPUT_WIDTH - 1
            elif (next_char_x > 0 and next_char_y > 0) or next_char_x > 2:
                next_char_x -= 1

            inputwin.addstr(next_char_y, next_char_x, " ")
            inputwin.move(next_char_y, next_char_x)
            to_send = to_send[:-1]
        else:
            to_send += curr_in
            inputwin.addstr(next_char_y, next_char_x, curr_in)
            next_char_x += 1
            if next_char_x == INPUT_WIDTH:
                next_char_x = 0
                next_char_y += 1

        inputwin.refresh()
예제 #4
0
파일: end.py 프로젝트: nicey0/tui-snake
def run_end(scr: curses.window, score: int):
    curses.curs_set(0)
    message: str = f"Game over! Score: {score}"
    menu_items: list = ["Play again", "Exit"]
    game_state = ""
    cursor: int = 0
    while True:
        scr.erase()
        scr.addstr(1, 1, message, curses.A_BOLD)
        scr.addstr(2, 1, '-' * (len(message) + 5))
        for i, item in enumerate(menu_items):
            if i == cursor:
                scr.addstr(3 + i, 2, item, curses.A_REVERSE)
            else:
                scr.addstr(3 + i, 2, item)
        # ++ Tick ++
        key = scr.getch()
        cursor, game_state = process_key(key, cursor, menu_items)
        if game_state != "":
            return game_state, 0
        # -- Tick --
        scr.refresh()
예제 #5
0
def run_mainmenu(scr: curses.window):
    curses.curs_set(0)
    title: str = "Snake"
    menu_items: list = ["Start", "Exit"]
    game_state = ""
    cursor: int = 0
    while True:
        scr.erase()
        scr.addstr(1, 1, title, curses.A_BOLD)
        scr.addstr(2, 1, '-'*(len(title)*2))
        for i, item in enumerate(menu_items):
            if i == cursor:
                scr.addstr(3+i, 2, item, curses.A_REVERSE)
            else:
                scr.addstr(3+i, 2, item)
        # ++ Tick ++
        key = scr.getch()
        cursor, game_state = process_key(key, cursor, menu_items)
        if game_state != "":
            return game_state, 0
        # -- Tick --
        scr.refresh()
예제 #6
0
def run_game(scr: curses.window):
    curses.curs_set(0)
    scr.nodelay(True)
    game_state = ""
    status_w: int = 25
    maxy = scr.getmaxyx()[0] - 1
    maxx = scr.getmaxyx()[1]

    midy = int(maxy / 2)
    midx = int(maxx / 2)
    snake: list = []
    for i in range(SNAKE_LEN):
        snake.append((midy, status_w + midx + i))
    direction = WEST
    apples: list = []
    score = 0
    while True:
        scr.erase()
        draw_statusbar(scr, (maxy - 1, maxx), status_w, score)
        draw_snek(scr, snake, direction)
        draw_apples(scr, apples)
        # ++ Tick ++
        key = scr.getch()
        direction, game_state = process_key(key, direction)
        snake = move_snake(snake, direction)
        snake = portals(snake, direction, status_w + 2, (maxy - 1, maxx))
        snake, apples, nscore = eat_apples(snake, apples)
        apples = create_apples(snake, apples, MAX_APPLES - len(apples),
                               status_w, (maxy, maxx))
        score += nscore
        if game_state != "":
            return game_state, 0
        # -- Tick --
        scr.refresh()
        if check_loss(snake):
            curses.napms(1000)
            return "end", score
        curses.napms(100)
예제 #7
0
def start(screen: curses.window):
    while 1:

        key = screen.getch()
        screen.erase()
        h_h, h_w = center(screen)
        head_text = "Potato Messenger"
        head = (h_h // 2, h_w - len(head_text) // 2, head_text)
        click_text = "Click Here To Continue or press Enter."
        button = (h_h, h_w - len(click_text) // 2, click_text)
        box1 = screen.subwin(3,
                             len(button[2]) + 4, button[0] - 1, button[1] - 2)
        box1.box()
        screen.addstr(head[0], head[1], head[2])
        screen.addstr(button[0], button[1], button[2])
        if key == curses.KEY_ENTER or key in (10, 13):
            break
        if key == curses.KEY_MOUSE:
            _, mx, my, *_ = curses.getmouse()
            if button[1] <= mx <= button[1] + len(
                    button[2]) and my == button[0]:
                break
        screen.refresh()
예제 #8
0
def chat_page(screen: curses.window):
    global running
    user_typing = False
    read_str = ""
    rendered_display_chats = 0
    display_chats: List[str] = []
    while running:
        key = screen.getch()
        if key == curses.KEY_MOUSE:
            _, mx, my, *_ = curses.getmouse()
            if my == center(screen)[0] * 2 - 2:
                user_typing = True
            else:
                user_typing = False
        elif key == 27 or key == curses.KEY_BREAK:
            running = False
        screen.erase()
        start_index = -1
        end_index = -(center(screen)[0] * 2 - 3) - 1
        box2 = screen.subwin(3,
                             center(screen)[1] * 2,
                             center(screen)[0] * 2 - 3, 0)
        box2.box()

        start_render_from = rendered_display_chats
        rendered_display_chats = len(chats)
        width_limit = center(screen)[1] * 2 - 2
        for chat in chats[start_render_from:]:
            parts = textwrap.fill(chat, width_limit).split("\n")
            display_chats.extend(parts)
        for index, msg in enumerate(display_chats[start_index:end_index:-1]):
            y = center(screen)[0] * 2 - 4 - index
            x = 0
            screen.addstr(y, x, msg)
        if user_typing:
            curses.curs_set(1)
            screen.move(
                center(screen)[0] * 2 - 2,
                len(read_str[-(center(screen)[1] - 2) * 2:]))
            if key:
                if key == curses.KEY_ENTER or key in (10, 13):
                    if read_str:
                        send_this.append(read_str)
                        read_str = ""
                elif key == curses.KEY_BACKSPACE or key == 8:
                    if read_str:
                        read_str = read_str[:-1]
                else:
                    if curses.ascii.isascii(key):
                        letter = chr(key)
                        read_str += letter
            screen.move(
                center(screen)[0] * 2 - 2,
                len(read_str[-(center(screen)[1] - 2) * 2:]))
        else:
            screen.move(0, center(screen)[1] * 2 - 1)
            if key == ord("q"):
                running = False
            elif key == ord("w") or key == curses.KEY_ENTER or key in (10, 18):
                user_typing = True
        screen.addstr(
            center(screen)[0] * 2 - 2, 1,
            read_str[-(center(screen)[1] - 2) * 2 - 1:])
        screen.refresh()