- # include <stdio.h>
- # include <stdlib.h>
- # include <time.h>
- # include <conio.h>
- # include <windows.h>
- # define M 20
- # define S 500
- # define E 16
- FILE* fp = NULL;
- char name[7];
- int rank[E]={0,0,0,0,0};
- char rn[E][10];
- double tim = 0;
- const int m = 20,n = 20;//map size
- int x[M*M],y[M*M];
- int skx,sky,fdx,fdy;//snake
- int len,sp,sc,col;
- int ch,i,j,f,ch2,f2;//other
- void gotohc()//hide _
- {
- CONSOLE_CURSOR_INFO cursor_info = {1, 0};
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
- }
- void gotosc()//show _
- {
- CONSOLE_CURSOR_INFO cursor_info = {1, 1};
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
- }
- void gotoxy(int y, int x)//goto xy
- {
- COORD pos = {x,y};
- HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(hOut, pos);
- }
- void food()//setfood
- {
- do{
- f = 0;
- fdx = rand()%(m-2)+1,fdy = rand()%(n-2)+1;
- for(i=1;i<len;i++)
- if(fdx==x[i] && fdy==y[i])
- f = 1;
- }while(f);//if xy same
- gotoxy(fdx,fdy*2),puts("★");
- }
- int lost()//if lost
- {
- for(i=1;i<len-1;i++)//itself
- if(skx==x[i] && sky==y[i])
- return 1;
- if(sky<=0 || skx<=0 || skx>(m-2) || sky>(n-2))//wall
- return 2;
- return 0;
- }
- void show()//print
- {
- gotoxy(x[0],y[0]*2),puts(" ");
- for(i=1;i<len;i++)//show
- gotoxy(x[i],y[i]*2),puts("●");
- gotoxy(skx,sky*2),puts("■");
- gotoxy(4,54),printf("■坐标:%2d,%2d",skx,sky);
- Sleep(sp);//wait
- }
- void init()
- {
- SetConsoleTitle("贪吃蛇V1.3 - by 流星不死");
- system("mode con cols=80 lines=22");
- gotohc();
- srand((unsigned)time(NULL));
- skx = 5,sky = 8,len = 4,sp = S,sc = 0,ch = 'd',f2 = 1;
- for(i=5;i<9;i++)
- x[i-5] = 5,y[i-5] = i;
- for(i=0;i<M;i++,Sleep(10))
- {
- gotoxy(0,i*2),printf("□");
- gotoxy(i,0),printf("□");
- }
- for(i=0;i<M;i++,Sleep(10))
- {
- gotoxy(M-1,i*2),printf("□");
- gotoxy(i,(M-1)*2),printf("□");
- }
- for(i=M;i<M*2;i++,Sleep(10))
- {
- gotoxy(0,i*2),printf("□");
- gotoxy(19,i*2),printf("□");
- }
- for(i=0;i<M;i++,Sleep(10))
- {
- gotoxy(i,52),printf("□");
- gotoxy(i,66),printf("□");
- gotoxy(i,78),printf("□");
- }
- for(i=0;i<M/2-3;i++,Sleep(10))
- {
- gotoxy(7,i*2+54),printf("□");
- gotoxy(10,i*2+54),printf("□");
- gotoxy(13,i*2+54),printf("□");
- gotoxy(16,i*2+54),printf("□");
- }
- for(i=0;i<M/2-4;i++,Sleep(10))
- {
- gotoxy(9,i*2+68),printf("□");
- gotoxy(16,i*2+68),printf("□");
- }
- if(NULL == (fp=fopen("rank.txt","r")))
- {
- fp = fopen("rank.txt","w");
- for(i=0;i<E;i++)
- {
- fprintf(fp,"%s %d","XXX",0);
- fprintf(fp,"\\n");
- }
- }
- fclose(fp);
- fp=fopen("rank.txt","r");
- gotoxy(1,43),printf("排行榜");Sleep(10);
- gotoxy(1,58),printf("属性");Sleep(10);
- gotoxy(1,69),printf("键位说明");Sleep(10);
- gotoxy(2,40),printf("名字\\t得分");Sleep(10);
- gotoxy(2,54),printf("●长度:%5d",len-1);Sleep(10);
- gotoxy(2,68),printf("W/w键:上移");Sleep(10);
- gotoxy(3,54),printf("◆速度:%5d",S-sp+20);Sleep(10);
- gotoxy(3,68),printf("S/s键:下移");Sleep(10);
- gotoxy(4,54),printf("■坐标:%2d,%2d",skx,sky);Sleep(10);food();
- gotoxy(4,68),printf("A/a键:左移");Sleep(10);
- gotoxy(5,54),printf("★坐标:%2d,%2d",fdx,fdy);Sleep(10);
- gotoxy(5,68),printf("D/d键:右移");Sleep(10);
- gotoxy(6,54),printf("▲得分:%5d",sc);Sleep(10);
- gotoxy(6,68),printf("Q/q键:退出");Sleep(10);
- gotoxy(7,68),printf("R/r键:重来");Sleep(10);
- gotoxy(8,56),printf("生命状态");Sleep(10);
- gotoxy(8,68),printf("其他键暂停");Sleep(10);
- gotoxy(9,58),printf("正常");Sleep(10);
- gotoxy(10,69),printf("游戏规则");Sleep(10);
- gotoxy(11,68),printf("不停的吃★");Sleep(10);
- gotoxy(12,68),printf("来增加●的");Sleep(10);
- gotoxy(13,68),printf("长度,撞到");Sleep(10);
- gotoxy(14,57),printf("版本号");Sleep(10);
- gotoxy(14,68),printf("墙壁或者撞");Sleep(10);
- gotoxy(15,56),printf(" 1.3");Sleep(10);
- gotoxy(15,68),printf("到自己死亡");Sleep(10);
- gotoxy(17,58),printf("作者");Sleep(10);
- gotoxy(17,69),printf("游戏时间");Sleep(10);
- gotoxy(18,56),printf("流星不死");Sleep(10);
- gotoxy(18,71),printf("%.2lf",(double)clock()/CLOCKS_PER_SEC-tim);
- for(i=0;i<E;i++)
- {
- fscanf(fp,"%s %d",rn[i],&rank[i]);
- gotoxy(3+i,40),printf("%-6s %5d",rn[i],rank[i]);
- Sleep(10);
- }
- show();
- fclose(fp);
- }
- int move()//move snake
- {
- if(kbhit()) ch = getch();//if getch
- if(ch=='W' || ch=='w') skx--;//change direction
- else if(ch=='S' || ch=='s') skx++;
- else if(ch=='A' || ch=='a') sky--;
- else if(ch=='D' || ch=='d') sky++;
- else if(ch=='R' || ch=='r') return 1;
- else if(ch=='Q' || ch=='q') exit(0);
- else
- {
- gotoxy(11,57),printf("暂停中");
- gotoxy(12,54),printf("按方向键继续");
- goto k2;
- }
- gotoxy(11,57),printf(" ");
- gotoxy(12,54),printf(" ");
- for(i=1;i<len;i++)//change xy
- x[i-1] = x[i],y[i-1] = y[i];
- x[len-1] = skx,y[len-1] = sky;
- if(skx==fdx && sky==fdy)//if eat
- {
- len++;
- for(i=len;i>=1;i--)
- x[i] = x[i-1],y[i] = y[i-1];
- food();
- if((sp=(sp-20)) < 50)
- sp+=20;
- sc+=((S-sp)/10);
- gotoxy(2,54),printf("●长度:%5d",len-1);
- gotoxy(3,54),printf("◆速度:%5d",S-sp+20);
- gotoxy(5,54),printf("★坐标:%2d,%2d",fdx,fdy);
- gotoxy(6,54),printf("▲得分:%5d",sc);
- }
- k2:
- gotoxy(18,71),printf("%.2lf",(double)clock()/CLOCKS_PER_SEC-tim);
- show();
- return 0;
- }
- int main()
- {
- k1:
- init();
- while(1)//main while
- {
- if(move())
- {
- tim = (double)clock()/CLOCKS_PER_SEC;
- system("CLS");
- goto k1;
- }
- j = lost();
- if(j)
- {
- gotoxy(9,58),printf("异常");
- gotoxy(11,56),printf("死亡原因");
- if(j == 1)
- gotoxy(12,56),printf("撞到自己");
- else if(j == 2)
- gotoxy(12,56),printf("撞到墙壁");
- break;
- }
- }
- gotosc();
- fflush(stdin);
- gotoxy(M,0),printf("你输了,请输入你的名字<6个字符以内,按Enter键结束>:");
- for(i=0;i<6;i++)
- {
- name[i] = getchar();
- if(name[i] == ' ')
- name[i] = '_';
- if(name[i]=='\\n' && i!=0)
- break;
- else if(name[i] == '\\n' && i==0)
- {
- strcpy(name,"empty"),i = 5;
- break;
- }
- }
- name[i]='\\0';
- gotohc();
- for(i=0;i<E;i++)
- if(sc>=rank[i])
- {
- for(j=3;j>=i;j--)
- rank[j+1] = rank[j],strcpy(rn[j+1],rn[j]);
- rank[i] = sc,strcpy(rn[i],name);
- break;
- }
- fp = fopen("rank.txt","w");
- for(i=0;i<E;i++)
- fprintf(fp,"%s %d",rn[i],rank[i]),fprintf(fp,"\\n");
- fclose(fp);
- gotoxy(M+1,0),printf("游戏结束,按R/r键重新开始游戏,按Q/q键退出游戏:");
- while(1)
- if(kbhit())
- {
- ch2 = getch();
- tim = (double)clock()/CLOCKS_PER_SEC;
- if(ch2=='r' || ch2=='R')
- goto k1;
- else if(ch2=='Q' || ch2=='q')
- break;
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1910201513851.html
来源: http://www.codesnippet.cn/detail/1910201513851.html