免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 17379 | 回复: 33
打印 上一主题 下一主题

shell游戏:贪吃蛇 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-07-28 17:00 |只看该作者 |倒序浏览
shell游戏:贪吃蛇

期待各位帮忙测试,如有BUG,请及时告知,谢谢
转载请保持版权声明完整,十分感谢

下载代码:
http://bitbull.cn/works/snake

六关卡屏幕截图:
http://bitbull.cn/pic/blog/2005072801.jpg
http://bitbull.cn/pic/blog/2005072802.jpg
http://bitbull.cn/pic/blog/2005072803.jpg
http://bitbull.cn/pic/blog/2005072804.jpg
http://bitbull.cn/pic/blog/2005072805.jpg
http://bitbull.cn/pic/blog/2005072806.jpg

由于程序运行过程中会产生己个临时文件,
请先确定/tmp目录可写可读..如果权限不够或无此目录,请把游戏
cpath="/tmp/snake_ctrl_pid.tmp"
dpath="/tmp/snake_disply_pid.tmp"
vartmp="/tmp/snake_var_tmpfile.tmp"
改到有权限读写的目录


测试环境:
RHEL4 AS
bash 3.0
80x24终端

usage:
w上 s下 a左 d右
p暂停
n新游戏
q退出

规则:
吃完屏幕上所有的红色果实即可过关,共有6个关卡.碰到任何障碍蛇都将死亡,玩家有4次生命.

代码:

  1. #!/bin/bash
  2. #-------------CopyRight-------------
  3. #   Name:Snake
  4. #   Version Number:1.00
  5. #   Type:game
  6. #   Language:bash shell
  7. #   Date:2005-07-28
  8. #   Author:BitBull
  9. #   Email:wengjianyi@tom.com
  10. #------------Environment------------
  11. #   Terminal: column 80 line 24
  12. #   Linux 2.6.9 i686
  13. #   GNU Bash 3.00.15
  14. #-----------------------------------


  15. #--------------variable--------------
  16. #game variable
  17. level=1
  18. score=0
  19. life=3
  20. length=8
  21. runtime=0.15
  22. fruitspare=8

  23. #game kernel variable
  24. x=2 #init snake x=2 y=2
  25. y=2
  26. direction=0
  27. shead=1 #snake's head in snake[]
  28. stail=1 #snake's tail in snake[]
  29. mappoint=1 #point exactmap[] bottom
  30. state=on #snake run or stop
  31. run=off  #if run=on,snake shadow is working
  32. displaypid=""
  33. controlpid=""

  34. #game temp file;if your system's /tmp unwrite or unread, you can change to home
  35. cpath="/tmp/snake_ctrl_pid.tmp"
  36. dpath="/tmp/snake_disply_pid.tmp"
  37. vartmp="/tmp/snake_var_tmpfile.tmp"

  38. #rename kill sign
  39. pause=23
  40. newgame=24
  41. gameover=25
  42. gameexit=26
  43. up=27
  44. down=28
  45. left=29
  46. right=22
  47. #---------------array---------------                                          
  48. #init exactmap
  49. exactmap=()

  50. #map format: y x HowLong "-- or |" ( 1=| 2=-- )
  51. map1=("6 14 6 2" "6 50 6 2" "14 14 6 2" "14 50 6 2")
  52. map2=("2 16 10 1" "2 48 10 1" "7 32 10 1" "7 64 10 1")
  53. map3=("4 16 24 2" "10 16 24 2" "16 16 24 2" "4 16 11 1")
  54. map4=("10 4 34 2" "4 20 12 1" "4 40 12 1" "4 60 12 1")
  55. map5=("5 10 29 2" "15 10 29 2" "5 16 7 1" "7 60 6 1" )
  56. map6=("8 4 35 2" "2 50 5 1" "10 4 36 2" "11 30 5 1" )

  57. #where is fruit? format:y x
  58. fruit1=("14 10" "13 56" "2 40" "3 8" "17 50" "18 76" "14 30" "6 66")
  59. fruit2=("4 14" "2 40" "14 48" "12 68" "9 30" "18 6" "3 76" "18 78")
  60. fruit3=("7 14" "18 4" "15 40" "11 24" "5 18" "9 56" "3 76" "17 64")
  61. fruit4=("11 10" "11 62" "9 38" "9 72" "6 58" "14 26" "17 58" "3 6")
  62. fruit5=("6 14" "16 14" "3 40" "6 22" "14 58" "12 34" "8 50" "9 62")
  63. fruit6=("2 52" "7 40" "7 60" "4 70" "11 28" "11 32" "15 22" "17 78" )

  64. #--------------function--------------
  65. #draw screen
  66. function Draw_line () {

  67.         local i=1

  68.         while [ "$i" -le "80" ]
  69.         do
  70.                 echo -ne "\33[${1};${i}H*"
  71.                 (( i++ ))
  72.         done
  73. }
  74. function Draw_row () {

  75.         local i=2

  76.         while [ "$i" -le "22" ]
  77.         do
  78.                 echo -ne "\33[${i};${1}H*"
  79.                 (( i++ ))
  80.         done
  81. }
  82. function Draw_help () {

  83.         echo -ne "\33[7;31m\33[24;1HPlay:w s a d Pause:p Newgame:n Quit:q      -- CopyRight -- 2005-07-28 BitBull --\33[0m"       
  84. }       
  85. function Screen () {

  86.         echo -ne "\33[37;44m"
  87.         Draw_line 1
  88.         Draw_line 19
  89.         Draw_line 23
  90.         Draw_row 1
  91.         Draw_row 80
  92.         echo -ne "\33[0m"
  93.         Draw_help
  94. }

  95. #init
  96. function Init () {

  97.         stty_save=$(stty -g) #backup stty
  98.         clear
  99.         trap "Game_exit;" 2 15
  100.         stty -echo

  101.         echo -ne "\33[?25l"  #hidden cursor
  102. }       

  103. #exit
  104. function Game_exit () {

  105.            kill -9 $displaypid>/dev/null 2>&1 #kill display function

  106.         #restore
  107.         stty $stty_save
  108.         stty echo
  109.         clear
  110.         trap 2 15
  111.         echo -ne "\33[?25h\33[0;0H\33[0m"
  112.         rm -f $cpath $dpath >/dev/null 2>&1

  113.         exit 0
  114. }

  115. #draw level score life SnakeLong
  116. function Draw_ls () {

  117.         echo -ne "\33[31m"
  118.         echo -ne "\33[21;10HLevel=$level         Score=$score        \
  119. Life=$life        Snake=$length"
  120.         echo -ne "\33[0m"
  121. }

  122. #output info to player
  123. function Info () {
  124.                
  125.         title="$1"
  126.         content="$2"
  127.         greeting="$3"
  128.        
  129.         printf "\33[31m"
  130.         printf "\33[11;20H ------------------------------------------- "
  131.         printf "\33[12;20H|         ======>$title<======           |"
  132.         printf "\33[13;20H|         $content          |"
  133.         printf "\33[14;20H|         ======>$greeting<======           |"
  134.         printf "\33[15;20H ------------------------------------------- "
  135.         printf "\33[0m"

  136. }

  137. #square:draw square in screen.you can define X Y COLOR LETTER
  138. function Square () {

  139.         local color=$1;line=$2;row=$3;pic=$4

  140.         echo -ne "\33[34444;${color}m\33[${line};${row}H${pic}\33[0m"
  141. }

  142. #show fruit
  143. function Show_fruits () {
  144.        
  145.         local red=45;fruitxy=""
  146.        
  147.         for (( i = 0; i < 8; i++ ))
  148.         do
  149.                 fruitxy="$(printf "\${fruit%s[$i]}" $level)"
  150.                 eval Square $red $fruitxy '@@'
  151.         done
  152. }

  153. #exact map:calculate mapXY into exactmap[]
  154. function Exact_map () {

  155.         local mapin xtmp ytmp long line_row
  156.        
  157.         for (( i = 0; i < 4; i++ ))
  158.         do
  159.                 mapin="$(printf "\${map%s[$i]}" $level)"
  160.                 xtmp=$(eval echo $mapin|cut -d" " -f2)
  161.                 ytmp=$(eval echo $mapin|cut -d" " -f1)
  162.                 long=$(eval echo $mapin|cut -d" " -f3)
  163.                 line_row=$(eval echo $mapin|cut -d" " -f4)

  164.                 exactmap[$mappoint]="$ytmp $xtmp"
  165.                 (( mappoint++ ))

  166.                 #judge mapline | or --
  167.                 if [[ "$line_row" == "1" ]]
  168.                 then
  169.                         for (( j = 0; j <= long; j++ ))
  170.                         do
  171.                                 (( ytmp++ ))
  172.                                 exactmap[$mappoint]="$ytmp $xtmp"
  173.                                 (( mappoint++ ))
  174.                         done
  175.                 else
  176.                         for (( k = 0; k <= long; k++ ))
  177.                         do
  178.                                 (( xtmp += 2 ))
  179.                                 exactmap[$mappoint]="$ytmp $xtmp"
  180.                                 (( mappoint++ ))
  181.                         done
  182.                 fi
  183.         done
  184. }


  185. #show map
  186. function Show_map () {

  187.         local mapxy="";blue=46

  188.         Exact_map

  189.         for (( i = 1; i < mappoint; i++ ))
  190.         do
  191.                 eval Square $blue ${exactmap[$i]} '[]'
  192.         done                       
  193. }

  194. #test snake is ok ?
  195. function Test_snake () {

  196. #snake self
  197.         for (( i = 1; i <= length; i++ ))
  198.         do
  199.                 if [[ "${snake[$i]}" == "$y $x" ]]
  200.                 then Dead
  201.                 fi
  202.         done
  203. #borderline
  204.         if [[ $x -lt 2 || $x -gt 79 || $y -lt 2 || $y -gt 18 ]]
  205.         then Dead
  206.         fi
  207. #map line
  208.         for (( i = 0; i < mappoint; i++ ))
  209.         do
  210.                 if [[ "${exactmap[$i]}" == "$y $x" ]]
  211.                 then Dead
  212.                 fi
  213.         done
  214. }

  215. #eat
  216. function Eat () {

  217.         local fruitxy="";xyvalue="";nowarray=""

  218.         for (( i = 0; i < 8; i++ ))
  219.         do
  220.                 fruitxy="$(printf "\${fruit%s[$i]}" $level)"
  221.                 xyvalue="$(eval echo $fruitxy)"

  222.                 if [[ "$xyvalue" = "$y $x" ]]
  223.                 then
  224.                         nowarray="$(printf "fruit%s[$i]=" $level)"
  225.                         eval $nowarray""
  226.                         (( score++ ))
  227.                         (( fruitspare-- ))
  228.                         Draw_ls
  229.                 fi
  230.         done
  231.         if [[ $fruitspare == 0 ]]
  232.         then Next_level
  233.         fi
  234. }

  235. #if snake dead
  236. function Dead () {

  237.         state=off

  238.         if (( "$life" == "0" ))
  239.         then
  240.                 kill -$gameover $controlpid
  241.         else
  242.                 (( life-- ))
  243.                 Info "SnakeDead" "OH!shit!You are a idiot!" "F**k  You"
  244.                 sleep 1               
  245.                 New_game
  246.         fi
  247. }

  248. #next level
  249. function Next_level () {

  250.         (( level++ ))
  251.         (( length += 6 ))
  252.         if [[ $level -gt 6 ]]
  253.         then
  254.                 Info "Well Done" "   WOW!Congratulation!  " "Thank You"
  255.                 sleep 4
  256.                 kill -$gameexit $controlpid
  257.         else
  258.                 Info "Well Done" "Level Update! Go Level $level" ".Loading."
  259.                 sleep 3
  260.                 New_game
  261.         fi
  262. }

  263. #newgame
  264. function New_game () {
  265.        
  266.         kill -9 $displaypid >/dev/null 2>&1

  267.         if [[ "$1" == "over" ]]
  268.         then
  269.                 exec $0
  270.         else
  271.                 echo "$level $score $life $length $runtime" > $vartmp
  272.                 exec $0 display
  273.         fi
  274. }

  275. #game over
  276. function Game_over () {

  277.         local y_n

  278.         Info "Game Over" "Do you want replay?<y/n>" "Thank You"

  279.         while read -s -n 1 y_n
  280.         do
  281.                 case $y_n in
  282.                 [yY] ) New_game over
  283.                 ;;
  284.                 [nN] ) Game_exit
  285.                 ;;
  286.                 * ) continue
  287.                 ;;
  288.                 esac
  289.         done
  290. }


  291. #main
  292. function Main () {
  293.        
  294.         local green=42;count=0
  295.        
  296.         case $direction in
  297.         "$up" ) (( y-- ))
  298.         ;;
  299.         "$down" ) (( y++ ))
  300.         ;;
  301.         "$left" ) (( x -= 2 ))
  302.         ;;
  303.         "$right" ) (( x += 2 ))
  304.         ;;
  305.         *):
  306.         ;;
  307.         esac       
  308.         Test_snake
  309.         Eat

  310.         #go go go
  311.         Square $green $y $x \#\#
  312.         snake[$shead]="$y $x"
  313.         (( shead++ ))
  314.        
  315.         if [[ "$shead" == "$length" ]]
  316.         then
  317.                 shead=1
  318.                 run=on #snake shadow run
  319.         fi
  320.        
  321.         #snake shadow,it can erase snake's tail,otherwise,snake will very long!
  322.         if [[ "$run" == "on" ]]
  323.         then
  324.                 Square 0 ${snake[$stail]} "  "
  325.                 (( stail++ ))
  326.                 if [[ "$stail" == "$length" ]]
  327.                 then
  328.                         stail=1
  329.                 fi
  330.         fi
  331. }

  332. #state change:off=snake stop.on=snake run
  333. function State_change () {
  334.         if [[ $state == "on" ]]
  335.         then state=off
  336.         else state=on
  337.         fi
  338. }
  339. #display
  340. function Display () {

  341.         trap "State_change;" $pause
  342.         trap "direction=$up;" $up
  343.         trap "direction=$down;" $down
  344.         trap "direction=$left;" $left
  345.         trap "direction=$right;" $right

  346.         echo $$ > $dpath
  347.         read controlpid < $cpath
  348.         if [[ -e $vartmp ]]
  349.         then
  350.                 read level score life length runtime< $vartmp
  351.                 rm -f $vartmp
  352.         fi

  353.         #drow all
  354.         Init                                                              
  355.         Screen
  356.         Draw_ls
  357.         Show_fruits
  358.         Show_map
  359.         Main
  360.         #game main loop
  361.         while :
  362.         do
  363.                 if [[ ( "$state" == "on" ) && ( "$direction" != "0" ) ]]
  364.                 then
  365.                         Main
  366.                         sleep $runtime
  367.                 fi
  368.         done
  369. }


  370. #control
  371. function Control () {

  372.         local sign=""

  373.         echo $$ > $cpath
  374.        
  375.         trap "Game_over;" $gameover
  376.         trap "Game_exit;" $gameexit

  377.         while read -s -n 1 key
  378.         do
  379.                
  380.                 case $key in
  381.                 [wW]) sign="$up"
  382.                 ;;
  383.                 [sS]) sign="$down"
  384.                 ;;
  385.                 [aA]) sign="$left"
  386.                 ;;
  387.                 [dD]) sign="$right"
  388.                 ;;
  389.                 [pP]) sign="$pause"
  390.                 ;;
  391.                 [nN]) New_game over
  392.                 ;;
  393.                 [qQ]) Game_exit
  394.                 ;;
  395.                 * ) continue 2
  396.                 ;;
  397.                 esac
  398.                
  399.                 eval displaypid=$(cat $dpath)
  400.                 kill -$sign $displaypid

  401.         done
  402. }


  403. #------------main----------------
  404. if [[ "$1" == "display" ]]
  405. then
  406.         Display
  407.         exit
  408. else
  409.         bash $0 display&
  410.         Control
  411.         exit
  412. fi
复制代码

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2005-07-29 00:38 |只看该作者

shell游戏:贪吃蛇

I 服了 U

论坛徽章:
0
3 [报告]
发表于 2005-07-29 09:18 |只看该作者

shell游戏:贪吃蛇

还是烈火兄好,亲一个,嘻嘻
好久没来了,现在很多人我都不认识了

论坛徽章:
0
4 [报告]
发表于 2005-07-29 09:36 |只看该作者

shell游戏:贪吃蛇

厉害~弄回去慢慢研究

论坛徽章:
0
5 [报告]
发表于 2005-07-29 12:29 |只看该作者

shell游戏:贪吃蛇

我的bash是2.05.8,好像不能用哟!

论坛徽章:
0
6 [报告]
发表于 2005-07-29 12:59 |只看该作者

shell游戏:贪吃蛇

啊~楼上的哥们,把bash 2.05的man导出一份给我看看好吗?我看看使用到了3.0的什么新特性,我测试环境是3.0

论坛徽章:
0
7 [报告]
发表于 2005-07-29 13:45 |只看该作者

shell游戏:贪吃蛇

我的是bash2.03,man有5000多行,怎么给你?

PS,考哪里学校了?

论坛徽章:
0
8 [报告]
发表于 2005-07-29 15:22 |只看该作者

shell游戏:贪吃蛇

发我邮箱?wengjianyi@tom.com
还是www.mofile.com开个空间,传上去给我地址

ice,考北京垃圾学校了,哎~~提这就不来精神

论坛徽章:
0
9 [报告]
发表于 2005-07-29 16:05 |只看该作者

shell游戏:贪吃蛇

没什么,也就是点人事安排问题

主要就是不支持ksh那样的for循环。

论坛徽章:
0
10 [报告]
发表于 2005-07-29 17:55 |只看该作者

shell游戏:贪吃蛇

我查了ice,bash2.05支持for (( )) 这种循环
我改了程序那些KILL信号的rename,你再试试
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP