这个是用shell脚步写的贪吃蛇,觉的好就放在上面了。这个是我的同学写的、
#!/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 [[ $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
(( iNewX = iNewP % iWidth ))
(( iNewY = (iNewP - iNewX) / iWidth ))
echo -ne "\033[1m\033[35m"
(( pX = 2 * iNewX + iLeft + 1 ))
(( pY = iNewY + iTop + 1 ))
echo -ne "\033["$pY";"$pX"H[]"
echo -ne "\033["$iCursor";1H"
echo -ne "\033[0m"
}
function InitDraw
{
clear
(( iTop = 1 ))
(( iBottom = iTop + iHeight + 1 ))
(( iLeft = 1 ))
(( iRight = iLeft + iWidth + iWidth + 1 ))
(( iCursor = iBottom + 1 ))
echo -ne "\033[1m\033[32m"
for (( i = iLeft + 1; i < iWidth + iWidth + iLeft + 1; i++ )); do
echo -ne "\033["$iTop";"$i"H="
echo -ne "\033["$iBottom";"$i"H="
done
for (( i = iTop; i < iHeight + iTop + 2; i++ ))
do
echo -ne "\033["$i";"$iLeft"H|"
echo -ne "\033["$i";"$iRight"H|"
done
echo -ne "\033["$iCursor";1H"
echo -ne "\033[0m"
echo -ne "\033[1m\033[33m"
for (( i = 0; i < ${#iX[@]}; i++ ))
do
(( pX = 2 * ${iX[$i]} + iLeft + 1 ))
(( pY = ${iY[$i]} + iTop + 1 ))
(( pM = ${iY[$i]} * iWidth + ${iX[$i]} ))
(( iMap[$pM] = 1 ))
echo -ne "\033["$pY";"$pX"H[]"
#echo ${iX[$i]}
done
echo -ne "\033["$iCursor";1H"
echo -ne "\033[0m"
RandomHeader
}
function ShiftSnake
{
(( iLastP = iS - 1 ))
if (( iLastP < 0 )); then ((iLastP = ${#iX[@]} - 1 )); fi
if (( iDir == 1 )) #left
then
(( iHX = ${iX[$iLastP]} - 1 ))
(( iHY = ${iY[$iLastP]} ))
elif (( iDir == 2 )) #up
then
(( iHX = ${iX[$iLastP]} ))
(( iHY = ${iY[$iLastP]} - 1 ))
elif (( iDir == 3 )) #right
then
(( iHX = ${iX[$iLastP]} + 1 ))
(( iHY = ${iY[$iLastP]} ))
elif (( iDir == 4 )) #down
then
(( iHX = ${iX[$iLastP]} ))
(( iHY = ${iY[$iLastP]} + 1 ))
fi
bOver=0
if (( iHX < 0 || iHY < 0 || iHX >= iWidth || iHY >= iHeight )); then bOver=1;fi
if (( bOver == 0 )); then
if (( ${iMap[iHY * iWidth + iHX]} == 1 )); then bOver=1; fi
fi
if (( bOver == 1 )); then
kill $pidCtlSnake
(( iBottom = iBottom + 1 ))
echo -e "\033["$iBottom";0HGame over! (Score: "${#iX[@]}"00)\033[0m"
exit 0;
fi
#check if catch the new box
if (( iHX == iNewX && iHY == iNewY )); then
for (( i = ${#iX[@]}; i > iS; i-- )); do
(( iX[$i] = ${iX[$i - 1]} ))
(( iY[$i] = ${iY[$i - 1]} ))
done
(( iX[$iS] = iHX ))
(( iY[$iS] = iHY ))
(( iNextP = iS + 1 ))
if (( iNextP >= ${#iX[@]} )); then iNextP=0; fi
echo -ne "\a"
RandomHeader
else
(( iNextP = iS + 1 ))
if (( iNextP >= ${#iX[@]} )); then iNextP=0; fi
#clear snake tailer
(( pX = 2 * ${iX[$iS]} + iLeft + 1 ))
(( pY = ${iY[$iS]} + iTop + 1 ))
(( pM = ${iY[$iS]} * iWidth + ${iX[$iS]} ))
(( iMap[$pM] = 0 ))
echo -ne "\033["$pY";"$pX"H "
(( iX[$iS] = iHX ))
(( iY[$iS] = iHY ))
fi
#draw snake header
echo -ne "\033[1m\033[33m"
(( pX = 2 * iHX + iLeft + 1 ))
(( pY = iHY + iTop + 1 ))
(( pM = iHY * iWidth + iHX ))
(( iMap[$pM] = 1 ))
echo -ne "\033["$pY";"$pX"H[]"
echo -ne "\033["$iCursor";1H"
echo -ne "\033[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 < iTime when new minute comes
if (( iTimeNew < iTime )); then
(( iTime = iTime - 60000000000 ))
fi
#detect the time interval
if (( iTimeNew - iTime < iNanoSec )); then continue; fi
iTime=$iTimeNew
iDir=$iDirection
if (( iDir != 0 )); then
ShiftSnake
fi
done