Sunday, November 29, 2015

Bash animations

Animations in various languages.


1. The donut.


$ gcc -lm donut.c -o donut


[code]
           k;double sin()
         ,cos();main(){float A=
       0,B=0,i,j,z[1760];char b[
     1760];printf("\x1b[2J");for(;;
  ){memset(b,32,1760);memset(z,0,7040)
  ;for(j=0;6.28>j;j+=0.07)for(i=0;6.28
 >i;i+=0.02){float c=sin(i),d=cos(j),e=
 sin(A),f=sin(j),g=cos(A),h=d+2,D=1/(c*
 h*e+f*g+5),l=cos      (i),m=cos(B),n=s\
in(B),t=c*h*g-f*        e;int x=40+30*D*
(l*h*m-t*n),y=            12+15*D*(l*h*n
+t*m),o=x+80*y,          N=8*((f*e-c*d*g
 )*m-c*d*e-f*g-l        *d*n);if(22>y&&
 y>0&&x>0&&80>x&&D>z[o]){z[o]=D;;;b[o]=
 ".,-~:;=!*#$@"[N>0?N:0];}}/*#****!!-*/
  printf("\x1b[H");for(k=0;1761>k;k++)
   putchar(k%80?b[k]:10);A+=0.04;B+=
     0.02;}}/*****####*******!!=;:~
       ~::==!!!**********!!!==::-
         .,~~;;;========;;;:~-.
             ..,--------,*/

[/code]

Other interesting things:
https://github.com/szantaii/bash-weather
http://www.a1k0n.net/2006/09/15/obfuscated-c-donut.html
http://soomka.com/
https://github.com/radare/tiv
https://github.com/thewtex/tmux-mem-cpu-load
https://github.com/gryftir/tmux-networkspeed

2. Yahoo

$ gcc -lm yahoo.c -o yahoo

[code]

c,p,i,j,n,F=40,k,m;float a,x,y,S=0,V=0;main(){for(;F--;usleep(50000),F?puts(
"\x1b[25A"):0)for(S+=V+=(1-S)/10-V/4,j=0;j<72;j+=3,putchar(10))for(i=0;x=S*(
i-27),i++<73;putchar(c[" ''\".$u$"]))for(c=0,n=3;n--;)for(y=S*(j+n-36),k=0,c
^=(136*x*x+84*y*y<92033)<<n,p=6,m=0;m<8;k++["<[\\]O=IKNAL;KNRbF8EbGEROQ@BSX"
"XtG!#t3!^"]/1.16-68>x*cos(a)+y*sin(a)?k=p,p="<AFJPTX"[m++]-50:k==p?c^=1<<n,
m=8:0)a=(k["O:85!fI,wfO8!yZfO8!f*hXK3&fO;:O;#hP;\"i[by asloane"]-79)/14.64;}
[/code]

3. Explode



$ python explode.py

[code]

!/usr/bin/python

import sys, math, time, random

def distance(x, y):
    # ((y - y_center) * 2) -> correct ratio for a true circle
    return math.ceil(math.sqrt(((x - 40) ** 2) + (((y - 12) * 2) ** 2)))

def star(radiuses):
    for r in radiuses:
        #~ clear screen: http://stackoverflow.com/a/2084521/2257664
        print chr(27) + "[2J"

        # width
        for y in range(24):
            # height
            for x in range(80):
                d = distance(x, y)

                #~ border
                if (d == r):
                    sys.stdout.write('*')
                #~ inside the star
                elif (d < r):
                    if (r > 35):
                        sys.stdout.write(' ')
                    elif (r > 25) and ((d % 2) != 0):
                        sys.stdout.write('-')
                    elif (r > 15) and ((d % 2) == 0):
                        sys.stdout.write(' ')
                    else :
                        sys.stdout.write(random.choice('****#@'))
                #~ space outside the star
                else:
                    sys.stdout.write(' ')
            print
        time.sleep(0.1)

star(range(0, 12) + range(10, 0, -1) + range(0, 50))

[/code]

3. car

$ gcc car.c -std=c99  -o car

[code]
 #include<stdio.h>
#include<stdlib.h>

#ifdef _WIN32
#define CLEAR "cls"
#define SLEEP "ping -n 1 localhost > nul"
#else //In any other OS
#define CLEAR "clear"
#define SLEEP "ping -c 1 localhost > nul"
#endif

void printline(char *, int);
void drawcar(int);

void printline(char *line, int no)
{
int i;
for( i = 0; i < no; i++)
  printf(" ");
printf("%s\n", line);
}

void drawcar(int no_of_spaces)
 {
 char *line1 = "       .--------.";
 char *line2 = " ____/_____|___ \\___";
 char *line3 = " O    _   - |   _   ,*";
 char *line4 = " '--(_)-------(_)--'";
 printline(line1, no_of_spaces);
 printline(line2, no_of_spaces);
 printline(line3, no_of_spaces);
 printline(line4, no_of_spaces);
}
int main()
  {
  int i;
  for (int i = 0; i < 50; i++)
    {
       system(SLEEP);
       system(CLEAR);
       drawcar(i);
    }
  }

[/code]

4. The cat.



$ chmod +x cat
$ ./cat

[code]

#!/bin/bash
#  --------------------------------------------------------------------------
#  (c) 2012 BruXy            Version: 1.0             http://bruxy.regnet.cz/
# downloaded with permission 
#  --------------------------------------------------------------------------
# switch off cursor, set blue background, clear screen
echo -e "\E[?25l\E[44m\E[2J"

# Play music
M=/tmp/cat_orig.mp3
[ ! -f $M ] && wget http://nyan.cat/music/original.mp3 -O $M >/dev/null 2>&1
while true; do mplayer $M > /dev/null 2>&1; done &

# easy way to reset terminal and exit subprocesses
trap "reset;killall nyan_cat.sh" 2

## Nyan cat ANSI picture data, my own creation :P, size 38 x 11
# Compressed version of picture
D="H4sIAHUNPlAAA22Q2w7CIAyG730rC1IGEs3itoS7cYlLvFGf37YcPMQvo5DxpT+pMYLb/8OVSxNv
Aiq1Db9sSmG1TgKqdhXqktKtsyBW6F08gB/EcgAUG2eAeRxbryqi1hq89yvQgfrFZ6If8E7sVoet
aVqWlNjqgYGlA5ckG5r4EGqvHmgtGeWzlqwLc6dJHD8Sr/IMQadMiTxR0IDrN/RUUjDnLJOghYn9
UgtyzvTcNvvdCw/8WhWjAQAA"

# decompress it to $C
C=`echo "$D" | base64 -d | zcat`

# hash field with 'pixel' atributes
declare -A B

B=([A]=43\;30m▀ [a]=43\;30m▄ [B]=47\;30m▀ [b]=47\;30m▄ [C]=43m\  [D]='47m '
[E]=1\;40\;37m▀\\E[0m [F]=44m\  [G]=40m\  [H]=1\;31m█\\E[0m [I]=0\;35m█
[J]=44\;30m▄ [j]=44\;30m▀ [K]=45\;30m▄ [k]=45\;30m▀ [L]=43\;35m▀
[l]=43\;35m▄ [M]=45\;31m▄ [O]=31m█ [o]=44\;31m▄ [P]=33m█ 
[R]=42\;33m▀ [S]=42\;32m█ [s]=46\;32m▀ [T]=46\;36m█ [t]=46\;30m▄
[U]=45\;35m█ [u]=46\;35m▀ [V]=43\;30m▄ [v]=42\;30m▀ [W]=45\;30m▀
[X]=43\;32m▄ [x]=32m█ [c]=44\;36m▀ [d]=42\;35m▄ [e]=41\;33m▄ [Z]=44m\\n)

# get size of terminal window
read X Y <<< `tput cols; tput lines`

# some terminals have problem with background color change
# this fills screen with spaces and fill my signature to bottom left
printf "%*s" $[X*Y] BruXy

##################
### Draw Nyan Cat
##################

# set position to the center
xp=$[(X-37)/2]
yp=$[(Y-11)/2]

# go through image data and draw them to specified position

j=1 #row    ... y
k=1 #column ... x
for i in {0..417} #38*11-1 image size
do
    p=${C:$i:1} # get pixel
    # put pixel to given position \E[y;xH
    echo -en "\E[$[j+yp];$[k+xp]H\E[${B[$p]}"
    # if pixel Z is found go to next row (pixel count worked strange)
    k=$[k++,k%38]; [ $p = 'Z' ] && : $[j++]
done

###################
### Star animation
###################

# Field with stars animation
S=("       " "      " "       " "       "
"       " "  ▄▀▄  " "      " "       "
"      " "      " "▀▀  ▀▀" "      "
"      " "     " "     " "    "
"      " "       " "     " "      ")

# function for star drawing
# $1 .. column, $2 .. row position
s () {
    echo -en "\E[1;37m" # set bright white
    for j in {0..4} # for each line
    do
        # draw one star frame of animation
        for i in {0..3}
        do
            echo -en "\E[$[i+$1];$[$2+m]H\E[1;37m${S[$[i+4*j]]}\E[0;44m"
        done

        sleep 0.2

        # erase star
        for i in {0..3}
        do
            echo -en "\E[$[i+$1];$[$2+m]H\E[44m${S[0]}"
        done
        : $[m-=5] # move the next frame 5 columns to the left
    done
}

# count star positions according to nyan cat position 
# -3,+3 best constant, I've played with those numbers
# to get best position of stars and do not destroy the cat :)
my=$[(Y-11)/2 - 3]
My=$[my+11 + 3]

while true
do
    for i in $(eval echo {1..$my..4}) # each position 4 rows difference
    do
        # draw top star, animation has 20 columns length, so generated
        # for lowest start position columns is 0+20
        (s $i $[20 + $RANDOM % (X - 20)]) &
        # draw bottom star 
        (s $[i+My] $[20 + $RANDOM % (X - 20)]) &
    done
    sleep 0.5
done &

######################
### Draw rainbow flag
######################

# oOPXxUuT .. top wave, OePxdUTc bottom wave
A="oOPXxUuTOePxdUTc";

# Flag waving:
# $c .. flip flop when counter $i modulo 5 is zero,
#       then it adress top or bottom wave
while true
do
    : $[c^=1]
    for x in `eval echo {1..$[xp]}`
    do
        if [ $[i++ % 5] -eq 4 ]; then : $[c^=1]; fi;
        for y in {0..7}
        do
            echo -en "\E["$[yp+y+2]";${x}H\E[${B[${A:$[y+(c*8)]:1}]}\E[0m"
        done
    done
    sleep 0.5
done
[/code]

5. Matrix



$ chmod +x matrix
$ ./matrix

[code]
 #!/bin/bash
echo -e "\033[2J\033[?25l"R=`tput lines` C=`tput cols`;: $[R--] ; while true 
do ( e=echo\ -e s=sleep j=$[RANDOM%C] d=$[RANDOM%R];for in `eval $e {1..$R}`;
do c=`printf '\\\\0%o' $[RANDOM%57+33]` ### http://bruxy.regnet.cz/web/linux ###
$e "\033[$[i-1];${j}H\033[32m$c\033[$i;${j}H\033[37m"$c; $s 0.1;if [ $i -ge $d ]
then $e "\033[$[i-d];${j}";fi;done;for in `eval $e {$[i-d]..$R}`; #[mat!rix]
do echo -e "\033[$i;${j}";$s 0.1;done)& sleep 0.05;done #(c) 2011 -- [ BruXy ]
[/code]

No comments:

Post a Comment