|
|
@ -4,7 +4,7 @@ |
|
|
|
_STTY=$(stty -g) # Save current terminal setup |
|
|
|
printf "\e[2J" # clear screen, set cursos at beginning |
|
|
|
stty -echo -icanon # Turn off line buffering |
|
|
|
printf '\e[?25h' |
|
|
|
printf '\e[?25h' # Turn on the cursor |
|
|
|
|
|
|
|
# VARIABLES |
|
|
|
typeset -A IMAGE |
|
|
@ -17,6 +17,7 @@ snake='═' |
|
|
|
vertical=false |
|
|
|
forward=true |
|
|
|
upward=true |
|
|
|
pen_down=true |
|
|
|
|
|
|
|
function get_size() { |
|
|
|
# Get terminal size ('stty' is POSIX and always available). |
|
|
@ -34,6 +35,7 @@ function start_the_snake() { |
|
|
|
IMAGE["${i};${j}"]=' ' ; printf ${IMAGE["${i};${j}"]} |
|
|
|
done |
|
|
|
done |
|
|
|
printf '\e[2J\e[H' ; printf '\e[0;24H' |
|
|
|
} |
|
|
|
|
|
|
|
function save_the_snake() { |
|
|
@ -56,6 +58,7 @@ function show_the_snake() { |
|
|
|
printf ${IMAGE["${i};${j}"]} |
|
|
|
done |
|
|
|
done |
|
|
|
printf '\e[%s;%sH' "$((Y+1))" "$((X+1))" |
|
|
|
} |
|
|
|
function at_exit() { |
|
|
|
printf "\e[?9l" # Turn off mouse reading |
|
|
@ -75,10 +78,14 @@ function move_snake_up() { |
|
|
|
else |
|
|
|
snake='╚' |
|
|
|
fi |
|
|
|
IMAGE["${Y};${X}"]=$snake ; Y=$((Y-1)) ; vertical=true ; upward=true |
|
|
|
else |
|
|
|
snake='║' ; IMAGE["${Y};${X}"]=$snake ; Y=$((Y-1)) |
|
|
|
snake='║' |
|
|
|
fi |
|
|
|
#draw nothing it the pen is not down |
|
|
|
if [ "$pen_down" = false ] ; then |
|
|
|
snake=' ' |
|
|
|
fi |
|
|
|
IMAGE["${Y};${X}"]=$snake ; Y=$((Y-1)) ; vertical=true ; upward=true |
|
|
|
} |
|
|
|
|
|
|
|
function move_snake_down() { |
|
|
@ -88,10 +95,14 @@ function move_snake_down() { |
|
|
|
else |
|
|
|
snake='╔' |
|
|
|
fi |
|
|
|
IMAGE["${Y};${X}"]=$snake ; Y=$((Y+1)) ; vertical=true ; upward=false |
|
|
|
else |
|
|
|
snake='║' ; IMAGE["${Y};${X}"]=$snake ; Y=$((Y+1)) |
|
|
|
snake='║' |
|
|
|
fi |
|
|
|
#draw nothing it the pen is not down |
|
|
|
if [ "$pen_down" = false ] ; then |
|
|
|
snake=' ' |
|
|
|
fi |
|
|
|
IMAGE["${Y};${X}"]=$snake ; Y=$((Y+1)) ; vertical=true ; upward=false |
|
|
|
} |
|
|
|
|
|
|
|
function move_snake_forward() { |
|
|
@ -101,10 +112,14 @@ function move_snake_forward() { |
|
|
|
else |
|
|
|
snake='╚' |
|
|
|
fi |
|
|
|
IMAGE["${Y};${X}"]=$snake ; X=$((X+1)) ; vertical=false ; forward=true |
|
|
|
else |
|
|
|
snake='═' ; IMAGE["${Y};${X}"]=$snake ; X=$((X+1)) |
|
|
|
snake='═' |
|
|
|
fi |
|
|
|
#draw nothing it the pen is not down |
|
|
|
if [ "$pen_down" = false ] ; then |
|
|
|
snake=' ' |
|
|
|
fi |
|
|
|
IMAGE["${Y};${X}"]=$snake ; X=$((X+1)) ; vertical=false ; forward=true |
|
|
|
} |
|
|
|
|
|
|
|
function move_snake_backward() { |
|
|
@ -114,15 +129,20 @@ function move_snake_backward() { |
|
|
|
else |
|
|
|
snake='╝' |
|
|
|
fi |
|
|
|
IMAGE["${Y};${X}"]=$snake ; X=$((X-1)) ; vertical=false ; forward=false |
|
|
|
else |
|
|
|
snake='═' ; IMAGE["${Y};${X}"]=$snake ; X=$((X-1)) |
|
|
|
snake='═' |
|
|
|
fi |
|
|
|
#draw nothing it the pen is not down |
|
|
|
if [ "$pen_down" = false ] ; then |
|
|
|
snake=' ' |
|
|
|
fi |
|
|
|
IMAGE["${Y};${X}"]=$snake ; X=$((X-1)) ; vertical=false ; forward=false |
|
|
|
} |
|
|
|
|
|
|
|
# Main # |
|
|
|
get_size |
|
|
|
start_the_snake |
|
|
|
echo |
|
|
|
while read -s -k 1 cr; |
|
|
|
do |
|
|
|
case "$cr" in |
|
|
@ -130,6 +150,8 @@ do |
|
|
|
s) move_snake_down ; show_the_snake ;; |
|
|
|
a) move_snake_backward ; show_the_snake ;; |
|
|
|
d) move_snake_forward ; show_the_snake ;; |
|
|
|
t) pen_down=true ;; |
|
|
|
y) pen_down=false ;; |
|
|
|
q) save_the_snake && at_exit ;; |
|
|
|
esac |
|
|
|
done |
|
|
|