标题: C 语言贪吃蛇 [打印本页]
作者: Gin_Q 时间: 2020-8-27 18:33 标题: C 语言贪吃蛇
本帖最后由 Gin_Q 于 2020-8-27 20:33 编辑
# 思路来自(知乎 id = int)int
# 还有些功能没有写!- /*
- @ BY COOL_BREEZE
- @ 2020-08-27
- */
-
-
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- #include<conio.h>
- #include<windows.h>
-
- #define HIGHT 30
- #define WIDTH 100
-
- // dos窗口显示模式
- #define MODE(X, Y) "MODE CON COLS="#X" LINES="#Y
- #define HAD (GetStdHandle(STD_OUTPUT_HANDLE))
-
- // 蛇的结构体开辟内存空间
- #define CALLSNAKE ((SNAKEP)malloc(sizeof(SNAKEV)))
-
- void hidecursor(void); // 改变光标的显示方式
- void gotoxy_print(int x, int y, char icon); // 移动光标到指定位置打印图标
- void __init__(void); // 初始化边框
- void create_snake(void); // 生成蛇
- void move_snake(void); // 移动蛇
- void create_food(void); // 生成食物
- void get_keyborad_input(void); // 获取键盘值
- int hit_the_wall(void); // 蛇撞墙
- void game_method(void);
-
- typedef struct SNAKE
- {
- int x;
- int y;
- struct SNAKE* next;
- } SNAKEV, *SNAKEP;
-
- SNAKEP SNAKE_HEAD; //蛇头
- SNAKEP SNAKE_FOOD; //食物
-
- //蛇前进的方向
- #define U 0X48 // 上
- #define D 0X50 // 下
- #define L 0X4B // 左
- #define R 0X4D // 右
- #define S 0X7F // 空格 暂停
-
- char DIRECTION = L; // 默认蛇往左边移动
- char HEAD_ICON = 0XF;
- char BODY_ICON = 0XB;
- char FOOD_ICON = 0X24;
- char NULL_ICON = 0X20;
-
- int main(void)
- {
- system(MODE(130, 30));
- system("title 贪吃蛇游戏");
- game_method();
- hidecursor();
- __init__();
- SNAKE_FOOD = CALLSNAKE;
- create_snake();
- create_food();
- int ser; // 延迟
- while(1)
- {
- ser = 0;
- move_snake();
- if (kbhit()) // 检查键盘是否被按下,非阻塞的 (相当于多线程)
- {
- get_keyborad_input();
- Sleep(20);
- ser = 1;
- }
- if (hit_the_wall())
- {
- gotoxy_print((WIDTH-8) / 2, HIGHT / 2, NULL_ICON);
- printf("游戏结束");
- system("pause >nul");
- break;
- }
- if (ser)
- Sleep(10);
- else Sleep(200);
- }
- return 0;
-
- }
-
- void hidecursor(void)
- {
- CONSOLE_CURSOR_INFO show = {1, 0};
- SetConsoleCursorInfo(HAD, &show);
- }
-
- void gotoxy_print(int x, int y, char icon)
- {
- COORD poin = {x, y}; // 结构体
- SetConsoleCursorPosition(HAD, poin); // 跳转到窗口指定位置
- printf("%c", icon);
- }
-
- void game_method(void)
- {
- gotoxy_print(WIDTH + 5, HIGHT/2-7, NULL_ICON);
- printf("DATE 2020-08-27");
- gotoxy_print(WIDTH + 5, HIGHT/2-6, NULL_ICON);
- printf("BY COOL_BREEZE");
- gotoxy_print(WIDTH + 5, HIGHT/2-4, NULL_ICON);
- printf("空格 暂停");
- gotoxy_print(WIDTH + 5, HIGHT/2-3, NULL_ICON);
- printf("向上 ↑");
- gotoxy_print(WIDTH + 5, HIGHT/2-2, NULL_ICON);
- printf("向下 ↓");
- gotoxy_print(WIDTH + 5, HIGHT/2-1, NULL_ICON);
- printf("向左 ←");
- gotoxy_print(WIDTH + 5, HIGHT/2, NULL_ICON);
- printf("向右 →");
- }
-
- void __init__(void)
- {
- int i = 0;
- gotoxy_print(0, 0, 0X1); // 角落 1
- gotoxy_print(WIDTH-1, 0, 0X2); // 角落 2
- for (i=1; i<WIDTH-1; i++) // 上下边框
- {
- gotoxy_print(i, 0, 0X6);
- gotoxy_print(i, HIGHT-1, 0X6);
- }
- for (i=1;i<HIGHT-1; i++) // 左右边框
- {
- gotoxy_print(0, i, 0X5);
- gotoxy_print(WIDTH-1, i, 0X5);
- }
- gotoxy_print(0, HIGHT-1, 0X3); // 角落 3
- gotoxy_print(WIDTH-1, HIGHT-1, 0X4); // 角落 4
- }
-
- void create_snake(void)
- {
- int i = 0,x = (WIDTH-6) / 2, y = HIGHT / 2; //在中心创建蛇
- SNAKEP phead, pend;
- for (; i<6; i++) // 蛇默认长度为6
- {
- pend = CALLSNAKE;
- if (!i)
- {
- SNAKE_HEAD = phead = pend;
- pend->x = x;
- pend->y = y;
- gotoxy_print(SNAKE_HEAD->x, SNAKE_HEAD->y, HEAD_ICON);
- }
- else
- {
- phead->next = pend;
- pend->x = x + i; // 蛇身体在头右方
- pend->y = y;
- gotoxy_print(pend->x, pend->y, BODY_ICON);
- phead = pend;
- }
- }
- pend->next = NULL;
- }
-
- void move_snake(void)
- {
- SNAKEP head = CALLSNAKE; // 蛇新的头部
- switch (DIRECTION)
- {
- case U:
- head->x = SNAKE_HEAD->x;
- head->y = SNAKE_HEAD->y - 1;
- break;
- case D:
- head->x = SNAKE_HEAD->x;
- head->y = SNAKE_HEAD->y + 1;
- break;
- case L:
- head->x = SNAKE_HEAD->x - 1;
- head->y = SNAKE_HEAD->y;
- break;
- case R:
- head->x = SNAKE_HEAD->x + 1;
- head->y = SNAKE_HEAD->y;
- break;
- case S:
- system("pause >nul");
- break;
- }
- head->next = SNAKE_HEAD; // 旧头部连接新头部 使旧头部成为身体
- SNAKE_HEAD = head; // 获取头部
- if (SNAKE_HEAD->x == SNAKE_FOOD->x && SNAKE_HEAD->y == SNAKE_FOOD->y) // 检查蛇是否吃到食物
- {
- while (head) // 吃到了食物蛇蛇尾不需要释放
- {
- if (head == SNAKE_HEAD)
- gotoxy_print(SNAKE_HEAD->x, SNAKE_HEAD->y, HEAD_ICON);
- else
- gotoxy_print(head->x, head->y, BODY_ICON);
- head = head->next;
- }
- create_food();
- }
- else
- {
- while (head->next->next) // 没吃到食物释放掉蛇尾
- {
- if (head == SNAKE_HEAD)
- gotoxy_print(SNAKE_HEAD->x, SNAKE_HEAD->y, HEAD_ICON);
- else
- gotoxy_print(head->x, head->y, BODY_ICON);
- head = head->next;
- }
- gotoxy_print(head->next->x, head->next->y, NULL_ICON);
- free(head->next);
- head->next = NULL;
- }
- }
-
- void create_food(void)
- {
- SNAKEP temp;
- while (1)
- {
- srand(time(NULL)); // 获取新种子 生成新的随机数
- SNAKE_FOOD->x = rand() % (WIDTH - 2) + 1; // 确保坐标只会出现在表格内(不包括表格)
- SNAKE_FOOD->y = rand() % (HIGHT - 2) + 1; // 确保坐标只会出现在表格内(不包括表格)
- temp = SNAKE_HEAD;
- while(temp) // 食物落在蛇身上重新获取
- {
- if (temp->x == SNAKE_FOOD->x && temp->y == SNAKE_FOOD->y)
- continue;
- else
- {
- gotoxy_print(SNAKE_FOOD->x, SNAKE_FOOD->y, FOOD_ICON);
- break;
- }
- }
- break;
- }
- }
-
- void get_keyborad_input(void)
- {
- int key = 0;
- while (1)
- {
- key = getch();
- if (key == 224) // 键盘方向键需要获取两次
- {
- key = getch();
- if (key == U ||\
- key == D ||\
- key == L ||\
- key == R)
- {
- DIRECTION = key;
- break;
- }
- else continue;
- }
- }
- }
-
- int hit_the_wall(void)
- {
- if (SNAKE_HEAD->x == 0 || SNAKE_HEAD->x == WIDTH-1 ||\
- SNAKE_HEAD->y == 0 || SNAKE_HEAD->y == HIGHT-1 )
- return 1;
- return 0;
- }
复制代码
作者: Batcher 时间: 2020-8-27 20:56
回复 1# Gin_Q
这个效果也挺酷:
http://bbs.bathome.net/thread-24093-1-1.html
作者: Gin_Q 时间: 2020-8-27 21:02
回复 2# Batcher
代码哪里有问题,卡卡的!难搞咯!
作者: wujunkai 时间: 2020-8-30 22:29
本帖最后由 wujunkai 于 2020-9-20 11:49 编辑
回复 3# Gin_Q
断断续续优化了2年,C++的- /* 贪吃蛇 3.40.0 */
- #include<cstdio>
- #include<windows.h>
- #define HIT
- #define UP 1
- #define LEFT 2
- #define DOWN 3
- #define RIGHT 4
- #define HEAD 5
- #define WALL 22
- #define Handle GetStdHandle(STD_OUTPUT_HANDLE)
- #define Pempty(a) Psame(a,' ')
- #define Key(a) GetAsyncKeyState(a)
- #define elif else if
- struct botton{
- int x ,y ;
- int length ;
- };
- struct bearing:POINT{
- void walk(int Flag);
- };
- struct Snake{
- int Sum ;
- int Flag ;
- int Grade ;
- int Color ;
- int Speed ;
- bool Life ;
- bool Active ;
- bearing End ;
- bearing Head ;
- Snake(int color,int speed,bool life) ;
- void Move() ;
- void Smarter() ;
- void Re(int flag,POINT pos) ;
- void Go(bool iiSmart,bool iiSmarter,int faster) ;
- void Direction(int up,int left,int down,int right) ;
- };
- bool Pcolor(int,int) ;
- bool Hit(botton,int) ;
- char*Psame(int,char) ;
- bool Pposition(int,int) ;
- void Dead() ;
- void Menu() ;
- void Foods() ;
- void Levels() ;
- void GradeIn() ;
- void Prepare() ;
- void Setting() ;
- void GradeInput() ;
- void Pedge(botton) ;
- void GradeOutput(int) ;
- void Walk(int,long&,long&) ;
- POINT Food = {} ;
- // 第一个人 第二个人 电脑
- Snake Frst(10,1,true) ,Scnd(14,1,false) ,Auto( 1,1,false) ;
- int Level = 1 ;
- int Score[4] = {} ;
- int Map[WALL][WALL] = {} ;
- // 开始 菜单 死亡
- bool iBegin = true ,iMenu = false ,iDead = true ;
- // 加速 循环地图 竞技模式
- bool iFast = true ,iFor = true ,iRace = false ;
- // 迷宫模式 额外奖励 帮助
- bool iMaze = false ,iPrize = false ,iHelp = false ;
- // 调试模式
- bool iDebug = false ;
- const char wLevel[5][5] ={"简单","普通","困难","炼狱","调试"} ;
- /* 初始化蛇 */
- Snake::Snake(int color,int speed,bool life)
- {
- Color=color ;
- Speed=speed ;
- Life=life ;
- }
- /* 数据导入 */
- void GradeIn()
- {
- if(fopen("score.log","rb"))
- fscanf(fopen("score.log","rb"),"%d%d%d",Score,Score+1,Score+2) ;
- }
- /* 检测击键 */
- bool Hit(botton bot,int key)
- {
- #ifdef HIT
- RECT rect={} ;
- POINT pos={} ;
- GetCursorPos(&pos) ;
- GetWindowRect(GetConsoleWindow(),&rect) ;
- int x=(pos.x-rect.left-6)/8 , y=(pos.y-rect.top-32)/16 ;
- return(x>=bot.x&&x<bot.x+bot.length&&y>=bot.y&&y<=bot.y+2)&&Key(key) ;
- #endif
- return false;
- }
- /* 选择菜单 */
- void Menu()
- {
- int place=0 ;
- void (*function[])()={Prepare,Levels,GradeInput,Setting} ;
- botton bot[5]={{16,7,12},{13,10,18},{13,13,10},{23,13,8},{16,16,12}} ;
- Print:
- system("cls") ;
- printf("\n\n\n%s贪%s吃%s蛇\n\n\n\n",Pempty(17),Pempty(2),Pempty(2),Pcolor(15,3)) ;
- printf("\n%s开始游戏\n\n",Pempty(18)) ;
- printf("\n%s游戏难度 :%s\n\n",Pempty(15),wLevel[Level]) ;
- printf("\n%s排行榜%s设置\n\n",Pempty(15),Pempty(4)) ;
- printf("\n%s退出游戏",Pempty(18)) ;
- Pedge(bot[place]);
- Scan:
- Sleep(125) ;
- if (Key(' ')||Key(VK_RETURN)){
- if(place<4)
- function[place]() ;
- if(place==0||place==4){
- iBegin=(place==0);
- return ;
- }
- place=0 ;
- }
- elif ((Key('S')||Key(VK_DOWN)) && place!=4)
- place+=(place==2)?2:1 ;
- elif ((Key('W')||Key(VK_UP)) && place!=0)
- place-=(place==3)?2:1 ;
- elif ((Key('D')||Key(VK_RIGHT)) && place==2)
- place++ ;
- elif ((Key('A')||Key(VK_LEFT)) && place==3)
- place-- ;
- else
- goto Scan ;
- goto Print ;
- }
- /* 难度选择 */
- void Levels()
- {
- Print:
- system("cls") ;
- printf("\n\n\n%s困 难 度 选 择\n\n\n\n",Pempty(15)) ;
- for(int i=0 ;i<4+iDebug ;i++)
- printf("%s%s\n\n\n",Pempty(20),wLevel[i]) ;
- Pedge({18,(Level+2)*3,8});
- Scan:
- Sleep(125) ;
- if ((Key('S')||Key(VK_DOWN)) && Level != 3+iDebug)
- Level++ ;
- elif((Key('W')||Key(VK_UP)) && Level != 0)
- Level-- ;
- elif(Key(' ')||Key(VK_RETURN))
- return ;
- else
- goto Scan ;
- goto Print ;
- }
- /* 排行数据 */
- void GradeInput()
- {
- system("cls") ;
- printf("\n\n\n%s排 行 榜\n\n",Pempty(18)) ;
- for(int i=0 ;i<3 ;i++)
- printf("%s第%d名: %d\n\n",Pempty(17) ,i+1,Score[i]) ;
- printf("\n\n\n%s知道了\n\n",Pempty(19)) ;
- Pedge({17,13,10});
- for(Sleep(125) ; !(Key(' ') ||Key(VK_RETURN)) ;Sleep(125)) ;
- }
- /* 游戏设置 */
- void Setting()
- {
- bool iClear = false ;
- bool *str[10] = {&iDead ,&iFor ,&iHelp ,&iMaze ,&iPrize ,&iRace ,&Auto.Life ,&Scnd.Life ,&iClear ,&iDebug } ;
- char name[10][9] = {"死亡判定" ,"循环地图" ,"智能辅助" ,"迷宫地图" ,"额外奖励" ,"淘汰模式" ,"人机模式" ,"双人模式" ,"分数清理" ,"调试模式" } ;
- int place = 0 ;
- Print:
- system("cls") ;
- printf("\n\n%s设%s置\n\n\n",Pempty(18),Pempty(4)) ;
- for(int i=0;i<10;i+=2){
- printf("%s%s%s%s\n\n\n",Pempty(4),name[i],Pempty(11),name[i+1],Pposition(0,i/2*3+5),Pcolor(15,3));
- printf("%s",(*str[i] )?" 开":"关",Pposition(16,i/2*3+5),Pcolor((*str[i] )?3:15,(*str[i])?15:3));
- printf("%s",(*str[i+1])?" 开":"关",Pposition(36,i/2*3+5),Pcolor((*str[i+1])?3:15,(*str[i+1])?15:3));
- }
- printf("\n\n\n%s退%s出",Pempty(16),Pempty(5),Pcolor(15,3));
- if(place<10)
- Pedge({(place%2)?34:14,place/2*3+4,8}) ;
- else
- Pedge({14,19,13}) ;
- Scan:
- if ((iFast||!iDead)&&!iDebug)
- iDead=true;
- Sleep(125) ;
- if ( (Key('S')||Key(VK_DOWN)) && place < 10)
- place+=2 ;
- elif( (Key('W')||Key(VK_UP)) && place > 1)
- place-=2 ;
- elif( (Key('A')||Key(VK_LEFT)) && place%2 == 1)
- place-- ;
- elif( (Key('D')||Key(VK_RIGHT)) && place%2 == 0)
- place++ ;
- elif(Key(' ')||Key(VK_RETURN)){
- if(place>=10)
- return ;
- *str[place]=!(*str[place]);
- }
- else
- goto Scan ;
- if(iClear)
- Score[0] = Score[1] = Score[2] = Score[3]= 0 ;
- goto Print ;
- }
- /* 主函数 */
- int main()
- {
- system("color 3f&mode con cols=44 lines=24&title 贪吃蛇") ;
- GradeIn() ;
- Menu:
- Menu() ;
- while( iBegin ){
- Frst.Direction('W','A','S','D' );
- Scnd.Direction(VK_UP,VK_LEFT,VK_DOWN,VK_RIGHT) ;
- if( !Scnd.Life )
- Frst.Direction(VK_UP,VK_LEFT,VK_DOWN,VK_RIGHT) ;
- Frst.Go(iHelp ,false ,'R' ) ;
- Scnd.Go(iHelp ,false ,VK_NUMPAD0 ) ;
- Auto.Go(true ,true ,-1 ) ;
- if(iDead&&(Frst.Active&& Map[Frst.Head.y][Frst.Head.x] != HEAD)||
- (Auto.Active&& Map[Auto.Head.y][Auto.Head.x] != HEAD)||
- (Scnd.Active&& Map[Scnd.Head.y][Scnd.Head.x] != HEAD))
- Dead() ;
- if(iMenu){
- GradeOutput(Frst.Grade+Scnd.Grade) ;
- goto Menu ;
- }
- Sleep((iDebug&&Level==4)?0 : ( Level < 3)?(150 -(Level * 50)) :(rand() % 80)) ;
- }
- return 0 ;
- }
- /* 运动处理 */
- void Snake::Go(bool iiSmart,bool iiSmarter,int faster)
- {
- for(int i=0;((iFast&&Key(faster))||i<Speed)&&Active;i++){
- iMenu = Key(VK_ESCAPE) ;
- if(iiSmarter)
- Smarter() ;
- if(iiSmart)
- for(int i=0 ;i<16 ;i++)
- if((Flag==LEFT && Map[Head.y] [Head.x-1])||
- (Flag==DOWN && Map[Head.y+1][Head.x] )||
- (Flag==RIGHT && Map[Head.y] [Head.x+1])||
- (Flag==UP && Map[Head.y-1][Head.x] ))
- Flag=rand()%4+1 ;
- Move() ;
- }
- }
- /* 智能方向 */
- void Snake::Smarter()
- {
- Map[Food.y][Food.x]=-2 ;
- for(int nem=0 ;nem<50&&Map[Head.y-1][Head.x]!=-2&&Map[Head.y+1][Head.x]!=-2&&Map[Head.y][Head.x-1]!=-2&&Map[Head.y][Head.x+1]!=-2 ;nem++){
- for(int i=1 ;i<WALL-1 ;i++){
- for(int j=1 ;j<WALL-1 ;j++){
- if(Map[i][j]==-2){
- if(Map[i-1][j]==0)
- Map[i-1][j]=-1 ;
- if(Map[i+1][j]==0)
- Map[i+1][j]=-1 ;
- if(Map[i][j-1]==0)
- Map[i][j-1]=-1 ;
- if(Map[i][j+1]==0)
- Map[i][j+1]=-1 ;
- }
- }
- }
- if(iFor){
- for(int i=0 ;i<WALL ;i++){
- for(int j=0 ;j<WALL ;j+=(i==0||i==WALL-1)?1:WALL-1){
- if(Map[i][j]==-1){
- Map[(i==0||i==WALL-1)?abs(i-WALL+1):i][(j==0||j==WALL-1)?abs(j-WALL+1):j] = (Map[(i==0||i==WALL-1)?abs(i-WALL+1):i][(j==0||j==WALL-1)?abs(j-WALL+1):j]==0)?-2:Map[(i==0||i==WALL-1)?abs(i-WALL+1):i][(j==0||j==WALL-1)?abs(j-WALL+1):j] ;
- Map[(i==0||i==WALL-1)?abs(i-WALL+2):i][(j==0||j==WALL-1)?abs(j-WALL+2):j] = (Map[(i==0||i==WALL-1)?abs(i-WALL+2):i][(j==0||j==WALL-1)?abs(j-WALL+2):j]==0)?-2:Map[(i==0||i==WALL-1)?abs(i-WALL+2):i][(j==0||j==WALL-1)?abs(j-WALL+2):j] ;
- }
- }
- }
- }
- for(int i=0 ;i<WALL ;i++)
- for(int j=0 ;j<WALL ;j++)
- Map[i][j]=(Map[i][j]==-1)?-2:Map[i][j] ;
- }
- if (Map[Head.y-1][Head.x]==-2)
- Flag=UP ;
- elif(Map[Head.y+1][Head.x]==-2)
- Flag=DOWN ;
- elif(Map[Head.y][Head.x-1]==-2)
- Flag=LEFT ;
- elif(Map[Head.y][Head.x+1]==-2)
- Flag=RIGHT ;
- for(int i=0 ;i<WALL ;i++)
- for(int j=0 ;j<WALL ;j++)
- if(Map[i][j]==-2)
- Map[i][j]=0 ;
- }
- /* 定位打印 */
- bool Pposition(int x ,int y)
- {
- CONSOLE_CURSOR_INFO CurSor ={};
- SetConsoleCursorPosition(Handle, { x,y }) ;
- GetConsoleCursorInfo(Handle,&CurSor) ;
- CurSor.bVisible = false ;
- SetConsoleCursorInfo(Handle,&CurSor) ;
- }
- /* 字体颜色 */
- bool Pcolor( int a ,int b)
- {
- SetConsoleTextAttribute(Handle, a + b * 0x10) ;
- }
- /* 批量打印 */
- char*Psame (int n,char word)
- {
- char * turn=new char [n+1] ;
- memset(turn,word,n);
- turn[n]=0;
- return turn ;
- }
- /* 边框打印 */
- void Pedge(botton the)
- {
- for(int i=0;i<2;i++){
- printf("%s",Psame(the.length,'-'),Pposition(the.x,the.y+i*2),Pcolor(15,3));
- printf("#" ,Pposition(the.x+i*(the.length-1),the.y+1));
- }
- }
- /* 运行准备 */
- void Prepare()
- {
- Frst.Re(RIGHT,{3,10}) ;
- if(Auto.Life)
- Scnd.Re(DOWN,{11,3});
- else
- Scnd.Re(LEFT,{18,10}) ;
- Auto.Re(LEFT,{18,10}) ;
- iMenu = false ;
- system("cls") ;
- for(int i = 0 ; i < WALL ; i++){
- for(int j = 0 ; j < WALL ; j++){
- Map[i][j] = 0;
- if ( i == 0 || i == WALL -1 || j == 0 || j == WALL -1){
- Map[i][j] = (iFor)?0:WALL;
- printf("■",Pcolor( 15,3)) ;
- }
- else
- printf("%s",Pempty(2),Pcolor( 3 , 3)) ;
- }
- }
- Foods() ;
- }
- /* 蛇的重置 */
- void Snake::Re(int flag,POINT pos)
- {
- Sum = -3 ;
- Grade = 0 ;
- Flag = flag ;
- Active = Life ;
- End.x = Head.x = pos.x ;
- End.y = Head.y = pos.y ;
- }
- /* 蛇皮走位 */
- void bearing::walk(int Flag)
- {
- switch(Flag){
- case LEFT:{
- x-=(iFor&&x==1)?-19:1 ;
- }return ;
- case RIGHT:{
- x+=(iFor&&x==20)?-19:1 ;
- }return ;
- case UP:{
- y-=(iFor&&y==1)?-19:1 ;
- }return ;
- case DOWN:{
- y+=(iFor&&y==20)?-19:1 ;
- }return ;
- }
- }
- /* 方向判断 */
- void Snake::Direction(int up,int left,int down,int right)
- {
- Flag = ((Key(left)) && Flag!=RIGHT )? LEFT :Flag ;
- Flag = ((Key(right))&& Flag!=LEFT )? RIGHT :Flag ;
- Flag = ((Key(up)) && Flag!=DOWN )? UP :Flag ;
- Flag = ((Key(down)) && Flag!=UP )? DOWN :Flag ;
- }
- /* 蛇的移动 */
- void Snake::Move()
- {
- printf("●",Pcolor(Color,3) ,Pposition(End.x*2,End.y) ) ;
- Map[Head.y][Head.x] = Flag ;
- Head.walk(Flag) ;
- printf("●",Pcolor( Color ,3),Pposition( Head.x * 2 ,Head.y)) ;
- Map[Head.y][Head.x] += HEAD ;
- if (Head.y==Food.y&& Head.x==Food.x){
- Grade++ ;
- Foods() ;
- }
- if (Sum==Grade){
- int Temp = Map[End.y][End.x] ;
- Map[End.y][End.x] = 0 ;
- printf("%s",Pempty(2),Pcolor( 3 ,3), Pposition( End.x * 2 ,End.y) ) ;
- End.walk(Temp) ;
- }
- else
- Sum++ ;
- }
- /* 死亡判断 */
- void Dead()
- {
- if(iRace&&(Frst.Active||Scnd.Active||Auto.Active)){
- Snake*they[3]={&Frst,&Scnd,&Auto} ;
- for(int i=0;i<3;i++)
- if(Map[they[i]->Head.y][they[i]->Head.x] != HEAD){
- they[i]->Active = false ;
- Map[they[i]->Head.y][they[i]->Head.x] -= HEAD ;
- }
- return ;
- }
- GradeOutput(Frst.Grade+Scnd.Grade) ;
- printf(" Game Over !",Pcolor( 15, 3) ,Pposition( 16 , 9)) ;
- Sleep(1500) ;
- char wDead[3][9] ={"重新开始","返回菜单","退出游戏"} ;
- int place = iBegin = false ;
- Print:
- system("cls") ;
- printf("\n\n\n\n%s游 戏 结 束\n",Pempty(16)) ;
- if (Scnd.Life&&Frst.Grade!=Scnd.Grade)
- printf("%s%s 赢 了",Pempty(16),(Frst.Grade>Scnd.Grade)?"左 边":"右 边");
- printf("\n%s分数 : %d\n\n\n\n" ,Pempty(17),Frst.Grade+Scnd.Grade) ;
- for( int i = 0 ; i < 3 ; i++)
- printf("%s%s\n\n\n",Pempty(18),wDead[i]) ;
- Pedge({16,place*3+9,12}) ;
- Scan:
- Sleep(125) ;
- if ((Key('W')||Key(VK_UP)) && place != 0)
- place-- ;
- elif((Key('S')||Key(VK_DOWN)) && place != 2)
- place++ ;
- elif(Key(' ')||Key(VK_RETURN)){
- iBegin = (place==0||place==1) ;
- if (place==0)
- Prepare() ;
- elif(place==1)
- iBegin = iMenu = true ;
- return ;
- }
- else
- goto Scan ;
- goto Print ;
- }
- /* 随机果实 */
- void Foods()
- {
- do{
- Food.x = rand() %(WALL - 2) +1 ,Food.y = rand() %(WALL - 2) +1 ;
- }while( Map[Food.y][Food.x]!=0 ) ;
- printf("★",Pcolor(4,3),Pposition( Food.x * 2 ,Food.y)) ;
- }
- /* 分数保存 */
- void GradeOutput(int grade)
- {
- for(int i=2 ;i>=0 ;i--)
- if(Score[i]<grade){
- Score[i+1] = Score[i] ;
- Score[i] = grade ;
- }
- fprintf(fopen("score.log","w+"),"%d\n%d\n%d",Score[0],Score[1],Score[2]) ;
- }
复制代码
作者: 老刘1号 时间: 2020-8-31 12:40
哇这么热闹,
老早以前用批处理写的咬文嚼字蛇:http://www.bathome.net/thread-43231-1-1.html
由于每个蛇身都不一样,不能直接处理头和尾中间不管,所以实现是全挪一遍233。
happy用纯批写过一个很精简的贪吃蛇,好像只有60行左右,不过链接找不到了。
作者: netdzb 时间: 2020-9-18 11:26
回复 4# wujunkai
这个是DOS程序吗?
作者: wujunkai 时间: 2020-9-20 11:39
回复 6# netdzb
不是,是控制台程序
作者: netdzb 时间: 2020-9-20 12:08
回复 7# wujunkai
在什么平台编译的?mingw可以吗?
作者: wujunkai 时间: 2020-9-20 21:28
回复 8# netdzb
不知道,我用的是dev C++
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |