#game kernel variable x=2 #init snake x=2 y=2 y=2 direction=0 shead=1 #snake's head in snake[] stail=1 #snake's tail in snake[] mappoint=1 #point exactmap[] bottom state=on #snake run or stop run=off #if run=on,snake shadow is working displaypid="" controlpid=""
#game temp file;if your system's /tmp unwrite or unread, you can change to home cpath="/tmp/snake_ctrl_pid.tmp" dpath="/tmp/snake_disply_pid.tmp" vartmp="/tmp/snake_var_tmpfile.tmp"
#judge mapline | or -- if [[ "$line_row" == "1" ]] then for (( j = 0; j <= long; j++ )) do (( ytmp++ )) exactmap[$mappoint]="$ytmp $xtmp" (( mappoint++ )) done else for (( k = 0; k <= long; k++ )) do (( xtmp += 2 )) exactmap[$mappoint]="$ytmp $xtmp" (( mappoint++ )) done fi done }
#show map function Show_map () {
local mapxy="";blue=46
Exact_map
for (( i = 1; i < mappoint; i++ )) do eval Square $blue ${exactmap[$i]} '[]' done }
#test snake is ok ? function Test_snake () {
#snake self for (( i = 1; i <= length; i++ )) do if [[ "${snake[$i]}" == "$y $x" ]] then Dead fi done #borderline if [[ $x -lt 2 || $x -gt 79 || $y -lt 2 || $y -gt 18 ]] then Dead fi #map line for (( i = 0; i < mappoint; i++ )) do if [[ "${exactmap[$i]}" == "$y $x" ]] then Dead fi done }
#eat function Eat () {
local fruitxy="";xyvalue="";nowarray=""
for (( i = 0; i < 8; i++ )) do fruitxy="$(printf "\${fruit%s[$i]}" $level)" xyvalue="$(eval echo $fruitxy)"
if [[ "$xyvalue" = "$y $x" ]] then nowarray="$(printf "fruit%s[$i]=" $level)" eval $nowarray"" (( score++ )) (( fruitspare-- )) Draw_ls fi done if [[ $fruitspare == 0 ]] then Next_level fi }
#if snake dead function Dead () {
state=off
if (( "$life" == "0" )) then kill -$gameover $controlpid else (( life-- )) Info "SnakeDead""OH!shit!You are a idiot!""F**k You" sleep 1 New_game fi }
#next level function Next_level () {
(( level++ )) (( length += 6 )) if [[ $level -gt 6 ]] then Info "Well Done"" WOW!Congratulation! ""Thank You" sleep 4 kill -$gameexit $controlpid else Info "Well Done""Level Update! Go Level $level"".Loading." sleep 3 New_game fi }
#newgame function New_game () {
kill -9 $displaypid >/dev/null 2>&1
if [[ "$1" == "over" ]] then exec $0 else echo "$level $score $life $length $runtime" > $vartmp exec $0 display fi }
#game over function Game_over () {
local y_n
Info "Game Over""Do you want replay?""Thank You"
while read -s -n 1 y_n do case $y_n in [yY] ) New_game over ;; [nN] ) Game_exit ;; * ) continue ;; esac done }
#main function Main () {
local green=42;count=0
case $direction in "$up" ) (( y-- )) ;; "$down" ) (( y++ )) ;; "$left" ) (( x -= 2 )) ;; "$right" ) (( x += 2 )) ;; *): ;; esac Test_snake Eat
#go go go Square $green $y $x \#\# snake[$shead]="$y $x" (( shead++ ))
if [[ "$shead" == "$length" ]] then shead=1 run=on #snake shadow run fi
#snake shadow,it can erase snake's tail,otherwise,snake will very long! if [[ "$run" == "on" ]] then Square 0 ${snake[$stail]} "" (( stail++ )) if [[ "$stail" == "$length" ]] then stail=1 fi fi }
#state change:off=snake stop.on=snake run function State_change () { if [[ $state == "on" ]] then state=off else state=on fi } #display function Display () {
echo $$ > $dpath read controlpid < $cpath if [[ -e $vartmp ]] then read level score life length runtime< $vartmp rm -f $vartmp fi
#drow all Init Screen Draw_ls Show_fruits Show_map Main #game main loop while : do if [[ ( "$state" == "on" ) && ( "$direction" != "0" ) ]] then Main sleep $runtime fi done }