用shell写俄罗斯方块(三)

1367阅读 0评论2012-03-30 bill_cao
分类:

欢迎转载,请注明原处,版权个人所有,不得用于商业用途,违者将追求起法律责任。  chengyaogen.blog.chinaunix.net
上一节我们学了一下shell脚本中信号的用法,这一节我们在屏幕上画出一个方块,并且让它动起来.

box0=(0  0  0  1  1  0  1  1)
现在我们就让box0在我们屏幕的任意地方动起来
实现功能:
A  -> 左移    D->右移     W->向上     S->向下
思路就是通过改变x,y的坐标,在屏幕不同的地方,把box0画出来

  1. #!/bin/bash

  2. #信号
  3. SigA=20
  4. SigS=21
  5. SigD=22
  6. SigW=23
  7. sig=0

  8. #方块在屏幕上的坐标点
  9. box0=(0 0 0 1 1 0 1 1)

  10. #边缘距离
  11. top=3
  12. left=3

  13. #当前x,y坐标
  14. currentX=15
  15. currentY=2

  16. function Draw_Box()
  17. {
  18.     local i j x y

  19.     if (($1 == 0))
  20.     then
  21.         for ((i = 0;i < 8;i += 2))
  22.         do
  23.             ((x = left + 3 * (currentX + ${box0[i]})))
  24.             ((y = top + currentY + ${box0[i+1]}))

  25.             echo -e "\033[${y};${x}H "
  26.         done
  27.     else
  28.         echo -e "\033[31m\033[1m"
  29.         for ((i = 0;i < 8;i += 2))
  30.         do
  31.             ((x = left + 3 * (currentX + ${box0[i]})))
  32.             ((y = top + currentY + ${box0[i+1]}))

  33.             echo -e "\033[${y};${x}H[*]"
  34.         done
  35.     fi

  36.     echo -e "\033[0m"
  37. }

  38. function move_left()
  39. {    

  40.     if ((currentX == 0 ))
  41.     then
  42.         return 1;
  43.     fi

  44.     #先清除以前的方块
  45.     Draw_Box 0
  46.     
  47.     #改变x坐标
  48.     (( currentX -- ))
  49.     
  50.     #画出新的方块
  51.     Draw_Box 1

  52.     return 0;
  53. }


  54. function move_right()
  55. {    

  56.     if ((currentX > 20 ))
  57.     then
  58.         return 1;
  59.     fi

  60.     #先清除以前的方块
  61.     Draw_Box 0
  62.     
  63.     #改变x坐标
  64.     (( currentX ++ ))
  65.     
  66.     #画出新的方块
  67.     Draw_Box 1

  68.     return 0;
  69. }


  70. function move_up()
  71. {    

  72.     if ((currentY == 0 ))
  73.     then
  74.         return 1;
  75.     fi

  76.     #先清除以前的方块
  77.     Draw_Box 0
  78.     
  79.     #改变x坐标
  80.     (( currentY -- ))
  81.     
  82.     #画出新的方块
  83.     Draw_Box 1

  84.     return 0;
  85. }


  86. function move_down()
  87. {    

  88.     if ((currenty > 20 ))
  89.     then
  90.         return 1;
  91.     fi

  92.     #先清除以前的方块
  93.     Draw_Box 0
  94.     
  95.     #改变x坐标
  96.     (( currentY ++ ))
  97.     
  98.     #画出新的方块
  99.     Draw_Box 1

  100.     return 0;
  101. }


  102. function Register_Signal()
  103. {
  104.     trap "sig=$SigA;" $SigA
  105.     trap "sig=$SigS;" $SigS
  106.     trap "sig=$SigD;" $SigD
  107.     trap "sig=$SigW;" $SigW
  108. }

  109. function Recive_Signal()
  110. {
  111.     Register_Signal

  112.     Draw_Box 1

  113.     while true
  114.     do
  115.         sigThis=$sig

  116.         case "$sigThis" in
  117.             "$SigA")
  118.                 move_left
  119.                 sig=0
  120.                 ;;

  121.             "$SigS")
  122.                 move_down
  123.                 sig=0
  124.                 ;;

  125.             "$SigD")
  126.                 move_right
  127.                 sig=0
  128.                 ;;

  129.             "$SigW")
  130.                 move_up
  131.                 sig=0
  132.                 ;;
  133.         esac
  134.         
  135.     done
  136. }

  137. function Kill_Signal()
  138. {
  139.     local sigThis

  140.     while :
  141.     do
  142.         read -s -n 1 key
  143.         
  144.         case "$key" in

  145.             "W"|"w")
  146.                 kill -$SigW $1
  147.                 ;;
  148.             "S"|"s")
  149.                 kill -$SigS $1
  150.                 ;;
  151.             "A"|"a")
  152.                 kill -$SigA $1
  153.                 ;;
  154.             "D"|"d")
  155.                 kill -$SigD $1
  156.                 ;;
  157.             "Q"|"q")
  158.                 kill -9 $1
  159.                 exit
  160.      esac

  161.     done
  162. }

  163. if [[ "$1" == "--show" ]]
  164. then
  165.     Recive_Signal
  166. else
  167.     bash $0 --show &
  168.     Kill_Signal $!
  169. fi
效果图:
 
就到这吧,下一节我们随机产生方块,让后让它改变形状。
上一篇:用shell写俄罗斯方块(二)
下一篇:用shell写俄罗斯方块(四)