//snake.h //主头文件,定义了所需的数据结构 #define MAX_HORIZ 39 //游戏界面水平宽度 #define MAX_VERTIC 19 //游戏界面垂直宽度 //贪吃蛇行动方向 enum destination { WEST,EAST,NORTH,SOUTH }; //贪吃蛇结构体 struct snake{ int x; int y; enum destination desti; struct snake *next; }; //食物 struct food { int x; int y; }; /*********************************************实现各个函数*****************************************/ #include"snake.h" #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<time.h> #include<windows.h> /*函数crawl()使贪吃蛇向前爬行一步 *参数s是当前玩家操作的贪吃蛇 *参数d指明蛇头移动方向 *参数f是当前食物的指针 */ int crawl(struct snake* s,enum destination d,struct food* f) { struct snake* first,*next,*last; enum destination d1=d,d2; int flag=0; s->desti=d; first=s; next=s; while(next!=NULL) { switch(next->desti) { case WEST:next->x-=1; if(-1==next->x) next->x+=(MAX_HORIZ+1);break; case EAST:next->x+=1; if(MAX_HORIZ<next->x) next->x-=(MAX_HORIZ+1);break; case NORTH:next->y-=1; if(-1==next->y) next->y+=(MAX_VERTIC+1);break; case SOUTH:next->y+=1; if(MAX_VERTIC<next->y) next->y-=(MAX_VERTIC+1);break; } next=next->next; } next=first; d1=d2=next->desti; while(next!=NULL) { d1=d2; next=next->next; if(next!=NULL) { d2=next->desti; next->desti=d1; } else break; } next=first->next; while(next!=NULL) { if(first->x==next->x&&first->y==next->y) { flag++; break; } next=next->next; } if(flag) return -1; if(first->x==f->x&&first->y==f->y) return 1; else return 0; } struct snake* s_create() { int i=4; struct snake *s=(struct snake*)malloc(sizeof(struct snake)); struct snake *next,*last; last=s; s->x=4; s->y=3; s->desti=EAST; while(i) { next=(struct snake*)malloc(sizeof(struct snake)); next->x=last->x-1; next->y=last->y; next->desti=last->desti; last->next=next; last=next; i--; } last->next=NULL; return s; } void show(struct snake* s,struct food* f) { int i,j; int flag=0; struct snake *first=s; system("cls"); for(i=0;i<=MAX_VERTIC;i++) { for(j=0;j<=MAX_HORIZ;j++) { int flag2=0; if(j==f->x&&i==f->y) { printf("*"); continue; } else do { if(j==s->x&&i==s->y) if(s==first) { printf("@"); flag2++; break; } else { printf("#"); flag2++; break; } s=s->next; }while(s!=NULL); s=first; if(!flag2) printf(" "); } } } struct food* f_create(struct snake* s) { struct food *f=(struct food*)malloc(sizeof(struct food)); int flag=1; while(flag) { flag=0; srand(time(0)); f->x=rand()%(MAX_HORIZ+1); f->y=rand()%(MAX_VERTIC); do { if(f->x==s->x&&f->y==s->y) { flag=1; break; } s=s->next; }while(s!=NULL); if(!flag) return f; } } int grow(struct snake *s) { while(s->next!=NULL) s=s->next; s->next=(struct snake*)malloc(sizeof(struct snake*)); switch(s->desti) { case EAST:s->next->x=s->x-1;s->next->y=s->y;s->next->desti=s->desti; if(-1==s->next->x) s->next->x+=(MAX_HORIZ+1);break; case WEST:s->next->x=s->x+1;s->next->y=s->y;s->next->desti=s->desti; if(MAX_HORIZ<s->next->x) s->next->x-=(MAX_HORIZ);break; case SOUTH:s->next->x=s->x;s->next->y=s->y-1;s->next->desti=s->desti; if(-1==s->next->y) s->next->y+=(MAX_VERTIC+1);break; case NORTH:s->next->x=s->x;s->next->y=s->y+1;s->next->desti=s->desti; if(MAX_VERTIC<s->next->y) s->next->y-=(MAX_VERTIC+1);break; } s->next->next=NULL; return 1; } int die() { int i; system("cls"); while(kbhit()) getch(); for(i=10;i>=0;i--) { printf("\n\n\n\n\n\n\n\n"); printf(" GAME OVER!\n"); printf(" %2d秒后将自动推出程序\n",i); printf(" 按任意键返回菜单"); Sleep(1000); system("cls"); if(kbhit()) { system("cls"); return 1; } } exit(0); } void gameRun() { enum destination d=EAST; struct snake *s; struct food *f; f=(struct food*)malloc(sizeof(struct food)); s=s_create(); while(1) { int g_flag=0; int d_flag=0; char c; f=f_create(s); while(1) { if(kbhit()) { c=getch(); //printf("%c",c); switch(c) { case 'w': case 'W':if(s->desti==EAST||s->desti==WEST) d=NORTH;break; case 'a': case 'A':if(s->desti==SOUTH||s->desti==NORTH) d=WEST;break; case 's': case 'S':if(s->desti!=NORTH) d=SOUTH;break; case 'd': case 'D':if(s->desti!=WEST) d=EAST;break; } //printf("%d",d); } switch(crawl(s,d,f)) { case 0:show(s,f);break; case 1:g_flag=grow(s);break; default:d_flag=die();break; } if(g_flag) break; if(d_flag) break; } if(d_flag) break; } } void direction() { system("cls"); printf("\n\n"); printf("1.游戏控制:\n"); printf(" W:向上\n"); printf(" A:向左 S:向下 D:向右\n\n"); printf("2.开始游戏之前请将输入法切换到英文.\n\n"); printf("3.本游戏作者能力有限,难免出错,如果您有什么建议请发送至邮箱:935292657@qq.com\n\n"); printf("4.本游戏版权及最终解释权归作者所有,请勿用于任何商业用途.\n\n\n\n"); printf(" 按任意键返回菜单..."); getch(); } char welcome() { char f; while(kbhit()) getch(); while(1) { system("cls"); printf("\n\n\n\n\n\n"); printf(" 1.开始游戏\n"); printf(" 2.游戏说明\n"); printf(" 3.退出游戏\n"); f=getch(); if(f=='1'||f=='2'||f=='3') return f; else { system("cls"); printf("对不起!无对应选项,请重新输入!\n按任意键继续..."); getch(); } } } /**********************************************************************实现游戏*********************************************************************/ #include<stdio.h> #include"snake.h" #include<stdlib.h> #include<conio.h> #include<windows.h> int main() { system("mode con:cols=40 lines=21"); SetConsoleTitle("贪吃蛇1.0 Beta"); while(1) { switch(welcome()) { case '1':gameRun();break; case '2':direction();break; case '3':exit(0); } } }
模拟贪吃蛇游戏(纯C语言实现)
最新推荐文章于 2024-07-15 15:35:24 发布