Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1654512
  • 博文数量: 135
  • 博客积分: 2820
  • 博客等级: 少校
  • 技术积分: 2544
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-16 13:33
文章分类

全部博文(135)

文章存档

2015年(1)

2014年(8)

2013年(16)

2012年(43)

2011年(56)

2010年(11)

分类: LINUX

2012-12-03 16:25:01

原贴:

点击(此处)折叠或打开

  1. [root@cacti test]# cat tanchi.sh
  2. #!/bin/sh

  3. ## 区域内部的可活动的范围。此处可修改相应的值
  4. Width=50 # 偶数
  5. Height=15
  6. PadX=10
  7. PadY=5
  8. Speed="2" # 1-9,值越小,速度越快

  9. ## 蛇起始位置
  10. ((SnakeX=PadX+Width/2))
  11. ((SnakeY=PadY+Height/2))
  12. ((ScoreX=PadX+Width/2-10))
  13. ((ScoreY=PadY-3))
  14. score=0
  15. SnakeHead="\033[41m@\033[0m"
  16. SnakeBody="\033[41m \033[0m"


  17. P1="\033[31mO\033[0m"
  18. P2="\033[32mO\033[0m"
  19. P3="\033[33mO\033[0m"
  20. P4="\033[34mO\033[0m"
  21. P5="\033[35mO\033[0m"
  22. P6="\033[36mO\033[0m"
  23. P7="\033[37mO\033[0m"

  24. PS=(" " $P1 $P2 $P3 $P4 $P5 $P6 $P7)
  25. PNum=${#PS[*]}


  26. clear
  27. function DrawBox(){
  28. local XSmall XBig YSmall YBig
  29. ((XSmall=PadX-2))
  30. ((YSmall=PadY-1))
  31. ((XBig=PadX+Width))
  32. ((YBig=PadY+Height))
  33. for((i=XSmall;i<=XBig;i+=2))
  34. do
  35. echo -ne "\033[$YSmall;${i}H\033[42m[]\033[0m"
  36. echo -ne "\033[$YBig;${i}H\033[42m[]\033[0m"
  37. done

  38. for((i=YSmall;i<=YBig;i++))
  39. do
  40. echo -ne "\033[$i;${XSmall}H\033[42m[]\033[0m"
  41. echo -ne "\033[$i;${XBig}H\033[42m[]\033[0m"
  42. done
  43. echo
  44. echo
  45. }


  46. function CordToKey(){
  47. local x y Max
  48. Max=100
  49. x=$1
  50. y=$2
  51. ((x+=Max))
  52. ((y+=Max))
  53. echo $x$y
  54. }

  55. function Values(){
  56. local i j
  57. for((i=PadX;i<=PadX+Width;i++))
  58. do
  59. for((j=PadY;j<=PadY+Height;j++))
  60. do
  61. values[`CordToKey $i $j`]="$i|$j"
  62. done
  63. done
  64. }

  65. function GameOver(){
  66. local x y
  67. ((x=PadX+Width/2-5))
  68. ((y=PadY+Height+2))
  69. echo -e "\033[$y;${x}H \033[32mGame Over!\033[0m\n\n"
  70. kill $PPID
  71. MoveXYExit
  72. }

  73. function NewP(){
  74. local x y p v
  75. while :
  76. do
  77. ((x=RANDOM%Width+PadX))
  78. ((y=RANDOM%Height+PadY))
  79. v=${values[`CordToKey $x $y`]}
  80. if [[ $v =~ "\|" ]];then
  81. ((p=RANDOM%((PNum-1))+1))
  82. echo -ne "\033[$y;${x}H${PS[$p]}"
  83. values[`CordToKey $x $y`]="$p"
  84. break
  85. fi
  86. done
  87. }

  88. function Moving(){
  89. local X Y oldX oldY v i j sx sy
  90. X=$1; Y=$2; oldX=$3; oldY=$4; v=$5
  91. echo -ne "\033[$Y;${X}H$SnakeHead"
  92. values[`CordToKey $X $Y`]="snake"
  93. Snake $X $Y
  94. echo -ne "\033[$oldY;${oldX}H$SnakeBody"
  95. if [[ ${#v} != 1 ]];then
  96. for((i=0;i<${#SnakeValue[*]};i+=2))
  97. do
  98. if [ "${SnakeValue[$i]}" != "" ];then
  99. ((j=i+1))
  100. sx=${SnakeValue[$i]}
  101. sy=${SnakeValue[$j]}
  102. SnakeValue[$i]=""
  103. SnakeValue[$j]=""
  104. echo -ne "\033[$sy;${sx}H "
  105. values[`CordToKey $sx $sy`]="$sx|$sy"
  106. break
  107. fi
  108. done
  109. else
  110. ((score+=v))
  111. echo -ne "\033[$ScoreY;${ScoreX}H \033[32m Score: $score \033[0m"
  112. NewP
  113. fi

  114. }

  115. function Snake(){
  116. SnakeValue[${#SnakeValue[*]}]=$1
  117. SnakeValue[${#SnakeValue[*]}]=$2
  118. }

  119. function MoveXY(){
  120. local sig oldX oldY v
  121. Init

  122. trap "sig=26" 26
  123. trap "sig=27" 27
  124. trap "sig=28" 28
  125. trap "sig=29" 29
  126. trap "MoveXYExit" 30

  127. while :
  128. do
  129. oldX=$X
  130. oldY=$Y

  131. case $sig in
  132. 28)((maxX=Width+PadX-1))
  133. ((X++))
  134. v=${values[`CordToKey $X $Y`]}
  135. [[ $X -gt $maxX || "$v" == "snake" ]] && GameOver
  136. ;;
  137. 29)((X--))
  138. v=${values[`CordToKey $X $Y`]}
  139. [[ $X -lt $PadX || "$v" == "snake" ]] && GameOver
  140. ;;
  141. 27)((maxY=Height+PadY-1))
  142. ((Y++))
  143. v=${values[`CordToKey $X $Y`]}
  144. [[ $Y -gt $maxY || "$v" == "snake" ]] && GameOver
  145. ;;
  146. 26)((Y--))
  147. v=${values[`CordToKey $X $Y`]}
  148. [[ $Y -lt $PadY || "$v" == "snake" ]] && GameOver
  149. ;;
  150. esac
  151. Moving $X $Y $oldX $oldY $v
  152. sleep .$Speed
  153. done
  154. }

  155. function Init(){
  156. SnakeValue=()
  157. DrawBox
  158. Values
  159. X=$SnakeX
  160. Y=$SnakeY
  161. Snake $X $Y
  162. echo -ne "\033[$Y;${X}H$SnakeHead"
  163. values[`CordToKey $X $Y`]="snake"
  164. echo -ne "\033[$ScoreY;${ScoreX}H \033[32m Score: $score \033[0m"
  165. NewP
  166. NewP
  167. NewP
  168. }

  169. function MoveXYExit(){
  170. local y
  171. ((y=PadY+Height+2))
  172. echo -e "\033[?25h\033[${y};0H"
  173. echo
  174. exit
  175. }

  176. function MoveSnakeExit(){
  177. kill -30 $pid
  178. stty $sTTY
  179. MoveXYExit
  180. }

  181. function MoveSnake(){
  182. local key sig
  183. pid=$1
  184. sTTY=`stty -g`
  185. echo -ne "\033[?25l"
  186. trap "MoveSnakeExit" INT TERM
  187. while :
  188. do
  189. sig=0
  190. read -s -n 1 key
  191. [[ "$key" == "A" ]] && sig=26
  192. [[ "$key" == "B" ]] && sig=27
  193. [[ "$key" == "C" ]] && sig=28
  194. [[ "$key" == "D" ]] && sig=29
  195. [[ "$key" == "q" ]] && MoveSnakeExit
  196. [ $sig -ne 0 ] && kill -$sig $pid
  197. done
  198. }

  199. ## Main ##
  200. if [ "$1" == "MoveXY" ];then
  201. MoveXY
  202. else
  203. bash $0 MoveXY &
  204. MoveSnake $! 2>/dev/null
  205. fi


CentOS5.4完美运行.

上个图

阅读(2486) | 评论(0) | 转发(1) |
0

上一篇:php判断平年闰年

下一篇:linux hwclock 详解

给主人留下些什么吧!~~