) x=$x+1 ;;
"q") ret ;;
esac
# 吃到食物
if [[ $x -eq $randomx && $y -eq $randomy ]]; then
((len++))
((res++));
x_arr[$len]=$x
y_arr[$len]=$y
gen_food
return
fi
if [[ $x -le 1 || $y -le 1 || $x -ge 60 || $y -ge 27 ]]; then
echo "出范围"
ret;
fi
for ((i = 2; i <= $len; i++)); do
if [[ $x -eq ${x_arr[$i]} && $y -eq ${y_arr[$i]} ]]; then
echo "吃到自己"
ret;
fi
done
for ((i = 0; i <= $len; i++)); do
x_arr[$i]=${x_arr[$i+1]}
y_arr[$i]=${y_arr[$i+1]}
done
echo -e "\033[${y_arr[0]};${x_arr[0]}H\033[40m \033[0m"
x_arr[$len]=$x
y_arr[$len]=$y
}
gen_food() {
let flag=1
while [ $flag -eq 1 ]; do
let randomx=$(($RANDOM%57+2))
let randomy=$(($RANDOM%24+2))
# 判断食物是否在蛇身
for ((i=0;i<len;i++)); do
if [[ ${x_arr[$i]} -eq $randomx && ${y_arr[$i]} -eq $randomy ]]; then
break
fi
done
if [ $i -eq $len ]; then
flag=0
fi
done
}
ret() {
echo -e "\033[0m"
echo -e "\033[?25h"
exit
}
init
set_show
while :; do
olddir=$dir
if [ $res -lt 5 ]; then
level=0
elif [ $res -lt 10 ]; then
level=1
elif [ $res -lt 15 ]; then
level=2
elif [ $res -lt 20 ]; then
level=3
elif [ $res -lt 25 ]; then
level=4
fi
if ! read -n 1 -t ${level_arr[level]} -s dir; then
dir=$olddir
fi
# 不能180反向
if [[ $dir = "w" && $olddir = "s" ]]; then
dir=$olddir
fi
if [[ $dir = "s" && $olddir = "w" ]]; then
dir=$olddir
fi
if [[ $dir = "a" && $olddir = "d" ]]; then
dir=$olddir
fi
if [[ $dir = "d" && $olddir = "a" ]]; then
dir=$olddir
fi
move
set_show
done
案例17:俄罗斯方块
[root@kube-node1 ~]# cat 俄罗斯方块.sh
#!/bin/bash
APP_NAME="${0##*[\\/]}"
APP_VERSION="1.0"
#颜色定义
iSumColor=7 #颜色总数
cRed=1 #红色
cGreen=2 #绿色
cYellow=3 #黄色
cBlue=4 #蓝色
cFuchsia=5 #紫红色
cCyan=6 #青色(蓝绿色)
cWhite=7 #白色
#位置与大小
marginLeft=3 #边框左边距
marginTop=2 #边框上边距
((mapLeft=marginLeft+2)) #棋盘左边距
((mapTop=$marginTop+1)) #棋盘上边距
mapWidth=10 #棋盘宽度
mapHeight=15 #棋盘高度
#颜色设置
cBorder=$cGreen
cScore=$cFuchsia
cScoreva lue=$cCyan
#控制信号
#游戏使用两个进程,一个用于接收输入,一个用于游戏流程和显示界面;
#当前者接收到上下左右等按键时,通过向后者发送signal的方式通知后者。
sigRotate=25 #向上键
sigLeft=26
sigRight=27
sigDown=28
sigAllDown=29 #空格键
sigExit=30
#方块定义,7大类19种样式
#前8位为方块坐标,后2位为方块刚出现的时候的位置
box0_0=(0 0 0 1 1 0 1 1 0 4)
box1_0=(0 1 1 1 2 1 3 1 0 3)
box1_1=(1 0 1 1 1 2 1 3 -1 3)
box2_0=(0 0 1 0 1 1 2 1 0 4)
box2_1=(0 1 0 2 1 0 1 1 0 3)
box3_0=(0 1 1 0 1 1 2 0 0 4)
box3_1=(0 0 0 1 1 1 1 2 0 4)
box4_0=(0 2 1 0 1 1 1 2 0 3)
box4_1=(0 1 1 1 2 1 2 2 0 3)
box4_2=(1 0 1 1 1 2 2 0 -1 3)
box4_3=(0 0 0 1 1 1 2 1 0 4)
box5_0=(0 0 1 0 1 1 1 2 0 3)
box5_1=(0 1 0 2 1 1 2 1 0 3)
box5_2=(1 0 1 1 1 2 2 2 -1 3)
box5_3=(0 1 1 1 2 0 2 1 0 4)
box6_0=(0 1 1 0 1 1 1 2 0 3)
box6_1=(0 1 1 1 1 2 2 1 0 3)
box6_2=(1 0 1 1 1 2 2 1 -1 3)
box6_3=(0 1 1 0 1 1 2 1 0 4)
iSumType=7 #方块类型总数
boxStyle=(1 2 2 2 4 4 4) #各种方块旋转后可能的样式数目
iScoreEachLevel=50 #提升一个级别需要的分数
#运行时数据
sig=0 #接收到的signal
iScore=0 #总分
iLevel=0 #速度级
boxNext=() #下一个方块
iboxNextColor=0 #下一个方块的颜色
iboxNextType=0 #下一个方块的种类
iboxNextStyle=0 #下一个方块的样式
boxCur=() #当前方块的位置定义
iBoxCurColor=0 #当前方块的颜色
iBoxCurType=0 #当前方块的种类
iBoxCurStyle=0 #当前方块的样式
boxCurX=-1 #当前方块的x坐标位置
boxCurY=-1 #当前方块的y坐标位置
map=() #棋盘图表
#初始化所有背景方块为-1, 表示没有方块
for ((i = 0; i < mapHeight * mapWidth; i++))
do
map[$i]=-1
done
#接收输入的进程的主函数
function RunAsKeyReceiver()
{
local pidDisplayer key aKey sig cESC sTTY
pidDisplayer=$1
aK