#include<stdio.h> #include<graphics.h> #include<time.h> #include<conio.h> #include<easyx.h> #define MAXLEN 100//定以蛇的长度 #define MAXFOD 6 #define eror 1 #define ok 0 #pragma comment ( lib, "Winmm.lib" ) enum direction { left,up,right,down }; int pos[MAXLEN][2]; int food[MAXFOD][2]; int LENFOD=0; int LENGTH=0; int MARK=0; int HIGHESTM=0; direction DIT; int snake(); int drawLine(); int init(); int controlfood(); int saveMark(); boolean isdead(); boolean iswin(); int countmark(); void main() { int i=0,count=1; srand((unsigned)time(0)); initgraph(600,660);//设置窗口的总大小 setcolor(YELLOW);//设置字体颜色 setfont( 30, 0,"隶书");//设置字体大小和类型 mciSendString( "open D:\\KuGou\\Adele - Someone Like You.mp3 Name alias skycity", 0, 0, 0 ); mciSendString( "play skycity repeat ", 0, 0, 0 ); char a;//按回车键开始 char numbe[10]; sprintf(numbe, "%d", HIGHESTM); outtextxy(100,300,"PRESS ENTER TO START \n"); a=getchar(); if(a!='\n') { exit(0); } start: cleardevice(); init(); /*or(i=0;i<LENGTH;i++) printf("\n%d %d",pos[i][0],pos[i][1]); printf("\n"); pos[0][1] -= 1; for(i=LENGTH-1;i>0;i--) { pos[i][0]=pos[i-1][0]; pos[i][1]=pos[i-1][1]; } closegraph(); for(i=0;i<LENGTH;i++) printf("\n%d %d",pos[i][0],pos[i][1]); Sleep(50000);*/ BeginBatchDraw(); drawLine(); snake(); FlushBatchDraw(); while(true) { if(kbhit()) { switch(getch()) { case 27: saveMark(); closegraph(); exit(ok); break; case 72: case 'w': case 'W': if(DIT!=down) { DIT=up; for(i=LENGTH-1;i>0;i--) { pos[i][0]=pos[i-1][0]; pos[i][1]=pos[i-1][1]; } pos[0][1] -= 1; } break; case 80: case 's': case 'S': if(DIT!=up) { DIT=down; for(i=LENGTH-1;i>0;i--) { pos[i][0]=pos[i-1][0]; pos[i][1]=pos[i-1][1]; } pos[0][1] += 1; } break; case 75: case 'a': case 'A': if(DIT!=right) { DIT=left; for(i=LENGTH-1;i>0;i--) { pos[i][0]=pos[i-1][0]; pos[i][1]=pos[i-1][1]; } pos[0][0] -= 1; } break; case 77: case 'd': case 'D': if(DIT!=left) { DIT=right; for(i=LENGTH-1;i>0;i--) { pos[i][0]=pos[i-1][0]; pos[i][1]=pos[i-1][1]; } pos[0][0] += 1; } break; } cleardevice(); controlfood(); drawLine(); countmark(); snake(); count=1; FlushBatchDraw(); } if(LENGTH<10) { if(count==20) { count=0; } } else if(LENGTH<15&&LENGTH>9) { if(count==15) count=0; } else if(LENGTH<20&&LENGTH>14) { if(count==10) count=0; } else { if(count==5) count=0; } if(count==0) { for(i=LENGTH-1;i>0;i--) { pos[i][0]=pos[i-1][0]; pos[i][1]=pos[i-1][1]; } switch(DIT) { case up: pos[0][1] -= 1; break; case down: pos[0][1] += 1; break; case left: pos[0][0] -= 1; break; case right: pos[0][0] += 1; break; } cleardevice(); controlfood(); drawLine(); countmark(); snake(); FlushBatchDraw(); } if(isdead()) { mciSendString( "play Lose ", 0, 0, 0 ); outtextxy(240,275,"失败!"); FlushBatchDraw(); while(true) { if(kbhit()) { switch(getch()) { case 13: mciSendString( "close Lose ", 0, 0, 0 ); mciSendString( "close win ", 0, 0, 0 ); goto start; case 27: mciSendString( "close skycity ", 0, 0, 0 ); closegraph(); exit(ok); } } } } if(iswin()) { mciSendString( "play win ", 0, 0, 0 ); outtextxy(240,275,"WINNING!"); FlushBatchDraw(); while(true) { if(kbhit()) { switch(getch()) { case 13: mciSendString( "close Lose ", 0, 0, 0 ); mciSendString( "close win ", 0, 0, 0 ); goto start; case 27: mciSendString( "close skycity ", 0, 0, 0 ); closegraph(); exit(ok); } } } } count++; Sleep(20); } } int snake() { int i=0,j=0; setfillcolor(YELLOW);//设置蛇的颜色 for(i=0;i<LENGTH;i++) solidcircle(pos[i][0]*20+10,pos[i][1]*20+10,10); setfillcolor(RED);//设置眼睛红色 i=0; if(DIT==left)//判断方向 { solidcircle(pos[i][0]*20+5,pos[i][1]*20+5,3); solidcircle(pos[i][0]*20+5,pos[i][1]*20+15,3); } else if(DIT==right) { solidcircle(pos[i][0]*20+15,pos[i][1]*20+5,3); solidcircle(pos[i][0]*20+15,pos[i][1]*20+15,3); } else if(DIT==down) { solidcircle(pos[i][0]*20+5,pos[i][1]*20+15,3); solidcircle(pos[i][0]*20+15,pos[i][1]*20+15,3); } else { solidcircle(pos[i][0]*20+5,pos[i][1]*20+5,3); solidcircle(pos[i][0]*20+15,pos[i][1]*20+5,3); } setfillcolor(GREEN); //setfont( 18, 0,"楷体"); for(i=0;i<LENFOD;i++) { solidcircle(food[i][0]*20+10,food[i][1]*20+10,7); /*j=rand()%2; if(j==0) { outtextxy(food[i][0]*20+1,food[i][1]*20+1,""); } else if(j==1) { outtextxy(food[i][0]*20+1,food[i][1]*20+1,""); } else { outtextxy(food[i][0]*20+1,food[i][1]*20+1,""); }*/ } //setfont( 30, 0,"楷体"); //solidcircle(food[i][0]*20+10,food[i][1]*20+10,7); setfillcolor(GREEN); return ok; } int drawLine() { int i; for(i=20;i<600;i+=20) { line(0,i,600,i); line(i,0,i,600); } line(0,i,600,i); line(0,i+5,600,i+5); return ok; } int init() { int i,j,k,l; FILE * fp; mciSendString( "open D:\\KuGou\\Adele - Someone Like You.mp3 alias Lose", 0, 0, 0 ); mciSendString( "open ./source/win.mp3 alias win", 0, 0, 0 ); LENGTH=0; LENFOD=0; MARK=0; if((fp=fopen("highestscore.txt","r"))==NULL)//读文件中保存的最高分 { printf("File open eror!\n"); exit(eror); } else { fscanf(fp,"%d",&HIGHESTM); if(fclose(fp)) { printf("file close eror\n"); exit(eror); } } setlinecolor(GREEN); setfillcolor(YELLOW); i=rand()%10+10; j=rand()%10+10; k=rand()%4; pos[0][0] = i; pos[0][1] = j; if(k==0) { for(i=1;i<5;i++) { pos[i][1]=pos[i-1][1]+1; pos[i][0]=pos[i-1][0]; DIT=up; } } else if(k==1) { for(i=1;i<5;i++) { pos[i][0]=pos[i-1][0]-1; pos[i][1]=pos[i-1][1]; DIT=right; } } else if(k==2) { for(i=1;i<5;i++) { pos[i][1]=pos[i-1][1]-1; pos[i][0]=pos[i-1][0]; DIT=down; } } else { for(i=1;i<5;i++) { pos[i][0]=pos[i-1][0]+1; pos[i][1]=pos[i-1][1]; DIT=left; } } LENGTH=5; k=rand()%4+1; for(l=0;l<k;l++) { i=rand()%30; j=rand()%30; food[LENFOD][0]=i; food[LENFOD++][1]=j; for(int m=0;m<LENGTH;m++) if(i==pos[m][0]&&j==pos[m][1]) { l--; LENFOD--; break; } } return ok; } int controlfood()//控制食物函数 { int i=0,j,k,l; for(i=0;i<LENFOD;i++) { if(pos[0][0]==food[i][0]&&pos[0][1]==food[i][1]) { if(LENGTH<10)//根据长度计算分数 { MARK+=5; } else if(LENGTH<15) { MARK+=10; } else if(LENGTH<20) { MARK+=15; } else if(LENGTH<25) { MARK+=20; } else { MARK+=25; } break; } } if(i<LENFOD) { for(j=i;j<LENFOD-1;j++) { food[j][0]=food[j+1][0]; food[j][1]=food[j+1][1]; } LENFOD--; LENGTH++; pos[LENGTH-1][0]=pos[LENGTH-2][0]*2-pos[LENGTH-3][0]; pos[LENGTH-1][1]=pos[LENGTH-2][1]*2-pos[LENGTH-3][1]; } if(LENFOD<2) { k=rand()%4+1; for(l=0;l<k;l++) { i=rand()%30; j=rand()%30; food[LENFOD][0]=i; food[LENFOD++][1]=j; for(int m=0;m<LENGTH;m++) if(i==pos[m][0]&&j==pos[m][1]) { l--; LENFOD--; break; } } } return ok; } boolean isdead()//判断是否死亡 { int i; if(pos[0][0]<0||pos[0][0]>30||pos[0][1]<0||pos[0][1]>30)//是否超出边界 { saveMark(); return true; } for(i=3;i<LENGTH;i++) if(pos[0][0]==pos[i][0]&&pos[0][1]==pos[i][1])//头是否碰上自身 return true; return false; } boolean iswin()//是否获胜 { if(LENGTH==MAXLEN&&!isdead()) { saveMark(); return true; } else return false; } int countmark()//计算总分 { char num[10]; sprintf(num, "%d", HIGHESTM); outtextxy(60,610,"最高记录:");//输出最高分,横坐标60,纵坐标610 outtextxy(200,610,num); outtextxy(350,610,"你的成绩:");//输出你的当前成绩 sprintf(num, "%d", MARK); outtextxy(510,610,num); return ok; } int saveMark()//保存成绩 { FILE * fp; if(MARK>HIGHESTM)//如果当前成绩大于最高纪录 { if((fp=fopen("highestscore.txt","w"))==NULL)//写入成绩 { printf("File open eror!\n"); exit(eror); } else { fprintf(fp,"%d",MARK); if(fclose(fp)) { printf("file close eror\n"); exit(eror); } } } return ok; }