def on_frame(): dx = app.mouse_x - app.mouse_px dy = app.mouse_y - app.mouse_py l = math.sqrt(dx**2 + dy**2) d = math.atan2(dy, dx) j = min(45, int(l)) * 2 for i in range(j): f = i / j ll = l * random.uniform(0.25, 0.5) dd = d + random.uniform(-0.2, 0.2) vx = ll * math.cos(dd) vy = ll * math.sin(dd) app.particles.append( Particle(app.mouse_px + dx * f, app.mouse_py + dy * f, vx, vy)) app.screen.clear() for i, p in enumerate(app.particles): col = Color.hsl(i / len(app.particles), 1.0, 0.5) app.screen.print(" ", int(p.x), int(p.y), bg=col) p.update() if p.x < 0 or p.y < 0 or p.x >= app.screen.w or p.y >= app.screen.h: app.particles.remove(p) app.screen.update() app.mouse_px = app.mouse_x app.mouse_py = app.mouse_y
def log(s): global scroll_y for line in splitlines_print(s): b.print(line, fg=Color.hsl(time() * 0.2, 0.5, 0.4)) b.print_pos = (0, b.print_pos[1] + 1) b.extend_to(0, b.print_pos[1] + 1) if autoscroll: scroll_y = b.print_pos[1] - a.screen.h
def do_box(buffer): if drag_start is None or mouse_pos is None: return x, y, w, h = corners_to_box(*drag_start, *mouse_pos) # draw_box(buffer, x, y, w, h, chars=BOX_CHARS_DOUBLE, fg=Color.hsl(perf_counter(), 1.0, 0.5)) draw_frame(buffer, x, y, w, h, chars=FRAME_CHARS_DOUBLE, fg=Color.hsl(perf_counter(), 1.0, 0.5))
def interact(place): nonlocal show_help show_help = False x = c_x y = c_y * 2 if x >= 0 and x < w and y >= 0 and y < h: if place: buf[y * w + x] = P(color=Color.hsl(perf_counter() * 0.25, 1, 0.5), **props["sand"]) else: buf[y * w + x] = P(**props["air"])
def on_frame(): app.screen.clear() # remove everything from the screen text = "Hello world, from termpixels!" for i, c in enumerate(text): f = i / len(text) color = Color.hsl(f + time(), 1, 0.5) # create a color from a hue value x = app.screen.w // 2 - len( text) // 2 # horizontally center the text offset = sin(time() * 3 + f * 5) * 2 # some arbitrary math y = round(app.screen.h / 2 + offset) # vertical center with an offset app.screen.print(c, x + i, y, fg=color) # draw the text to the screen buffer app.screen.update() # commit the changes to the screen
def redraw(app): white = Color.rgb(1, 1, 1) gray = Color.rgb(0.5, 0.5, 0.5) yellow = Color.rgb(1, 1, 0) app.screen.clear(bg=Color(0, 0, 0)) app.screen.fill(1, 1, app.screen.w - 2, app.screen.h - 2, bg=Color.rgb(0.2, 0.2, 0.2)) app.screen.print("Termpixels version {}\n".format(termpixels.__version__), 2, 2, fg=white) app.screen.print("Detected terminal: ", x=2, fg=gray) app.screen.print(app.backend.terminal_name, fg=yellow) app.screen.print("\n") app.screen.print("Color support: ", x=2, fg=gray) app.screen.print(app.backend.color_mode, fg=yellow) app.screen.print("\n") steps = app.screen.w - 4 for x in range(steps): app.screen.print(" ", x=x + 2, bg=Color.hsl(x / steps, 1, 0.5)) app.screen.print("\n") app.screen.print("Last keypress: ", x=2, fg=gray) app.screen.print(repr(app.key), fg=white) app.screen.print("\n") app.screen.print("Last render time: ", x=2, fg=gray) app.screen.print("{:.1f}ms".format(app.screen._update_duration * 1000), fg=white) app.screen.print("\n") if app.mouse: app.screen.print(" ", app.mouse.x, app.mouse.y, bg=white) app.screen.update()
def on_frame(): for x, p in enumerate(cols): # update colors for i in range(int(p.length) + 1): col = Color.hsl(HUE / 360, 1, (1 - i / p.length)**2) a.screen.at(x, int(p.pos - i + p.speed), clip=True).fg = col # render new characters for i in range(int(p.speed) + 1): pos = p.pos + i a.screen.print(" ", x, int(pos - p.length), fg=Color.rgb(0, 0, 0)) a.screen.print(randchar(), x, int(pos)) # randomly change a character a.screen.print(randchar(), x, int(p.pos + p.speed - randint(1, int(p.length)))) # move particle p.pos += p.speed if p.pos - p.length >= a.screen.h: cols[x] = Particle() a.screen.update()
def on_frame(): w = app.screen.w h = app.screen.h colormap = [Color.rgb(0,0,0) for x in range(w) for y in range(h * 2)] dx = app.mouse_x - app.mouse_px dy = app.mouse_y - app.mouse_py l = math.sqrt(dx ** 2 + dy ** 2) d = math.atan2(dy, dx) j = min(25, int(l)) * 6 for i in range(j): f = i / j ll = l* random.uniform(0.25, 0.5) dd = d + random.uniform(-0.25, 0.25) vx = ll * math.cos(dd) vy = ll * math.sin(dd) app.particles.append(Particle(app.mouse_px + dx * f, app.mouse_py + dy * f, vx, vy)) app.screen.clear() for i, p in enumerate(app.particles): col = Color.hsl(i/len(app.particles), 1.0, 0.5) steps = math.hypot(p.vx, p.vy) for step in range(int(steps + 1)): f = step / steps px = int(p.x - p.vx * f) py = int(p.y - p.vy * f) if px >= 0 and py >= 0 and px < w and py < h * 2: colormap[py * w + px] += RED p.update() if p.x < 0 or p.y < 0 or p.x >= w or p.y >= h * 2: app.particles.remove(p) draw_colormap_2x(app.screen, colormap, 0, 0, w=w, h=h * 2) app.screen.update() app.mouse_px = app.mouse_x app.mouse_py = app.mouse_y