How to use the curtsies.Input function in curtsies

To help you get started, we’ve selected a few curtsies 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 bpython / curtsies / examples / tictactoeexample.py View on Github external
def main():
    with Input() as input:
        with FullscreenWindow() as window:
            b = Board()
            while True:
                window.render_to_terminal(b.display())
                if b.turn == 9 or b.winner():
                    c = input.next() # hit any key
                    sys.exit()
                while True:
                    c = input.next()
                    if c == '':
                        sys.exit()
                    try:
                        if int(c) in range(9):
                            b = b.move(int(c))
                    except ValueError:
                        continue
github recursecenter / terminal_snake / snake.py View on Github external
def main():
    counter = FrameCounter()
    with FullscreenWindow() as window:
        print('Press escape to exit')
        game = SnakeGame(window.height, window.width)
        with Input() as input_generator:
            c = None
            last_c = ''
            for framenum in itertools.count(0):
                t0 = time.time()
                while True:
                    t = time.time()
                    temp_c = input_generator.send(max(0, t - (t0 + time_per_frame)))                    
                    if temp_c is not None:
                        c = temp_c
                    if c is None:
                        pass
                    elif c == '':
                        return
                    elif c == '' and last_c != '':
                        game.direction = (-1, 0)
                        last_c = ''
github DexterInd / GoPiGo3 / Projects / BasicRobotControl / run_this.py View on Github external
# result holds the exit string when we call a gopigo3 command
    # with the GoPiGo3WithKeyboard object
    result = "nothing"
    """
    result can take the following values:
    "nothing", "moving", "path", "static", "exit"
    """
    # if manual_mode is set to true, then the robot
    # moves for as long as the coresponding keys are pressed
    # if manual_mode is set to false, then a key needs
    # to be pressed once in order for the robot to start moving
    manual_mode = False
    successful_exit = True
    refresh_rate = 20.0

    with Input(keynames = "curtsies", sigint_event = True) as input_generator:
        while True:
            period = 1 / refresh_rate
            # if nothing is captured in [period] seconds
            # then send() function returns None
            key = input_generator.send(period)

            # if we've captured something from the keyboard
            if key is not None:
                result = gopigo3.executeKeyboardJob(key)

                if result == "exit":
                    break

            # if we haven't captured anything
            # and if the robot is set to manual_mode
            # then stop the robot from moving as soon as the key(s)
github bpython / curtsies / examples / chat.py View on Github external
def main(host, port):
    client = socket.socket()
    client.connect((host, port))
    client.setblocking(False)

    conn = Connection(client)
    keypresses = []

    with FullscreenWindow() as window:
        with Input() as input_generator:
            while True:
                a = FSArray(10, 80)
                in_text = ''.join(keypresses)[:80]
                a[9:10, 0:len(in_text)] = [red(in_text)]
                for i, line in zip(reversed(range(2,7)), reversed(conn.render())):
                    a[i:i+1, 0:len(line)] = [line]
                text = 'connected to %s:%d' % (host if len(host) < 50 else host[:50]+'...', port)
                a[0:1, 0:len(text)] = [blue(text)]

                window.render_to_terminal(a)
                ready_to_read, _, _ = select.select([conn, input_generator], [], [])
                for r in ready_to_read:
                    if r is conn:
                        r.on_read()
                    else:
                        e = input_generator.send(0)
github bpython / curtsies / examples / snake.py View on Github external
def main():
    MAX_FPS = 20
    time_per_frame = lambda: 1. / MAX_FPS

    with FullscreenWindow() as window:
        with Input() as input_generator:
            snake = Snake(window.height, window.width)
            while True:
                c = None
                t0 = time.time()
                while True:
                    t = time.time()
                    temp_c = input_generator.send(max(0, t - (t0 + time_per_frame())))
                    if temp_c == '':
                        return
                    elif temp_c == '+':
                        MAX_FPS += 1
                    elif temp_c == '-':
                        MAX_FPS = max(1, MAX_FPS - 1)
                    elif temp_c is not None:
                        c = temp_c # save this keypress to be used on next tick
                    if time_per_frame() < t - t0:
github bpython / curtsies / examples / simple.py View on Github external
def main():
    with FullscreenWindow() as window:
        print('Press escape to exit')
        with Input() as input_generator:
            a = FSArray(window.height, window.width)
            for c in input_generator:
                if c == '':
                    break
                elif c == '':
                    a = FSArray(window.height, window.width)
                else:
                    row = random.choice(range(window.height))
                    column = random.choice(range(window.width-len(repr(c))))
                    a[row, column:column+len(repr(c))] = [repr(c)]
                window.render_to_terminal(a)
github bpython / curtsies / examples / fps.py View on Github external
def realtime(fps=15):
    world = World()
    dt = 1/fps

    reactor = Input()
    schedule_next_frame = reactor.scheduled_event_trigger(Frame)
    schedule_next_frame(when=time.time())

    with reactor:
        for e in reactor:
            if isinstance(e, Frame):
                world.tick()
                print(world.s)
                when = e.when + dt
                while when < time.time():
                    when += dt
                schedule_next_frame(when)
            elif e == u'':
                break
            else:
                world.process_event(e)