Board logo

标题: [转载代码] Linux Shell脚本小游戏:迷你弹球 [打印本页]

作者: find    时间: 2012-1-16 11:39     标题: Linux Shell脚本小游戏:迷你弹球

说明:j,k 控制球拍,q 退出,c 失败后继续
第一次写,代码有点乱。
  1. #!/bin/bash
  2. Width=44
  3. Height=18
  4. Score=0
  5. Level=1
  6. WinFlag=0
  7. SleepTime=100000
  8. Normal="\033[0m"
  9. Green="\033[1;37;42m"
  10. Red="\033[1;37;41m"
  11. Brown="\033[1;37;43m"
  12. Blue="\033[1;35m"
  13. Ching="\033[1;34m"
  14. HumanLen=8
  15. ((HumanXPos=Width/2))
  16. ((HumanYPos=Height+1))
  17. CompXPos=10
  18. CompYPos=2
  19. ((BallXPos=RANDOM%(Width-2)))
  20. ((BallYPos=RANDOM%10))
  21. [[ BallXPos -lt 7 ]] && ((BallXPos=7))
  22. [[ BallYPos -lt 3 ]] && ((BallYPos=3))
  23. BallXSpeed=1
  24. BallYSpeed=1
  25. BorderLeft=7
  26. ((BorderRight=Width-1))
  27. ((InfoX=BorderRight+6))
  28. ((BorderTop=CompYPos+2))
  29. ((AlertY=Height/2))
  30. ((AlertX=Width/2-3))
  31. function DrawSquare(){
  32.   local PaddingLen i
  33.   Item="$Normal    $Green[]$Normal"
  34.   ((PaddingLen=Width-4))
  35.   Padding=""
  36.   for((i=1;i<=PaddingLen;i+=2))
  37.   do
  38.     Padding=$Padding"  "
  39.   done
  40.   Item=$Item$Padding"$Green[]$Normal"
  41.   tput cup 2 4
  42.   echo -e "$Green[][][][][][][][][][][][][][][][][][][][][][]$Normal"
  43.   for((i=1;i<Height;i++))
  44.   do
  45.     echo -e "$Item"
  46.   done
  47.   tput cup 3 $InfoX
  48.   echo -e "$Blue Score  $Normal"
  49.   tput cup 4 $InfoX
  50.   echo -e "$Ching $Score  $Normal"
  51.   tput cup 5 $InfoX
  52.   echo -e "$Blue Level  $Normal"
  53.   tput cup 6 $InfoX
  54.   echo -e "$Ching $Level  $Normal"
  55.   tput cup 8 $InfoX
  56.   echo -e "$Blue LEFT $Normal"
  57.   tput cup 9 $InfoX
  58.   echo -e "$Ching J $Normal"
  59.   tput cup 10 $InfoX
  60.   echo -e "$Blue RIGHT $Normal"
  61.   tput cup 11 $InfoX
  62.   echo -e "$Ching K $Normal"
  63.   tput cup 12 $InfoX
  64.   echo -e "$Blue EXIT $Normal"
  65.   tput cup 13 $InfoX
  66.   echo -e "$Ching Q $Normal"
  67. }
  68. function DrawBall(){
  69.   local OldX OldY
  70.   OldX=$BallXPos
  71.   OldY=$BallYPos
  72.   ((BallXPos=BallXPos+BallXSpeed))
  73.   ((BallYPos=BallYPos+BallYSpeed))
  74.   if [ $BallYPos -eq $((HumanYPos-1)) -a $BallXPos -gt $((HumanXPos-2)) -a $BallXPos -lt $((HumanXPos+HumanLen)) ]
  75.   then
  76.     ((BallYSpeed=-1))
  77.     ((Score=Score+1))
  78.     ((BallXPos=BallXPos+BallXSpeed))
  79.   fi
  80.   if [ $SleepTime -eq 0 ]
  81.   then
  82.     tput cup 12 $((Width/2-10))
  83.     echo -e "$Red\033[37m      Congratulations!      $Normal"
  84.     tput cup 13 $((Width/2-10))
  85.     echo -e "$Red\033[37m You are the king of games! $Normal"
  86.     exit 0
  87.   fi
  88.   if [ $Score -ne 0 -a $((Score%10)) -eq 0 ]
  89.   then
  90.     ((Level=Level+1))
  91.     ((Score=Score+1))
  92.         ((SleepTime=SleepTime-5000))
  93.   fi
  94.   if [ $BallYPos -lt $BorderTop ]
  95.   then
  96.     ((BallYSpeed=1))
  97.   fi
  98.   if [ $BallXPos -lt $BorderLeft ]
  99.   then
  100.     ((BallXSpeed=1))
  101.   fi
  102.   if [ $BallXPos -gt $BorderRight ]
  103.   then
  104.         ((BallXSpeed=-1))
  105.   fi
  106.   if [ $BallYPos -gt $HumanYPos ]
  107.   then
  108.     tput cup $AlertY $AlertX
  109.     echo -e "$Red\033[37m   GAME OVER   $Normal"
  110.     tput cup $((AlertY+1)) $AlertX
  111.     echo -e "$Red\033[37m               $Normal"
  112.     tput cup $((AlertY+2)) $AlertX
  113.     echo -e "$Red\033[37m Exit(q)       $Normal"
  114.     tput cup $((AlertY+3)) $AlertX
  115.     echo -e "$Red\033[37m Continue(c)   $Normal"
  116.     exit 0
  117.   fi
  118.   tput cup 4 $InfoX
  119.   echo -e "$Ching $Score  $Normal"
  120.   tput cup 6 $InfoX
  121.   echo -e "$Ching $Level  $Normal"
  122.   tput cup $OldY 0
  123.   echo -e "$Item"
  124.   tput cup $BallYPos $BallXPos
  125.   echo -e "$Normal$Red[]$Normal"
  126. }
  127. function DrawHuman(){
  128.   tput cup $HumanYPos 6
  129.   echo "$Padding"
  130.   tput cup $HumanYPos $1
  131.   echo -e "$Normal$Brown[][][][]$Normal"
  132. }
  133. function MoveHuman(){
  134.   local sig Xmax
  135.   ((Xmax=BorderRight-(HumanLen-2)))
  136.   trap "sig=26" 26
  137.   trap "sig=27" 27
  138.   trap "sig=28" 28
  139.   while :
  140.   do
  141.     DrawBall
  142.     case $sig in
  143.     26)
  144.       [[ HumanXPos -gt BorderLeft ]] && ((HumanXPos=HumanXPos-2)) && DrawHuman $HumanXPos
  145.     ;;
  146.     27)
  147.       [[ HumanXPos -lt Xmax ]] && ((HumanXPos=HumanXPos+2)) && DrawHuman $HumanXPos
  148.     ;;
  149.     28)
  150.           clear
  151.       exit 0
  152.     ;;
  153.     esac
  154.     #sig=0
  155.     usleep $SleepTime
  156.   done
  157. }
  158. function Restart(){
  159.   tput cup $AlertY 6
  160.   echo "$Padding"
  161.   tput cup $((AlertY+1)) 6
  162.   echo "$Padding"
  163.   tput cup $((AlertY+2)) 6
  164.   echo "$Padding"
  165.   tput cup $((AlertY+3)) 6
  166.   echo "$Padding"
  167.   ((BallXPos=RANDOM%(Width-2)))
  168.   ((BallYPos=RANDOM%10))
  169.   [[ BallXPos -lt 7 ]] && ((BallXPos=7))
  170.   [[ BallYPos -lt 3 ]] && ((BallYPos=3))
  171.   SleepTime=100000
  172.   DrawHuman $HumanXPos
  173. }
  174. clear
  175. Stty=`stty -g`
  176. echo -ne "\033[?25l"
  177. stty -echo
  178. DrawSquare
  179. DrawHuman $HumanXPos
  180. {
  181.   MoveHuman
  182. } &
  183. while read -s -n 1 Key
  184. do
  185.   case $Key in
  186.   [jJ])
  187.   kill -26 $! > /dev/null 2>&1
  188.   ;;
  189.   [kK])
  190.   kill -27 $! > /dev/null 2>&1
  191.   ;;
  192.   [cC])
  193.   if [ -z "`ps x | grep $! | grep -v "grep $!" | awk '{print $1}'`" ]
  194.   then
  195.   Restart
  196.   {
  197.         MoveHuman
  198.   } &
  199.   fi
  200.   ;;
  201.   [qQ])
  202.   kill -28 $! > /dev/null 2>&1
  203.   sleep 0.5
  204.   echo -e "\033[?25h\033[${y};0H"
  205.   stty $Stty
  206.   clear
  207.   exit 0
  208.   ;;
  209.   *)
  210.   ;;
  211.   esac
  212. done
复制代码

作者: ggehuliang    时间: 2012-8-5 20:42

没图???算了代码我收下了。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2