function DetectInput { while [[ 1 ]] do c="" read -n 1 c echo -ne "\r \r" if [[ $c == "a" || $c == "j" || $c == "A" || $c == "J" ]] then kill -36 $pidShowSnake elif [[ $c == "w" || $c == "i" || $c == "W" || $c == "I" ]] then kill -37 $pidShowSnake elif [[ $c == "d" || $c == "l" || $c == "D" || $c == "L" ]] then kill -38 $pidShowSnake elif [[ $c == "s" || $c == "k" || $c == "S" || $c == "K" ]] then kill -39 $pidShowSnake elif [[ $c == "q" || $c == "Q" ]] then echo Quit. kill -40 $pidShowSnake exit fi done }
function AllExit { kill -40 $pidShowSnake exit }
if [[ $1 != "--showsnake" ]] then eval $0" --showsnake "$$"&" trap "exit" TERM trap "AllExit" INT pidShowSnake=$! DetectInput exit else pidCtlSnake=$2 fi
echo sub
iSpeed=500 #time interval in millisecond iDirection=0 #1-left, 2-up, 3-right, 4-down iWidth=15 iHeight=15 iX=(6 7 8) iY=(7 7 7) iMap=() iS=0 #Start pointer in iX
(( iTotalBox = iWidth * iHeight )) for (( i = 0; i < iTotalBox; i++ )) do (( iMap[$i] = 0 )) done
function RandomHeader { (( iNew = RANDOM % ( iTotalBox - ${#iX[@]} ) )) for (( iNewP = 0, i = 0; iNewP < iTotalBox && i < iNew; iNewP++)) do if (( ${iMap[$iNewP]} != 1 )); then (( i++ )); fi done while (( ${iMap[$iNewP]} == 1 )); do (( iNewP++ )); done