在VC6.0平台上,通过C语言和EasyX图形库编写的贪吃蛇小游戏,为了使用图形库函数,建立的文件是.cpp文件,使用的是C语言编写,游戏具有背景音乐,代码长度500行左右,代码和算法,都是我自己源生的,分享给有兴趣的朋友看看。
- #include<stdio.h>
- #include<graphics.h>
- #include<time.h>
- #include<conio.h>
- #include<easyx.h>
- #define MAXLEN 30
- #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,900);
- setcolor(YELLOW);
- setfont( 30, 0,"楷体");
- mciSendString( "open ./source/skycity.mp3 alias skycity", 0, 0, 0 );
- mciSendString( "play skycity repeat ", 0, 0, 0 );
- start:
- cleardevice();
- init();
- /*for(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,"LOSSING!");
- 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 ./source/Lose.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,"Highest:");
- outtextxy(200,610,num);
- outtextxy(350,610,"YouScore:");
- sprintf(num, "%d", MARK);
- outtextxy(490,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;
- }
复制代码
|