Chinaunix首页 | 论坛 | 博客
  • 博客访问: 69766
  • 博文数量: 49
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2016-09-10 14:44
文章分类
文章存档

2017年(1)

2016年(48)

我的朋友

分类: LINUX

2016-09-17 22:32:59

欢迎转载,请注明原处,版权个人所有,不得用于商业用途,违者将追求起法律责任。  chengyaogen.blog.chinaunix.net
 
我们现在屏幕上画出一个矩阵,这个很简单
  1. #!/bin/bash

  2. clear

  3. #边缘距离
  4. left=10
  5. top=5

  6. #矩阵的长和宽
  7. widthSize=25
  8. hightSize=25

  9. #画出矩阵
  10. function draw_rectangle()
  11. {
  12.     local x y

  13.     echo -e "\033[32m\033[46m\033[1m"

  14.     for ((i = 0 ;i < widthSize;i ++))
  15.     do
  16.         ((x = left + i))
  17.         ((y = top + hightSize - 1))
  18.     
  19.         echo -e "\033[${top};${x}H="
  20.         echo -e "\033[${y};${x}H="

  21.     done

  22.     for ((i = 0 ;i < hightSize;i ++))
  23.     do
  24.         ((x = left + widthSize - 1))
  25.         ((y = top + i))
  26.     
  27.         echo -e "\033[${y};${left}H||"
  28.         echo -e "\033[${y};${x}H||"

  29.     done

  30.     echo -e "\033[0m"
  31. }

  32. draw_rectangle
效果图:
 
矩阵有了,接下来就是让盒子从矩阵的顶部下落下来到底部停止
  1. #!/bin/bash

  2. #七中不同的方块的定义
  3. #通过旋转,每种方块的显示的样式可能有几种
  4. box0=(0 0 0 1 1 0 1 1)
  5. box1=(0 2 1 2 2 2 3 2 1 0 1 1 1 2 1 3)
  6. box2=(0 0 0 1 1 1 1 2 0 1 1 0 1 1 2 0)
  7. box3=(0 1 0 2 1 0 1 1 0 0 1 0 1 1 2 1)
  8. 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)
  9. 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)
  10. 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)

  11. #把所有盒子放在box中
  12. box=(${box0[@]} ${box1[@]} ${box2[@]} ${box3[@]} ${box4[@]} ${box5[@]} ${box6[@]})

  13. #每个盒子在box中的偏移
  14. boxOffset=(0 1 3 5 7 11 15)

  15. #旋转次数
  16. rotateCount=(1 2 2 2 4 4 4)

  17. #颜色数组
  18. colourArry=(31 32 33 34 35 36 37)

  19. #选装类型
  20. rotateType=-1

  21. #盒子标号
  22. boxNum=-1

  23. #新盒子
  24. newBox=()

  25. #边缘距离
  26. left=10
  27. top=5

  28. #矩阵的长和宽
  29. widthSize=28
  30. hightSize=26

  31. #确定从矩阵那个地方出来
  32. function ensure_postion()
  33. {
  34.     local sumx=0 i j

  35.     ((minx = ${newBox[0]}))
  36.     ((miny = ${newBox[1]}))
  37.     ((maxy = miny ))
  38.     
  39.     for ((i = 2; i < ${#newBox[*]};i += 2))
  40.     do
  41.         #确定最小的x坐标
  42.         if ((minx > ${newBox[i]}))
  43.         then
  44.             ((minx = ${newBox[i]}))
  45.         fi
  46.         
  47.         #确定最小的y坐标
  48.         if ((miny > ${newBox[i+1]}))
  49.         then
  50.             ((miny = ${newBox[i+1]}))
  51.         fi

  52.         if ((${newBox[i]} == ${newBox[i-2]}))
  53.         then
  54.             continue
  55.         fi
  56.         
  57.         ((sumx ++))

  58.     done

  59.     if ((sumx == 0))
  60.     then
  61.         ((sumx = 1))
  62.     fi
  63.     
  64.     #当前x,y坐标
  65.     ((currentX = left + widthSize / 2 - sumx * 2 - minx))
  66.     ((currentY = top + 1 - miny))    

  67.     return 0
  68. }

  69. #画出矩阵
  70. function draw_rectangle()
  71. {
  72.     local x y

  73.     echo -e "\033[32m\033[46m\033[1m"

  74.     for ((i = 0 ;i < widthSize;i ++))
  75.     do
  76.         ((x = left + i))
  77.         ((y = top + hightSize - 1))
  78.     
  79.         echo -e "\033[${top};${x}H="
  80.         echo -e "\033[${y};${x}H="

  81.     done

  82.     for ((i = 0 ;i < hightSize;i ++))
  83.     do
  84.         ((x = left + widthSize - 1))
  85.         ((y = top + i))
  86.     
  87.         echo -e "\033[${y};${left}H||"
  88.         echo -e "\033[${y};${x}H||"

  89.     done

  90.     echo -e "\033[0m"
  91. }


  92. #画出方块
  93. function Draw_Box()
  94. {
  95.     local i j x y

  96.     if (($1 == 0))
  97.     then
  98.         for ((i = 0;i < 8;i += 2))
  99.         do
  100.             ((x = currentX + 3 * ${newBox[i]}))
  101.             ((y = currentY + ${newBox[i+1]}))

  102.             echo -e "\033[${y};${x}H "
  103.         done
  104.     else
  105.         echo -e "\033[${colourArry[$colourNum]}m\033[1m"
  106.         for ((i = 0;i < 8;i += 2))
  107.         do
  108.             ((x = currentX + 3 * ${newBox[i]}))
  109.             ((y = currentY + ${newBox[i+1]}))

  110.             echo -e "\033[${y};${x}H[*]"
  111.         done
  112.     fi

  113.     echo -e "\033[0m"
  114. }

  115. #随机产生方块
  116. function Random_Box()
  117. {
  118.     #随机产生盒子号
  119.     (( boxNum = $RANDOM % 7 ))    
  120.     #随机长生盒子的类型
  121.     ((rotateType = $RANDOM % ${rotateCount[boxNum]}))
  122.     #随机产生颜色
  123.     ((colourNum = $RANDOM % ${#colourArry[*]}))

  124.     #找到所在box中的起始位置
  125.     ((j = ${boxOffset[boxNum]} * 8 + rotateType * 8))

  126.     for(( i = 0 ;i < 8;i ++))
  127.     do
  128.         ((newBox[i] = ${box[j+i]}))
  129.     done
  130. }

  131. #判断能否下移
  132. function move_test()
  133. {
  134.     local vary=$1 i
  135.     
  136.     #当前的y坐标加上newBox里面的坐标,其值是否大于28
  137.     for ((i = 0;i < ${#newBox[@]}; i += 2))
  138.     do
  139.         if ((vary + ${newBox[i+1]} > 28))
  140.         then
  141.             return 0
  142.         fi
  143.     done

  144.     return 1
  145. }


  146. draw_rectangle
  147. Random_Box
  148. ensure_postion

  149. while :
  150. do
  151.     Draw_Box 1

  152.     sleep 0.1
  153.     Draw_Box 0

  154.     ((currentY ++))

  155.     if move_test currentY
  156.     then
  157.         Draw_Box 1
  158.         sleep 2
  159.         Draw_Box 0
  160.         Random_Box
  161.         ensure_postion
  162.     fi

  163. done

  164. echo
效果图:
下一次,我们实现通过键盘改变方块形状和移动方块,并且实现累加
阅读(839) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~