Skip to content

Instantly share code, notes, and snippets.

@nvanderw
Created May 20, 2022 04:39
Show Gist options
  • Save nvanderw/1d85f1eac54995b193ca64c9bde41b89 to your computer and use it in GitHub Desktop.
Save nvanderw/1d85f1eac54995b193ca64c9bde41b89 to your computer and use it in GitHub Desktop.
Python Blessed terminal animation -- golfed and ungolfed
(i:=__import__,n:=255,p:=lambda a:print(a,end='',flush=True),t:=i('blessed').Terminal(),H:=0,w:=t.width,h:=t.height,p(t.clear+t.home),x:=1,y:=1,X:=1,Y:=1,u:=lambda v,p,m:v if 0<p<m-1else -v,p(t.hide_cursor),all((x:=x+X,y:=y+Y,X:=u(X,x,w),Y:=u(Y,y,h),R:=i('colorsys').hsv_to_rgb(H/n,1,1),p(t.move_xy(x,y)+t.bold+t.color_rgb(*map(lambda m:int(m*n),R))+"☻"),H:=H+7&n,i('time').sleep(.01))for _ in iter(int,1)))
from blessed import Terminal
from colorsys import hsv_to_rgb
from time import sleep
from random import randrange
# Print with no trailing newline
def printn(*args, **kwargs):
kwargs['end'] = ''
kwargs['flush'] = 'True'
print(*args, **kwargs)
term = Terminal()
printn(term.clear + term.home)
hue = 0
width, height = term.width, term.height
pos_x, pos_y = randrange(width), randrange(height)
vx, vy = 1, 1
with term.hidden_cursor():
while True:
u = lambda v,p,m:v if 0<p<(m-1)else -v
vx = u(vx, pos_x, width)
vy = u(vy, pos_y, height)
pos_x += vx
pos_y += vy
printn(term.move_xy(pos_x, pos_y))
red, green, blue = hsv_to_rgb(hue / 255, 1, 1)
color = term.color_rgb(int(red * 255), int(green * 255), int(blue * 255))
printn(term.bold(color("☻")))
hue = (hue + 7) & 0xff
sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment