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