#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
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
}
#问题:这是一个什么样的函数,主要作用如何?
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
}
#问题:说明此函数的判断条件是什么意思?各导致什么结果?是不是输了退出,赢了继续?SLEEP是什么意思?
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 $(dirname $0)/$(basename $0)
else
echo "$level $score $life $length $runtime" > $vartmp
exec $(dirname $0)/$(basename $0) display
fi
}
#game over
function Game_over () {
local y_n
Info "Game Over" "Do you want replay?<y/n>" "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
#问题:此处的判断语句是什么意思,导致什么结果?是不是过关加长蛇身体长度?
if [[ "$run" == "on" ]]
then
Square 0 ${snake[$stail]} " "
(( stail++ ))
if [[ "$stail" == "$length" ]]
then
stail=1
fi
fi
}
#问题:此处的函数是什么作用?
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
}