Shell写的贪吃蛇游戏(转摘)
超强
游戏键:
上:w, i
下:s, k
左:a, j
右:d, l
退出:q
#!/bin/bash
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 [[ != "--showsnake" ]]
then
eval " --showsnake "$$"&"
trap "exit" TERM
trap "AllExit" INT
pidShowSnake=$!
DetectInput
exit
else
pidCtlSnake=
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
do
(( iMap[$i] = 0 ))
done
function RandomHeader
{
(( iNew = RANDOM % ( iTotalBox - $ ) ))
for (( iNewP = 0, i = 0; iNewP
do
if (( $ != 1 )); then (( i++ )); fi
done
while (( $ == 1 )); do (( iNewP++ )); done
(( iNewX = iNewP % iWidth ))
(( iNewY = (iNewP - iNewX) / iWidth ))
echo -ne "[1m[35m"
(( pX = 2 * iNewX + iLeft + 1 ))
(( pY = iNewY + iTop + 1 ))
echo -ne "["$pY";"$pX"H[]"
echo -ne "["$iCursor";1H"
echo -ne "[0m"
}
function InitDraw
{
clear
(( iTop = 1 ))
(( iBottom = iTop + iHeight + 1 ))
(( iLeft = 1 ))
(( iRight = iLeft + iWidth + iWidth + 1 ))
(( iCursor = iBottom + 1 ))
echo -ne "[1m[32m"
for (( i = iLeft + 1; i
do
echo -ne "["$iTop";"$i"H="
echo -ne "["$iBottom";"$i"H="
done
for (( i = iTop; i
do
echo -ne "["$i";"$iLeft"H|"
echo -ne "["$i";"$iRight"H|"
done
echo -ne "["$iCursor";1H"
echo -ne "[0m"
echo -ne "[1m[33m"
for (( i = 0; i
do
(( pX = 2 * $ + iLeft + 1 ))
(( pY = $ + iTop + 1 ))
(( pM = $ * iWidth + $ ))
(( iMap[$pM] = 1 ))
echo -ne "["$pY";"$pX"H[]"
#echo $
done
echo -ne "["$iCursor";1H"
echo -ne "[0m"
RandomHeader
}
function ShiftSnake
{
(( iLastP = iS - 1 ))
if (( iLastP
if (( iDir == 1 )) #left
then
(( iHX = $ - 1 ))
(( iHY = $ ))
elif (( iDir == 2 )) #up
then
(( iHX = $ ))
(( iHY = $ - 1 ))
elif (( iDir == 3 )) #right
then
(( iHX = $ + 1 ))
(( iHY = $ ))
elif (( iDir == 4 )) #down
then
(( iHX = $ ))
(( iHY = $ + 1 ))
fi
bOver=0
if (( iHX = iWidth || iHY >= iHeight )); then bOver=1;fi
if (( bOver == 0 )); then
if (( $ == 1 )); then bOver=1; fi
fi
if (( bOver == 1 ))
then
kill $pidCtlSnake
(( iBottom = iBottom + 1 ))
echo -e "["$iBottom";0HGame over! (Score: "$"00)[0m"
exit 0;
fi
#check if catch the new box
if (( iHX == iNewX && iHY == iNewY ))
then
for (( i = $; i > iS; i-- ))
do
(( iX[$i] = $ ))
(( iY[$i] = $ ))
done
(( iX[$iS] = iHX ))
(( iY[$iS] = iHY ))
(( iNextP = iS + 1 ))
if (( iNextP >= $ )); then iNextP=0; fi
echo -ne "\a"
RandomHeader
else
(( iNextP = iS + 1 ))
if (( iNextP >= $ )); then iNextP=0; fi
#clear snake tailer
(( pX = 2 * $ + iLeft + 1 ))
(( pY = $ + iTop + 1 ))
(( pM = $ * iWidth + $ ))
(( iMap[$pM] = 0 ))
echo -ne "["$pY";"$pX"H "
(( iX[$iS] = iHX ))
(( iY[$iS] = iHY ))
fi
#draw snake header
echo -ne "[1m[33m"
(( pX = 2 * iHX + iLeft + 1 ))
(( pY = iHY + iTop + 1 ))
(( pM = iHY * iWidth + iHX ))
(( iMap[$pM] = 1 ))
echo -ne "["$pY";"$pX"H[]"
echo -ne "["$iCursor";1H"
echo -ne "[0m"
(( iS = iNextP ))
}
trap "if (( iDir != 3 && iDir != 0 )); then iDirection=1; fi" 36
trap "if (( iDir != 4 )); then iDirection=2; fi" 37
trap "if (( iDir != 1 )); then iDirection=3; fi" 38
trap "if (( iDir != 2 )); then iDirection=4; fi" 39
trap "exit" 40
InitDraw
(( iNanoSec = iSpeed * 1000000 ))
iTime="1"`date +"%S%N"`
while [[ 1 ]]
do
usleep 100000
iTimeNew="1"`date +"%S%N"`
#avoid iTimeNew
if (( iTimeNew
then
(( iTime = iTime - 60000000000 ))
fi
#detect the time interval
if (( iTimeNew - iTime
iTime=$iTimeNew
iDir=$iDirection
if (( iDir != 0 ))
then
ShiftSnake
fi
done