def test_ellipses(scr, sleep=0.2): import random # scr.draw.ellipse((0, 0), (40, 20)) scr.context.color = 0.5, 0, 1 scr.high.draw.ellipse((90, 15), (200, 50), fill=True) scr.context.color = 1, 1, 1 scr.high.draw.ellipse((90, 15), (200, 50), fill=False) scr.high.draw.ellipse((5, 3), (85, 60), fill=False) pause() for i in range(30): with scr.commands: for x in range(0, scr.high.get_size()[0] - 50, 10): for y in range(0, scr.high.get_size()[1] - 30, 10): scr.context.color = ( random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1), ) scr.high.draw.ellipse( (x, y), (x + random.randrange(10, 40), y + random.randrange(5, 20)), ) inkey() time.sleep(sleep)
async def __anext__(self): # callbackless subscriptions can be used in an async-for to fetch events if self.callback: raise TypeError( "Subscriptions with a callback set can't be iterated") while not self.terminated: if self.queue: return self.queue.popleft() await asyncio.sleep(self.resolution) # HACK: pump keyboard events if not in a Screen context if KeyPress in self.types: terminedia.inkey(consume=False) process() raise StopAsyncIteration()
def main(): """Example and test for multi-threaded terminal output """ with keyboard(), Screen() as scr: scr.context.color = 1, 1, 1 scr.draw.line((0, 0), (100, 0)) threads = [] with scr.context(color=(1, 0, 0)) as ctx: for i, color in enumerate( [(0.1, 0, 0), (0.1, 0.05, 0), (0, 0.1, 0), (0, 0.1, 0.1), (0.1, 0, 0.1)] ): threads.append( threading.Thread(target=worker, args=(scr, i * 20, color, 5)) ) threads[-1].start() time.sleep(1 / 10) for t in threads: t.join() scr.draw.line((0, 13), (100, 13)) while True: key = inkey() if key == "\x1b": break time.sleep(1 / 30)
def iterate(sc): with TM.keyboard(): while True: if TM.inkey() == K.ESC: break sc.update() time.sleep(0.1)
def main(): """Example for drawing straight lines with the API, using the 1/4 block resolution. """ with keyboard(), Screen() as scr: test_lines(scr.high) while True: if inkey() == "\x1b": break
def draw2(event): global char sc = context.screen width, height = sc.size char = inkey() or char #if char == "\x1b": #Event(EventTypes.QuitLoop) sc.draw.rect(Rect((random.randint(0, width), random.randint(0, height)), width_height=(20, 10)), color=random.choice("red green yellow blue".split()), char=char, fill=True)
def main(shape, high=False, braille=False): """Quick example to navigate an string-defined shape through the terminal using the arrow keys! Press <ESC> to exit. Usage example: $ terminedia-shapes --custom=" \\n *** \\n * * \\n *** " """ # Work around apparent bug in click: if shape is None: shape = shape1 if "\\n" in shape: shape = shape.replace("\\n", "\n") shape = shape.rstrip("\n") size_ = V2((shape.find("\n") if "\n" in shape else len(shape)), shape.count("\n") + 1) factor = 1 with realtime_keyb(), Screen(clear_screen=False) as scr: parent_scr = scr if high: scr = scr.high factor = 2 elif braille: scr = scr.braille factor = 1 x = scr.get_size()[0] // 2 - 6 y = 0 pos = V2(x, y) old_pos = pos while True: key = inkey() if key in (K.ESC, "q"): break with parent_scr.commands: scr.draw.rect(pos, rel=size_ + (1, 1), erase=True) pos += (factor * ((key == K.RIGHT) - (key == K.LEFT)), factor * ((key == K.DOWN) - (key == K.UP))) scr.draw.blit( pos, shape, **({ "color_map": c_map } if shape == shape2 else {})) time.sleep(1 / 30)
def main(shape, high=False): """Quick example to navigate an string-defined shape through the terminal using the arrow keys! Press <ESC> to exit. Usage example: $ terminedia-shapes --custom=" \\n *** \\n * * \\n *** " """ # Work around apparent bug in click: if shape is None: shape = shape1 if "\\n" in shape: shape = shape.replace("\\n", "\n") size_ = (shape.find("\n") if "\n" in shape else 1), shape.count("\n") #size_ = 13, 7 factor = 1 #if shape == shape2: # size_ = 21, 12 with realtime_keyb(), Screen(clear_screen=False) as scr: parent_scr = scr if high: scr = scr.high factor = 2 x = scr.get_size()[0] // 2 - 6 y = 0 while True: key = inkey() if key in (K.ESC, "q"): break with parent_scr.commands: scr.draw.rect((x, y), rel=size_, erase=True) x += factor * ((key == K.RIGHT) - (key == K.LEFT)) y += factor * ((key == K.DOWN) - (key == K.UP)) scr.draw.blit((x, y), shape, **({ "color_map": c_map } if shape == shape2 else {})) time.sleep(1 / 30)
def run(self): self.start_scene() while True: key = terminedia.inkey() if key == K.ESC: raise GameOver() if key == K.DOWN: self.snake.direction = D.DOWN elif key == K.UP: self.snake.direction = D.UP elif key == K.RIGHT: self.snake.direction = D.RIGHT elif key == K.LEFT: self.snake.direction = D.LEFT self.snake.update(self) self.snake.draw(self.scr.high) self.maybe_create_item() self.show_status() time.sleep(1 / 30) self.tick += 1
def vitoria(vencedor): if (vencedor == 'empate'): print('\n\n Empate.') else: print('\n\n' + COLOR + vencedor + RESETCOLOR + ' ganhou!') posicoes = [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] cursorX = 0 cursorY = 0 rodada = 1 while (rodada > 0): posicoes[cursorY][cursorX] = '▮' tabuleiro() time.sleep(FPS) posicoes[cursorY][cursorX] = ' ' tabuleiro() time.sleep(FPS) with TM.keyboard(): key = TM.inkey() if (key == SELECT): cursorX, cursorY, rodada = jogada(cursorX, cursorY, rodada) else: cursorX, cursorY = controle(key, cursorX, cursorY, posicoes) tabuleiro()
def main(shape, resolution=None, clear=False, cycle=False): """Quick example to navigate an string-defined shape through the terminal using the arrow keys! Press <ESC> to exit. Usage example: $ terminedia-shapes --custom=" \\n *** \\n * * \\n *** " """ # Work around apparent bug in click: if shape is None: shape = shape1 if "\\n" in shape: shape = shape.replace("\\n", "\n") original_shape = shape shape = TM.shape( original_shape, **({ "color_map": c_map } if original_shape == shape2 else {})) shape = TM.FullShape.promote(shape, resolution=resolution) last_frame = time.time() time_acumulator = 0 counter = 0 def cycle_color(foreground, tick): if foreground != TM.DEFAULT_FG: return foreground return TM.Color(["red", "blue", "yellow", "lime"][tick % 4]) try: with keyboard(), Screen(clear_screen=clear) as scr: # with Screen(clear_screen=True) as scr: x = scr.get_size()[0] // 2 - 6 y = 0 pos = V2(x, y) sprite = scr.data.sprites.add(shape, pos, active=True) if cycle: sprite.transformers.append( TM.Transformer(foreground=cycle_color)) while True: key = inkey() if key in (K.ESC, "q"): break if not clear and key in (K.RIGHT, K.LEFT, K.UP, K.DOWN): scr.data.draw.rect(sprite.rect, erase=True) sprite.pos += ( ((key == K.RIGHT) - (key == K.LEFT)), ((key == K.DOWN) - (key == K.UP)), ) scr.update() current = time.time() ellapsed = current - last_frame time_acumulator += ellapsed counter += 1 pause_time = max(FRAME_DELAY - ellapsed, 0) time.sleep(pause_time) last_frame = time.time() finally: print( f"\nTotal frames: {counter}\nAverage time per frame: {time_acumulator / (counter or 1):.04f}" )