前面我们已经可以随机产生俄罗斯方块了,今天我们让它随键盘而改变
- #!/bin/bash
- #七中不同的方块的定义
- #通过旋转,每种方块的显示的样式可能有几种
- box0=(0 0 0 1 1 0 1 1)
- box1=(0 2 1 2 2 2 3 2 1 0 1 1 1 2 1 3)
- box2=(0 0 0 1 1 1 1 2 0 1 1 0 1 1 2 0)
- box3=(0 1 0 2 1 0 1 1 0 0 1 0 1 1 2 1)
- box4=(0 1 0 2 1 1 2 1 1 0 1 1 1 2 2 2 0 1 1 1 2 0 2 1 0 0 1 0 1 1 1 2)
- box5=(0 1 1 1 2 1 2 2 1 0 1 1 1 2 2 0 0 0 0 1 1 1 2 1 0 2 1 0 1 1 1 2)
- box6=(0 1 1 1 1 2 2 1 1 0 1 1 1 2 2 1 0 1 1 0 1 1 2 1 0 1 1 0 1 1 1 2)
- #把所有盒子放在box中
- box=(${box0[@]} ${box1[@]} ${box2[@]} ${box3[@]} ${box4[@]} ${box5[@]} ${box6[@]})
- #每个盒子在box中的偏移
- boxOffset=(0 1 3 5 7 11 15)
- #旋转次数
- rotateCount=(1 2 2 2 4 4 4)
- #颜色数组
- colourArry=(31 32 33 34 35 36 37)
- #选装类型
- rotateType=-1
- #盒子标号
- boxNum=-1
- #新盒子
- newBox=()
- #边缘距离
- top=3
- left=3
- #当前x,y坐标
- currentX=15
- currentY=2
- #信号
- SigA=20
- SigS=21
- SigD=22
- SigW=23
- sig=0
- #随机产生盒子
- function Random_Box()
- {
- #随机产生盒子号
- (( boxNum = $RANDOM % 7 ))
- #随机长生盒子的类型
- ((rotateType = $RANDOM % ${rotateCount[boxNum]}))
- #随机产生颜色
- ((colourNum = $RANDOM % ${#colourArry[*]}))
- #找到所在box中的起始位置
- ((j = ${boxOffset[boxNum]} * 8 + rotateType * 8))
- for(( i = 0 ;i < 8;i ++))
- do
- ((newBox[i] = ${box[j+i]}))
- done
- }
- function Draw_Box()
- {
- local i j x y
- if (($1 == 0))
- then
- for ((i = 0;i < 8;i += 2))
- do
- ((x = left + 3 * (currentX + ${newBox[i]})))
- ((y = top + currentY + ${newBox[i+1]}))
- echo -e "\033[${y};${x}H "
- done
- else
- echo -e "\033[${colourArry[$colourNum]}m\033[1m"
- for ((i = 0;i < 8;i += 2))
- do
- ((x = left + 3 * (currentX + ${newBox[i]})))
- ((y = top + currentY + ${newBox[i+1]}))
- echo -e "\033[${y};${x}H[*]"
- done
- fi
- echo -e "\033[0m"
- }
- function move_left()
- {
- local temp
- if (( currentX == 0 ))
- then
- return 1
- fi
-
- #先清除以前的方块
- Draw_Box 0
-
- #改变x坐标
- (( currentX -- ))
-
- #画出新的方块
- Draw_Box 1
- return 0
- }
- function move_right()
- {
- if ((currentX > 20 ))
- then
- return 1;
- fi
- #先清除以前的方块
- Draw_Box 0
-
- #改变x坐标
- (( currentX ++ ))
-
- #画出新的方块
- Draw_Box 1
- return 0;
- }
- #记录已经旋转的方块次数
- tempCount=0
- #按下w键旋转处理
- function box_rotate()
- {
- local start_post
-
- ((tempCount ++))
- #echo ${rotateCount[boxNum]}
- if ((tempCount >= ${rotateCount[boxNum]}))
- then
- ((tempCount = 0))
- fi
- #每个盒子在box中的始位置
- ((start_post = ${boxOffset[boxNum]} * 8 + tempCount * 8))
- for ((i = 0;i < 8;i ++))
- do
- ((newBox[i] = ${box[start_post+i]}))
- done
- return 0
- }
- function move_rotate()
- {
- if ((currentY == 0 ))
- then
- return 1;
- fi
- #先清除以前的方块
- Draw_Box 0
-
- #改变当前方块的形状
- box_rotate
-
- #画出新的方块
- Draw_Box 1
- return 0;
- }
- function move_down()
- {
- if ((currenty > 20 ))
- then
- return 1;
- fi
- #先清除以前的方块
- Draw_Box 0
-
- #改变x坐标
- (( currentY ++ ))
-
- #画出新的方块
- Draw_Box 1
- return 0;
- }
- function Register_Signal()
- {
- trap "sig=$SigA;" $SigA
- trap "sig=$SigS;" $SigS
- trap "sig=$SigD;" $SigD
- trap "sig=$SigW;" $SigW
- }
- function Recive_Signal()
- {
- Random_Box
- Draw_Box 1
- Register_Signal
- while true
- do
- sigThis=$sig
- case "$sigThis" in
- "$SigA")
- move_left
- sig=0
- ;;
- "$SigS")
- move_down
- sig=0
- ;;
- "$SigD")
- move_right
- sig=0
- ;;
- "$SigW")
- move_rotate
- sig=0
- ;;
- esac
-
- done
- }
- function Kill_Signal()
- {
- local sigThis
- while :
- do
- read -s -n 1 key
-
- case "$key" in
- "W"|"w")
- kill -$SigW $1
- ;;
- "S"|"s")
- kill -$SigS $1
- ;;
- "A"|"a")
- kill -$SigA $1
- ;;
- "D"|"d")
- kill -$SigD $1
- ;;
- "Q"|"q")
- kill -9 $1
- exit
- esac
- done
- }
- if [[ "$1" == "--show" ]]
- then
- Recive_Signal
- else
- bash $0 --show &
- Kill_Signal $!
- fi
效果图:
红色部分是我们今天写的代码,思路是使用随机产生的盒子号确定此盒子在box中的位置,然后从此位置开始,把它的所有造型在屏幕上挨个画出来。
到现在为止,我们可以随机产生俄罗斯方块,并且可以移动和改变,下一节我们在屏幕上画出一个矩阵,让盒子从矩阵的顶部自动下落,到矩阵的底部停止。
