Ok, next one.
Pipes in the terminal, like an old Microsoft windows machine screensaver.
Sparsely commented, you'll need to pick it apart and learn. The function
names are self explanatory.
There is math in this one, random numbers, loops, arrays. As you see, some
of the variables are not even assigned before usage. That's ok. They will
get a value when you make an equation with them.
Minimal script, you'll need to Ctrl+C to get out of it.
Code:
#!/usr/bin/env bash
pipes=("┃┏ ┓┛━┓ ┗┃┛┗ ┏━")
PIPES=()
#Number of pipes
p=1
#Get term size
resize() {
w=$(tput cols)
h=$(tput lines)
}
init_screen() {
stty -echo
tput smcup
tput civis
tput clear
trap cleanup HUP TERM
resize
trap resize SIGWINCH
}
main() {
#Set term bg black
tput setab 0
#Pipe colors, leave out black
C=(1 2 3 4 5 6 7)
CN=${#C[@]}
for ((i = 0; i < CN; i++)); do
E[i]+=$(tput setaf ${C[i]})
done
for ((i = 0; i < ${#pipes[@]}; i++)); do
for ((j = 0; j < 16; j++)); do
PIPES+=("${pipes[i]:j:1}")
done
done
init_screen
t=0
while :; do
read -t 0.02 -n 1 2>/dev/null
for ((i = 0; i < p; i++)); do
((l[i] % 2)) && ((x[i] += -l[i] + 2, 1)) || ((y[i] += l[i] - 1))
# Loop at edge
((!0 && (x[i] >= w || x[i] < 0 || y[i] >= h || y[i] < 0))) \
&& { c[i]=${E[CN * RANDOM / 32768]}; ((v[i] = V[VN * RANDOM / 32768])); }
((x[i] = (x[i] + w) % w, y[i] = (y[i] + h) % h))
((
n[i] = 13 * RANDOM / 32768 - 1,
n[i] = n[i] >= 0 ? l[i] : l[i] + (2 * (RANDOM % 2) - 1),
n[i] = (n[i] + 4) % 4
))
printf '\e[%d;%dH%s%s' $((y[i] + 1)) $((x[i] + 1)) ${c[i]} \
"${PIPES[v[i] * 16 + l[i] * 4 + n[i]]}"
l[i]=${n[i]}
t=$(($t + 1))
done
((t > 3000)) && tput reset && tput civis && t=0 || ((t++))
done
}
main "$@"
Now if you want the challenge.
1. Change the speed that the pipes are drawn.
2. Let more or fewer pipes be drawn before reset.
3. Change the pipe sizes.
4. Have the pipes loop before the edge of the terminal is reached.
5. Change the number of pipes drawn at once.
6. Whatever else you want to do with it.