Board logo

标题: [游戏娱乐] 2016版贪吃蛇 [打印本页]

作者: happy886rr    时间: 2016-9-21 00:51     标题: 2016版贪吃蛇

本帖最后由 happy886rr 于 2016-9-21 22:28 编辑

[2016/9/21]贪吃蛇bat版 Version1.3
  2016版贪吃蛇,采用动态显示算法,时分复用随机,具有很高的流畅性且不会闪屏。核心模块完全是在新版推箱子的基础上改编的。WSAD 上下左右、Q退出,P暂停,方向键恢复行走。采用屏幕背景色来显示蛇的状态和游戏的gameover,即绿屏表示蛇正常,红屏表示蛇撞墙,紫屏表示蛇自杀。“☆★○●◎△▲¤⊙”表示食物,相应的分值渐增。⊙代表蛇头。
  1. @echo off
  2. title &setlocal enabledelayedexpansion
  3. ::**************Happy's 贪吃蛇**************
  4. REM 设置蛇的时钟,T值越小速度越快
  5.   set T=3
  6. REM 开启防闪烁,用于画质增强
  7.   set F1=1
  8. REM WSAD 上下左右
  9. REM Q退出,P暂停,方向键恢复行走
  10. REM 绿屏表示蛇正常,红屏表示蛇撞墙,紫屏表示蛇自杀
  11. ::**************方向控制台窗口**************
  12. if "%1"=="CONTROLP" (
  13. MODE CON cols=15 lines=1
  14. echo 1 >key.ini
  15. for /l %%i in () do (
  16. choice /C WSADPQ>NUL
  17. echo !errorlevel!>key.ini&if !errorlevel! equ 6 (exit)
  18. )
  19. )
  20. start "" "%~f0" CONTROLP
  21. ::****************先导程序******************
  22. REM 默认皮肤
  23.   set "ELEMENTS= □◇⊙★■■■■■☆★○●◎△▲¤⊙"
  24. REM 设置窗体
  25.   mode con cols=68 lines=38
  26. REM 初始化元素
  27.   set/a "MapHeight=30,MapWidth=30"
  28.   for /l %%i in (1,1,500) do (set "SPACE=!SPACE!")
  29.   for /l %%N in (0,1,19) do (set "P%%N=!ELEMENTS:~%%N,1!")
  30.   for /l %%i in (1,1,%MapWidth%) do (set "LINES=!LINES!━")
  31. REM 构建显存
  32. :DISRAM
  33. color 30&cls&set "MapData=000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000008000000000000000000000000000008000000000000000000000000000008000000000000000000000000000008000000000000000000000000000008000000000000000000000000000008000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
  34. for /l %%j in (1,1,%MapHeight%) do (
  35. set "DISPLAY[%%j]="
  36. set "tmp=!MapData:~0,%MapWidth%!"
  37. for /l %%i in (1,1,%MapWidth%) do (
  38. set "POINT[%%i][%%j]=!tmp:~0,1!"
  39. set tmp=!tmp:~1!
  40. if !POINT[%%i][%%j]! equ 3 (set/a "X=%%i,Y=%%j")
  41. if !POINT[%%i][%%j]! equ 5 (set/a "Xe=%%i,Ye=%%j,POINT[%%i][%%j]=8")
  42. for %%P in (!POINT[%%i][%%j]!) do (
  43. set "DISPLAY[%%j]=!DISPLAY[%%j]!!P%%P!"
  44. )
  45. )
  46. set "MapData=!MapData:~%MapWidth%!"
  47. )
  48. set/a "SS=POINT[%Xe%][%Ye%],dx=(8-(SS|1))*(SS&1),dy=(8-(SS|1))*((SS^1)&1)"
  49. set/a "Xo=7,Yo=3,POINT[7][13]=11,LEVEL=1,Add=11"
  50. ::****************主体程序******************
  51. :MAIN
  52. REM 绘图引擎
  53. if !F1! equ 1 (2>nul echo !SPACE!&set/p=<NUL) else cls
  54. echo                                       [LEVEL !LV!]  Score:!SC!
  55. echo ┏%LINES%┓
  56. for /l %%j in (1,1,%MapHeight%) do (if defined #%%j (echo;┃!DISPLAY[%%j]!┃&set "#%%j=") else (echo;))
  57. echo ┗%LINES%┛
  58. if !T! gtr 0 (for /l %%N in (1,1,!T!) do (for /l %%a in (1,1,100) do (echo;>NUL)))
  59. :READKEY
  60. REM 读取按键
  61. set/p KEY=<key.ini
  62. if "%KEY: =%"=="1" (if !dy! neq 1 (set/a "dx= 0,dy=-1,SS=8"))
  63. if %KEY% equ 2 (if !dy! neq -1 (set/a "dx= 0,dy= 1,SS=6"))
  64. if %KEY% equ 3 (if !dx! neq  1 (set/a "dx=-1,dy= 0,SS=9"))
  65. if %KEY% equ 4 (if !dx! neq -1 (set/a "dx= 1,dy= 0,SS=7"))
  66. if %KEY% equ 5 (goto READKEY)
  67. if %KEY% equ 6 (exit)
  68. set/a "S=POINT[%Xe%][%Ye%],Xf=X+dx,Yf=Y+dy"
  69. REM 判断蛇的状态
  70. if not defined POINT[%Xf%][%Yf%] (
  71. color C0&ping -n 2 127.1 >NUL&goto DISRAM
  72. )
  73. if !POINT[%Xf%][%Yf%]! lss 10 (
  74. if !POINT[%Xf%][%Yf%]! gtr 5 (
  75. color 50&ping -n 2 127.1 >NUL&goto DISRAM
  76. )
  77. )
  78. set/a "POINT[!Xe!][!Ye!]=0,POINT[!X!][!Y!]=SS,Mark=POINT[!Xf!][!Yf!]"
  79. REM 数据处理单元
  80. if !dy! neq 0 (
  81. for /l %%j in (!Y!,!dy!,!Yf!) do (
  82. set "DISPLAY[%%j]="
  83. set "#%%j=1"
  84. if !Yf! equ %%j (set POINT[%X%][%%j]=3)
  85. for /l %%i in (1,1,%MapWidth%) do (
  86. for %%P in (!POINT[%%i][%%j]!) do (
  87. set DISPLAY[%%j]=!DISPLAY[%%j]!!P%%P!
  88. )
  89. )
  90. )
  91. ) else (
  92. for /l %%j in (!Y!,1,!Y!) do (
  93. set "DISPLAY[%%j]="
  94. set "#%%j=1"
  95. for /l %%i in (1,1,%MapWidth%) do (
  96. if !Xf! equ %%i (set POINT[%%i][%Y%]=3)
  97. for %%P in (!POINT[%%i][%%j]!) do (
  98. set DISPLAY[%%j]=!DISPLAY[%%j]!!P%%P!
  99. )
  100. )
  101. )
  102. )
  103. if !Mark! lss 10 (
  104. for /l %%j in (!Ye!,1,!Ye!) do (
  105. set "DISPLAY[%%j]="
  106. set "#%%j=1"
  107. for /l %%i in (1,1,%MapWidth%) do (
  108. for %%P in (!POINT[%%i][%%j]!) do (
  109. set DISPLAY[%%j]=!DISPLAY[%%j]!!P%%P!
  110. )
  111. )
  112. )
  113. set/a "Xe+=(8-(S|1))*(S&1),Ye+=(8-(S|1))*((S^1)&1)"
  114. ) else (
  115. set/a "POINT[!Xe!][!Ye!]=S,SC+=Add,TP=LV*50,Mark1=1"
  116. if !SC! gtr !TP! (set/a "LV+=1,T-=1")
  117. )
  118. REM 时分复用随机
  119. if !Mark1! equ 1 (
  120. for /l %%i in (1,1,30) do (
  121. if !POINT[%Xo%][%Yo%]! neq 0 (
  122. set/a "Xo=1+!random!%%MapHeight,Yo=1+!random!%%MapWidth"
  123. )
  124. )
  125. if !POINT[%Xo%][%Yo%]! equ 0 (
  126. set/a "POINT[!Xo!][!Yo!]=!random!%%8+10,Add=POINT[!Xo!][!Yo!],Mark1=0"
  127. )
  128. )
  129. set/a "X=Xf,Y=Yf"
  130. goto MAIN
复制代码
[2016/9/21]贪吃蛇C语言版 Version1.0
  该版本是由批处理版改编而成,但风格迥异,该C语言蛇具有一定的智力,它不会傻到自己撞墙,除非你强迫它撞。当然这个智力已经接近自动驾驶了,但是它太贪吃了,连自己的身体也吃,你要保证它不吃到自己就行。WSAD 上下左右、Q退出,P暂停,再次恢复行走。“△”表示食物,⊙代表蛇头。该蛇具备一定的模拟生物应急性,有一定的个性,同时向随机游动粒子致敬O(∩_∩)O~
  1. ::**************Happy's 贪吃蛇C语言AI版*********
  2. REM 使用说明
  3. REM WSAD 上下左右
  4. REM Q退出,P暂停,再次恢复行走
  5. @certutil -decode "%~f0" "%~dp0snake.exe"&cls&color F0&mode con cols=68 lines=38&snake.exe 50 25 1&exit /b
  6. -----BEGIN CERTIFICATE-----
  7. TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  8. AAAAAAAAAAAAAAAAgAAAAA4fug4A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E6duHwKfGPRQHvq2STI/pgKpYMOr
  40. APcztsAAwiyNReicXHdG5w3tkdQHA/thD2emhbiEuQHqbpvdzIw11FD8CdwDZlvG
  41. muDkHZQUlNy2zXya4OQMGVbYDtgHsmfGQJyZAIcst/zd/yRVjWwkBFGJ6YHJHY2F
  42. AS0AED0Epi58+33sKcEPieCJzBD/YP2Nzdrf7MPBqIsAAQnRrC57V467pIFQw4tl
  43. 6BSs79zCpz26HZfSA18OtFWLhLe7b20IjUQkpwAxwAQEZKEMG3lz2/jsIwcMuOAQ
  44. HeGz7+0U7QhkoyBdx/8lvOUyyCCfB8jAzMgggwzQ1Ngggwwy3ODkgwwyyOjs8PQO
  45. Msgg+PwAIlUFZk4EAHSDfY/sAAgDDwcee9N0gxoDBwKps6mlASi7HSi3CkGnoQAn
  46. yroWPUG7g78A//8v26H2AtH3AMnfzKvMsMHLo6zBrNfUvLq13L/7/8TI4tKysru3
  47. xbn9o6EesbvE47qmy8AiPfc/9g4IrNPOz7e94cr4EkZf4P/TTKFbTEVWRUwgJWRd
  48. 5FNjb3Iw/t/aZToKrLLOyv207c7zoaMKpQWErSoNogJkBf+/bI5gvBEBR2V0U3Rk
  49. SGFuZGxlb2+/LFNDb25zbwtDdXIGclBv/2m29nNpdGkRMG3IbWVtY3B5ffvlbwdw
  50. cmludGZjbG9jawZfa2Joa275rioHZ2V0Y2hyUQU7W/ezbm1VcwxhdG9pIGNOdPff
  51. drdyYWZwC19zLV9hcHBfdHlwKI1d/l9fPW1haW5hcmdzZXhRWNvN3L9vdEZpbHRl
  52. cgwTBmNlFcsTOnZfaLdyM8tQRUwX9k/IAQLgAA8DCwEGExy62dN0DRAgQAs2BO33
  53. kk0HGhfxYQ/0BvadAygQBwaAkOVlSYAhPBcGkm68V1AuGnh0C5vNdgfADpDPI4J9
  54. C3fqYC5kgmGnEvtvdwv76BIn8sAAMAdcIgAAAADMANUACQAA/wAAAAAAAAAAAAAA
  55. YL4VUEAAjb7rv///V4PN/+sQkJCQk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AAAAAAAAYGAAADxgAAAAAAAAAAAAAAAAAABtYAAAWGAAAAAAAAAAAAAA
  72. AAAAAAAAAAAAAAAAeGAAAIZgAACWYAAApmAAALRgAADCYAAAAAAAANBgAAAAAAAA
  73. S0VSTkVMMzIuRExMAG1zdmNydC5kbGwAAABMb2FkTGlicmFyeUEAAEdldFByb2NB
  74. ZGRyZXNzAABWaXJ0dWFsUHJvdGVjdAAAVmlydHVhbEFsbG9jAABWaXJ0dWFsRnJl
  75. ZQAAAEV4aXRQcm9jZXNzAAAAcmFuZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  76. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  77. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  78. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  79. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  80. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  81. AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
  82. -----END CERTIFICATE-----
复制代码
  涉及到的C代码如下,就是批处理版的C翻译Version1.2,增加识别自己肉的AI,不会再咬自己
  1. #include   <conio.h>
  2. #include   <stdio.h>
  3. #include  <stdlib.h>
  4. #include    <time.h>
  5. #include <Windows.h>
  6. /**********按键定义**************/
  7. #define U 8
  8. #define D 6
  9. #define L 9
  10. #define R 7
  11. /**********初始化参数************/
  12. int Level=1,Score=0,ST;
  13. int POINT_V[31][31],SS=8,S=8;
  14. int Xe=15,Ye=30,X=15,Y=26,Xf,Yf,Xo=7,Yo=2;
  15. /**********光标位置函数**********/
  16. void Pos(int x,int y)
  17. {
  18. COORD pos;
  19. HANDLE hOutput;
  20. pos.X=x;
  21. pos.Y=y;
  22. hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
  23. SetConsoleCursorPosition(hOutput,pos);
  24. }
  25. /***********标数转换器***********/
  26. int Dx(int N){
  27. return (8-(N|1))*(N&1);
  28. }
  29. int Dy(int N){
  30. return (8-(N|1))*((N^1)&1);
  31. }
  32. /***********判断自己的肉*********/
  33. int Sme(int N){
  34. if(N>5 && N<10){return 1;}
  35. return 0;
  36. }
  37. /**********初始化地图************/
  38. void CreatMap()
  39. {
  40. int i,j;
  41. printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
  42. for(j=1;j<=30;j++) {printf("┃                              ┃\n");}
  43. printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛");
  44. for(j=1;j<=30;j++){
  45. for(i=1; i<=30;i++){
  46. POINT_V[i][j]=0;
  47. }
  48. }
  49. for(j=27;j<=30;j++){
  50. Pos(2*15,j);printf("■");
  51. POINT_V[15][j]=8;
  52. }
  53. Pos(2*15,26);printf("⊙");
  54. Pos(2*7,2);printf("△");
  55. POINT_V[15][26]=3,POINT_V[7][2]=10;
  56. }
  57. /**********按键获取**************/
  58. int Getkey(int N,int T)
  59. {
  60. int i,KEY_V,start=clock();
  61. do{
  62. if(kbhit()){
  63. KEY_V=(int)(getch());
  64. if(KEY_V<97){KEY_V+=32;}
  65. return KEY_V;
  66. }
  67. for(i=0;i<=N;i++);
  68. }while((clock()-start)<T);
  69. return -1;
  70. }
  71. /***********游戏主程序***********/
  72. int GameCycle(int N, int T, int F1)
  73. {
  74. int KEY_V;
  75. while(1) {
  76. KEY_V=Getkey(N,T);
  77. if     (KEY_V==119 && SS!=D){SS=8;}
  78. else if(KEY_V==115 && SS!=U){SS=6;}
  79. else if(KEY_V==97  && SS!=R){SS=9;}
  80. else if(KEY_V==100 && SS!=L){SS=7;}
  81. else if(KEY_V==112){getch();}
  82. else if(KEY_V==113){return 0;}
  83. Pos(2,31);           
  84. S=POINT_V[Xe][Ye],Xf=X+Dx(SS),Yf=Y+Dy(SS);                         //数据处理
  85. if(Sme(POINT_V[Xf][Yf])){
  86. if(F1==1 && (KEY_V<0)){
  87. if(Dy(SS)==0){
  88. ST=POINT_V[Xf][Yf],SS=(ST==6)?8:6,Xf=X,Yf=Y+Dy(SS);
  89. }
  90. else if(Dx(SS)==0){
  91. ST=POINT_V[Xf][Yf],SS=(ST==7)?9:7,Xf=X+Dx(SS),Yf=Y;
  92. }
  93. }else{
  94. if(KEY_V<0){
  95. printf("蛇太贪了,连自己的肉也不放过!");
  96. }else{
  97. printf("蛇被你害死了!");
  98. }
  99. getch();
  100. return 0;
  101. }
  102. }
  103. if(F1==1 && (KEY_V<0)){
  104. if(Xf==0 || Xf==31 || Yf==0 || Yf==31){
  105. if(!Dy(SS)){
  106. SS=6+2*(rand()%2),Xf=X,Yf=Y+Dy(SS);
  107. if(Yf==0 || Yf==31){
  108. SS=(SS==6)?8:6,Yf=Y+Dy(SS);
  109. }
  110. }else{
  111. SS=7+2*(rand()%2),Xf=X+Dx(SS),Yf=Y;
  112. if(Xf==0 || Xf==31){
  113. SS=(SS==7)?9:7,Xf=X+Dx(SS);
  114. }
  115. }
  116. }
  117. if(Dy(SS)==0 && X==Xo){
  118. ST=SS,SS=(Y-Yo)>0?8:6,Xf=X,Yf=Y+Dy(SS);
  119. if(Sme(POINT_V[Xf][Yf])){SS=ST,Xf=X+Dx(SS),Yf=Y+Dy(SS);}
  120. }
  121. else if(Dx(SS)==0 && Y==Yo){
  122. ST=SS,SS=(X-Xo)>0?9:7,Xf=X+Dx(SS),Yf=Y;
  123. if(Sme(POINT_V[Xf][Yf])){SS=ST,Xf=X+Dx(SS),Yf=Y+Dy(SS);}
  124. }
  125. }
  126. else if(Xf==0 || Xf==31 || Yf==0 || Yf==31){
  127. printf("蛇死了,游戏结束!");
  128. getch();
  129. return 0;
  130. }
  131. if(Sme(POINT_V[Xf][Yf])){
  132. printf("蛇已被困住了!!!");
  133. getch();
  134. return 0;
  135. }
  136. Pos(2*Xf,Yf);printf("⊙");                                         //动态显示
  137. Pos(2*X,Y);printf("■");
  138. POINT_V[X][Y]=SS;
  139. if(POINT_V[Xf][Yf]<10){
  140. Pos(2*Xe,Ye);printf(" ");
  141. POINT_V[X][Y]=SS,POINT_V[Xe][Ye]=0;
  142. Xe+=Dx(S),Ye+=Dy(S);
  143. }else{
  144. do{
  145. Xo=1+rand()%30,Yo=1+rand()%30;                     //随机食物
  146. if(POINT_V[Xo][Yo]==0){break;}
  147. }while(POINT_V[Xo][Yo]!=0);
  148. Pos(2*Xo,Yo);printf("△");
  149. POINT_V[Xo][Yo]=10,Score++;
  150. if(Score>Level*5){
  151. Level++,T-=1;
  152. }
  153. }
  154. X=Xf,Y=Yf;
  155. Pos(2,0);printf("[LEVEL %d]  Score:%d\n",Level,Score);
  156. }
  157. }
  158. /**********Main主函数***********/
  159. int main(char argc, char* argv[])
  160. {
  161. srand((unsigned)time(NULL));
  162. int N,T,F;
  163. if(argc!=4){
  164. printf("参数错误。\n");
  165. return 1;
  166. }else{
  167. N=atoi(argv[1]);
  168. T=atoi(argv[2]);
  169. F=atoi(argv[3]);
  170. }
  171. CreatMap();
  172. GameCycle(N,T,F);
  173. return 0;
  174. }
复制代码
  核心部分用到了如标数转换器,就是如果用6、7、8、9这四个数表示方向,那么只用一行逻辑语句就可分离出坐标分量。减小了按键处理的复杂度。


[2016/9/21]根据贪吃蛇的start cmd /k启动方式,做的同类型启动方式游戏“看美女”下载地址http://pan.baidu.com/s/1miLdpuo,版本Version1.0。WSAD 上下左右,Q退出、N下张、P暂停、方向键恢复滑动。用的是第三方bmp.exe的切片功能。很养眼!
  1. @echo off&setlocal enabledelayedexpansion
  2. PUSHD "%~dp0images"
  3. REM 设置滑动速度
  4.   set SPEED=25
  5. ::***************Happy's 看美女**************
  6. REM 使用说明
  7. REM WSAD 上下左右
  8. REM Q退出,N下张,P暂停,方向键恢复滑动
  9. ::***************控制窗口********************
  10. if "%1"=="CONTROLP" (
  11. MODE CON cols=15 lines=1
  12. for /l %%i in () do (
  13. choice /C WSADPQN>NUL
  14. echo !errorlevel!>key.ini&if !errorlevel! equ 6 (exit)
  15. )
  16. )
  17. start "" "%~f0" CONTROLP
  18. ::****************主体程序*******************
  19. title Happy'S Look Beauty
  20. MODE CON cols=65 lines=20
  21. REM LOGO
  22. bmp 0 0 520 360 0 0 logo.bmp
  23. ping -n 3 127.1 >NUL
  24. set/a "N=1,X=0,Y=SPEED,XEND=SPEED*(2000/SPEED),YEND=SPEED*(1200/SPEED)"
  25. echo 4 >key.ini
  26. for /l %%i in () do (
  27. (
  28. if !X! gtr  !XEND! (echo 3)
  29. if !X! lss !SPEED! (echo 4)
  30. if !Y! gtr  !YEND! (echo 1)
  31. if !Y! lss !SPEED! (echo 2)
  32. ) >key.ini
  33. bmp 0 0 520 360 !X! !Y! !N!.bmp
  34. set/p KEY=<key.ini
  35. if !KEY! equ 1 (set/a Y-=SPEED)
  36. if !KEY! equ 2 (set/a Y+=SPEED)
  37. if !KEY! equ 3 (set/a X-=SPEED)
  38. if "!KEY: =!"=="4" (set/a X+=SPEED)
  39. if !KEY! equ 6 (exit)
  40. if !KEY! equ 7 (
  41. set/a "N+=1,X=0"
  42. if !N! gtr 3 (set N=1)
  43. )
  44. )
复制代码

作者: 523066680    时间: 2016-9-21 01:33

本帖最后由 523066680 于 2016-9-21 02:32 编辑

良心批处理,代码很工整。C的那部分代码,申请授权转发~

还未细看,你的延时用的是 for(i=0;i<=N;i++); ?
建议用 _sleep ,或者usleep
  1. unistd.h:int __cdecl __MINGW_NOTHROW usleep(useconds_t);
  2. stdlib.h:  _CRTIMP void __cdecl _sleep(unsigned long _Duration) __MINGW_ATTRIB_DEPRECATED;
复制代码
死亡之吻没必要了,that's not funny.

试了C代码,须保存为ANSI(GBK)编码文件,调用的时候需要传3个参数(N, T, F)
N是延时中For循环的次数,设置精细一些按键响应快一些;T是延时的实际延时间隔(1000 = 1秒);F好像是模式

建议设置最小阀值和最大阀值,限定延时范围
作者: happy886rr    时间: 2016-9-21 02:43

本帖最后由 happy886rr 于 2016-9-21 22:42 编辑

回复 2# 523066680
感谢点赞,转发。那个循环是用来改善按键检测密度的,sleep只能延时毫秒级别,但那个地方需要的是微秒级的延时,只能用循环实现。C代码已更新,加入识别自肉的功能,不过没有人为干预,蛇会把自己困死,其实蛇只要一直寻找自己的尾巴就不会死,在寻找尾巴的过程中蛇要权衡哪个食物可以吃,哪个吃了会把自己害死。估计再有80行AI蛇才能铺满整个屏。
作者: codegay    时间: 2016-9-21 12:38

回复 2# 523066680


    你该这样说,楼主能不能把这些C的内容也转发一份到我的论坛。。。
这样又赚用户又赚内容。。。
作者: aa77dd@163.com    时间: 2016-9-22 16:54

回复 1# happy886rr

楼主 此例中

缓冲区宽 68, 要回退的行数是 33, 回退前 光标水平坐标是 0
可用如下公式计算出 所需退格数  (最前面的 2 在 回退前 光标水平坐标是 0 时都适用)

2 + ceil(68 / 8) * 33 = 2 + 9 * 33 = 299




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