本帖最后由 happy886rr 于 2016-9-21 22:28 编辑
[2016/9/21]贪吃蛇bat版 Version1.3
2016版贪吃蛇,采用动态显示算法,时分复用随机,具有很高的流畅性且不会闪屏。核心模块完全是在新版推箱子的基础上改编的。WSAD 上下左右、Q退出,P暂停,方向键恢复行走。采用屏幕背景色来显示蛇的状态和游戏的gameover,即绿屏表示蛇正常,红屏表示蛇撞墙,紫屏表示蛇自杀。“☆★○●◎△▲¤⊙”表示食物,相应的分值渐增。⊙代表蛇头。- @echo off
- title &setlocal enabledelayedexpansion
- ::**************Happy's 贪吃蛇**************
- REM 设置蛇的时钟,T值越小速度越快
- set T=3
- REM 开启防闪烁,用于画质增强
- set F1=1
- REM WSAD 上下左右
- REM Q退出,P暂停,方向键恢复行走
- REM 绿屏表示蛇正常,红屏表示蛇撞墙,紫屏表示蛇自杀
-
- ::**************方向控制台窗口**************
- if "%1"=="CONTROLP" (
- MODE CON cols=15 lines=1
- echo 1 >key.ini
- for /l %%i in () do (
- choice /C WSADPQ>NUL
- echo !errorlevel!>key.ini&if !errorlevel! equ 6 (exit)
- )
- )
- start "" "%~f0" CONTROLP
-
- ::****************先导程序******************
- REM 默认皮肤
- set "ELEMENTS= □◇⊙★■■■■■☆★○●◎△▲¤⊙"
- REM 设置窗体
- mode con cols=68 lines=38
- REM 初始化元素
- set/a "MapHeight=30,MapWidth=30"
- for /l %%i in (1,1,500) do (set "SPACE=!SPACE!")
- for /l %%N in (0,1,19) do (set "P%%N=!ELEMENTS:~%%N,1!")
- for /l %%i in (1,1,%MapWidth%) do (set "LINES=!LINES!━")
- REM 构建显存
- :DISRAM
- color 30&cls&set "MapData=000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000008000000000000000000000000000008000000000000000000000000000008000000000000000000000000000008000000000000000000000000000008000000000000000000000000000008000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
- for /l %%j in (1,1,%MapHeight%) do (
- set "DISPLAY[%%j]="
- set "tmp=!MapData:~0,%MapWidth%!"
- for /l %%i in (1,1,%MapWidth%) do (
- set "POINT[%%i][%%j]=!tmp:~0,1!"
- set tmp=!tmp:~1!
- if !POINT[%%i][%%j]! equ 3 (set/a "X=%%i,Y=%%j")
- if !POINT[%%i][%%j]! equ 5 (set/a "Xe=%%i,Ye=%%j,POINT[%%i][%%j]=8")
- for %%P in (!POINT[%%i][%%j]!) do (
- set "DISPLAY[%%j]=!DISPLAY[%%j]!!P%%P!"
- )
- )
- set "MapData=!MapData:~%MapWidth%!"
- )
- set/a "SS=POINT[%Xe%][%Ye%],dx=(8-(SS|1))*(SS&1),dy=(8-(SS|1))*((SS^1)&1)"
- set/a "Xo=7,Yo=3,POINT[7][13]=11,LEVEL=1,Add=11"
-
- ::****************主体程序******************
- :MAIN
- REM 绘图引擎
- if !F1! equ 1 (2>nul echo !SPACE!&set/p=<NUL) else cls
- echo [LEVEL !LV!] Score:!SC!
- echo ┏%LINES%┓
- for /l %%j in (1,1,%MapHeight%) do (if defined #%%j (echo;┃!DISPLAY[%%j]!┃&set "#%%j=") else (echo;))
- echo ┗%LINES%┛
- if !T! gtr 0 (for /l %%N in (1,1,!T!) do (for /l %%a in (1,1,100) do (echo;>NUL)))
- :READKEY
- REM 读取按键
- set/p KEY=<key.ini
- if "%KEY: =%"=="1" (if !dy! neq 1 (set/a "dx= 0,dy=-1,SS=8"))
- if %KEY% equ 2 (if !dy! neq -1 (set/a "dx= 0,dy= 1,SS=6"))
- if %KEY% equ 3 (if !dx! neq 1 (set/a "dx=-1,dy= 0,SS=9"))
- if %KEY% equ 4 (if !dx! neq -1 (set/a "dx= 1,dy= 0,SS=7"))
- if %KEY% equ 5 (goto READKEY)
- if %KEY% equ 6 (exit)
- set/a "S=POINT[%Xe%][%Ye%],Xf=X+dx,Yf=Y+dy"
- REM 判断蛇的状态
- if not defined POINT[%Xf%][%Yf%] (
- color C0&ping -n 2 127.1 >NUL&goto DISRAM
- )
- if !POINT[%Xf%][%Yf%]! lss 10 (
- if !POINT[%Xf%][%Yf%]! gtr 5 (
- color 50&ping -n 2 127.1 >NUL&goto DISRAM
- )
- )
- set/a "POINT[!Xe!][!Ye!]=0,POINT[!X!][!Y!]=SS,Mark=POINT[!Xf!][!Yf!]"
- REM 数据处理单元
- if !dy! neq 0 (
- for /l %%j in (!Y!,!dy!,!Yf!) do (
- set "DISPLAY[%%j]="
- set "#%%j=1"
- if !Yf! equ %%j (set POINT[%X%][%%j]=3)
- for /l %%i in (1,1,%MapWidth%) do (
- for %%P in (!POINT[%%i][%%j]!) do (
- set DISPLAY[%%j]=!DISPLAY[%%j]!!P%%P!
- )
- )
- )
- ) else (
- for /l %%j in (!Y!,1,!Y!) do (
- set "DISPLAY[%%j]="
- set "#%%j=1"
- for /l %%i in (1,1,%MapWidth%) do (
- if !Xf! equ %%i (set POINT[%%i][%Y%]=3)
- for %%P in (!POINT[%%i][%%j]!) do (
- set DISPLAY[%%j]=!DISPLAY[%%j]!!P%%P!
- )
- )
- )
- )
- if !Mark! lss 10 (
- for /l %%j in (!Ye!,1,!Ye!) do (
- set "DISPLAY[%%j]="
- set "#%%j=1"
- for /l %%i in (1,1,%MapWidth%) do (
- for %%P in (!POINT[%%i][%%j]!) do (
- set DISPLAY[%%j]=!DISPLAY[%%j]!!P%%P!
- )
- )
- )
- set/a "Xe+=(8-(S|1))*(S&1),Ye+=(8-(S|1))*((S^1)&1)"
- ) else (
- set/a "POINT[!Xe!][!Ye!]=S,SC+=Add,TP=LV*50,Mark1=1"
- if !SC! gtr !TP! (set/a "LV+=1,T-=1")
- )
- REM 时分复用随机
- if !Mark1! equ 1 (
- for /l %%i in (1,1,30) do (
- if !POINT[%Xo%][%Yo%]! neq 0 (
- set/a "Xo=1+!random!%%MapHeight,Yo=1+!random!%%MapWidth"
- )
- )
- if !POINT[%Xo%][%Yo%]! equ 0 (
- set/a "POINT[!Xo!][!Yo!]=!random!%%8+10,Add=POINT[!Xo!][!Yo!],Mark1=0"
- )
- )
- set/a "X=Xf,Y=Yf"
- goto MAIN
复制代码 [2016/9/21]贪吃蛇C语言版 Version1.0
该版本是由批处理版改编而成,但风格迥异,该C语言蛇具有一定的智力,它不会傻到自己撞墙,除非你强迫它撞。当然这个智力已经接近自动驾驶了,但是它太贪吃了,连自己的身体也吃,你要保证它不吃到自己就行。WSAD 上下左右、Q退出,P暂停,再次恢复行走。“△”表示食物,⊙代表蛇头。该蛇具备一定的模拟生物应急性,有一定的个性,同时向随机游动粒子致敬O(∩_∩)O~- ::**************Happy's 贪吃蛇C语言AI版*********
- REM 使用说明
- REM WSAD 上下左右
- REM Q退出,P暂停,再次恢复行走
- @certutil -decode "%~f0" "%~dp0snake.exe"&cls&color F0&mode con cols=68 lines=38&snake.exe 50 25 1&exit /b
- -----BEGIN CERTIFICATE-----
-
- TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- AAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v
- dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAATAEDAAAAAAAAAAAA
- AAAAAOAADwMLAQYAABAAAAAQAAAAQAAAAFcAAABQAAAAYAAAAABAAAAQAAAAAgAA
- BAAAAAAAAAAEAAAAAAAAAABwAAAAEAAAAAAAAAMAAAAAABAAABAAAAAAEAAAEAAA
- AAAAABAAAAAAAAAAAAAAAABgAADYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- AAA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AAAAAAAAAAAAAAAAAAAAA
- AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
- -----END CERTIFICATE-----
复制代码 涉及到的C代码如下,就是批处理版的C翻译Version1.2,增加识别自己肉的AI,不会再咬自己- #include <conio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <Windows.h>
-
- /**********按键定义**************/
- #define U 8
- #define D 6
- #define L 9
- #define R 7
-
- /**********初始化参数************/
- int Level=1,Score=0,ST;
- int POINT_V[31][31],SS=8,S=8;
- int Xe=15,Ye=30,X=15,Y=26,Xf,Yf,Xo=7,Yo=2;
-
- /**********光标位置函数**********/
- void Pos(int x,int y)
- {
- COORD pos;
- HANDLE hOutput;
- pos.X=x;
- pos.Y=y;
- hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(hOutput,pos);
- }
-
- /***********标数转换器***********/
- int Dx(int N){
- return (8-(N|1))*(N&1);
- }
- int Dy(int N){
- return (8-(N|1))*((N^1)&1);
- }
-
- /***********判断自己的肉*********/
- int Sme(int N){
- if(N>5 && N<10){return 1;}
- return 0;
- }
-
- /**********初始化地图************/
- void CreatMap()
- {
- int i,j;
- printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
- for(j=1;j<=30;j++) {printf("┃ ┃\n");}
- printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛");
-
- for(j=1;j<=30;j++){
- for(i=1; i<=30;i++){
- POINT_V[i][j]=0;
- }
- }
- for(j=27;j<=30;j++){
- Pos(2*15,j);printf("■");
- POINT_V[15][j]=8;
- }
- Pos(2*15,26);printf("⊙");
- Pos(2*7,2);printf("△");
- POINT_V[15][26]=3,POINT_V[7][2]=10;
- }
-
- /**********按键获取**************/
- int Getkey(int N,int T)
- {
- int i,KEY_V,start=clock();
- do{
- if(kbhit()){
- KEY_V=(int)(getch());
- if(KEY_V<97){KEY_V+=32;}
- return KEY_V;
- }
- for(i=0;i<=N;i++);
- }while((clock()-start)<T);
- return -1;
- }
-
- /***********游戏主程序***********/
- int GameCycle(int N, int T, int F1)
- {
- int KEY_V;
- while(1) {
- KEY_V=Getkey(N,T);
- if (KEY_V==119 && SS!=D){SS=8;}
- else if(KEY_V==115 && SS!=U){SS=6;}
- else if(KEY_V==97 && SS!=R){SS=9;}
- else if(KEY_V==100 && SS!=L){SS=7;}
- else if(KEY_V==112){getch();}
- else if(KEY_V==113){return 0;}
- Pos(2,31);
- S=POINT_V[Xe][Ye],Xf=X+Dx(SS),Yf=Y+Dy(SS); //数据处理
- if(Sme(POINT_V[Xf][Yf])){
- if(F1==1 && (KEY_V<0)){
- if(Dy(SS)==0){
- ST=POINT_V[Xf][Yf],SS=(ST==6)?8:6,Xf=X,Yf=Y+Dy(SS);
- }
- else if(Dx(SS)==0){
- ST=POINT_V[Xf][Yf],SS=(ST==7)?9:7,Xf=X+Dx(SS),Yf=Y;
- }
- }else{
- if(KEY_V<0){
- printf("蛇太贪了,连自己的肉也不放过!");
- }else{
- printf("蛇被你害死了!");
- }
- getch();
- return 0;
- }
- }
- if(F1==1 && (KEY_V<0)){
- if(Xf==0 || Xf==31 || Yf==0 || Yf==31){
- if(!Dy(SS)){
- SS=6+2*(rand()%2),Xf=X,Yf=Y+Dy(SS);
- if(Yf==0 || Yf==31){
- SS=(SS==6)?8:6,Yf=Y+Dy(SS);
- }
- }else{
- SS=7+2*(rand()%2),Xf=X+Dx(SS),Yf=Y;
- if(Xf==0 || Xf==31){
- SS=(SS==7)?9:7,Xf=X+Dx(SS);
- }
- }
- }
- if(Dy(SS)==0 && X==Xo){
- ST=SS,SS=(Y-Yo)>0?8:6,Xf=X,Yf=Y+Dy(SS);
- if(Sme(POINT_V[Xf][Yf])){SS=ST,Xf=X+Dx(SS),Yf=Y+Dy(SS);}
- }
- else if(Dx(SS)==0 && Y==Yo){
- ST=SS,SS=(X-Xo)>0?9:7,Xf=X+Dx(SS),Yf=Y;
- if(Sme(POINT_V[Xf][Yf])){SS=ST,Xf=X+Dx(SS),Yf=Y+Dy(SS);}
- }
- }
- else if(Xf==0 || Xf==31 || Yf==0 || Yf==31){
- printf("蛇死了,游戏结束!");
- getch();
- return 0;
- }
- if(Sme(POINT_V[Xf][Yf])){
- printf("蛇已被困住了!!!");
- getch();
- return 0;
- }
- Pos(2*Xf,Yf);printf("⊙"); //动态显示
- Pos(2*X,Y);printf("■");
- POINT_V[X][Y]=SS;
- if(POINT_V[Xf][Yf]<10){
- Pos(2*Xe,Ye);printf(" ");
- POINT_V[X][Y]=SS,POINT_V[Xe][Ye]=0;
- Xe+=Dx(S),Ye+=Dy(S);
- }else{
- do{
- Xo=1+rand()%30,Yo=1+rand()%30; //随机食物
- if(POINT_V[Xo][Yo]==0){break;}
- }while(POINT_V[Xo][Yo]!=0);
- Pos(2*Xo,Yo);printf("△");
- POINT_V[Xo][Yo]=10,Score++;
- if(Score>Level*5){
- Level++,T-=1;
- }
- }
- X=Xf,Y=Yf;
- Pos(2,0);printf("[LEVEL %d] Score:%d\n",Level,Score);
- }
- }
-
- /**********Main主函数***********/
- int main(char argc, char* argv[])
- {
- srand((unsigned)time(NULL));
- int N,T,F;
- if(argc!=4){
- printf("参数错误。\n");
- return 1;
- }else{
- N=atoi(argv[1]);
- T=atoi(argv[2]);
- F=atoi(argv[3]);
- }
- CreatMap();
- GameCycle(N,T,F);
- return 0;
- }
复制代码 核心部分用到了如标数转换器,就是如果用6、7、8、9这四个数表示方向,那么只用一行逻辑语句就可分离出坐标分量。减小了按键处理的复杂度。
[2016/9/21]根据贪吃蛇的start cmd /k启动方式,做的同类型启动方式游戏“看美女”下载地址http://pan.baidu.com/s/1miLdpuo,版本Version1.0。WSAD 上下左右,Q退出、N下张、P暂停、方向键恢复滑动。用的是第三方bmp.exe的切片功能。很养眼!- @echo off&setlocal enabledelayedexpansion
- PUSHD "%~dp0images"
- REM 设置滑动速度
- set SPEED=25
- ::***************Happy's 看美女**************
- REM 使用说明
- REM WSAD 上下左右
- REM Q退出,N下张,P暂停,方向键恢复滑动
-
- ::***************控制窗口********************
- if "%1"=="CONTROLP" (
- MODE CON cols=15 lines=1
- for /l %%i in () do (
- choice /C WSADPQN>NUL
- echo !errorlevel!>key.ini&if !errorlevel! equ 6 (exit)
- )
- )
- start "" "%~f0" CONTROLP
-
- ::****************主体程序*******************
- title Happy'S Look Beauty
- MODE CON cols=65 lines=20
- REM LOGO
- bmp 0 0 520 360 0 0 logo.bmp
- ping -n 3 127.1 >NUL
- set/a "N=1,X=0,Y=SPEED,XEND=SPEED*(2000/SPEED),YEND=SPEED*(1200/SPEED)"
- echo 4 >key.ini
- for /l %%i in () do (
- (
- if !X! gtr !XEND! (echo 3)
- if !X! lss !SPEED! (echo 4)
- if !Y! gtr !YEND! (echo 1)
- if !Y! lss !SPEED! (echo 2)
- ) >key.ini
- bmp 0 0 520 360 !X! !Y! !N!.bmp
- set/p KEY=<key.ini
- if !KEY! equ 1 (set/a Y-=SPEED)
- if !KEY! equ 2 (set/a Y+=SPEED)
- if !KEY! equ 3 (set/a X-=SPEED)
- if "!KEY: =!"=="4" (set/a X+=SPEED)
- if !KEY! equ 6 (exit)
- if !KEY! equ 7 (
- set/a "N+=1,X=0"
- if !N! gtr 3 (set N=1)
- )
- )
复制代码
|