贪食蛇<2.0>
游戏中,用四个方向键来控制蛇的运用,空格键暂停,ESC退出!
吃掉十个食物升一级!~升级同时速度加快!
较前一版本更新:
1.更新了蛇身显示,使之更加美观!
2.食物由原来的一个增加到三个!
3.增加了蛇移动时的加速功能,低等级时,可以使用加速功能,更快的吃到食物!
作者:yuleol E_mail:kuworm@126.com (2006.7.11)
附件里有源码及编译后的程序下载
源码:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <bios.h>
#include <dos.h>
#define SPACE 0x3920
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ESC 0x011b
#define LONG 150/*定义最大蛇身长度!*/
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC 18
#endif
struct container{
int left;
int top;
int right;
int bottom;
int sign; /*记号,0表示空白,1表示墙,2表示食物*/
}lab[22][42];/*定义大容器存储结构*/
struct snakes{
int i;
int j;
int sign;/*0空白,1蛇头,2蛇身*/
}snake[LONG];
int mark,grade,max;
/*初始化图形显示*/
int initialize(void)
{
int gdriver, gmode,errorcode;
gdriver=VGA;
gmode=VGAHI;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
return 0;
}
void my_delay(unsigned long ms)
{ /*延时函数*/
double start_time = (double)clock() / CLOCKS_PER_SEC * 1000;
while ( (double)clock() / CLOCKS_PER_SEC * 1000 - start_time < ms ) ;
}
void showcontainer(int i,int j)
{/*显示容器函数*/
if(lab[i][j].sign!=0 && lab[i][j].sign!=1 && lab[i][j].sign!=2)
{
setfillstyle(SOLID_FILL,BLUE);
bar(lab[i][j].left,lab[i][j].top,lab[i][j].right,lab[i][j].bottom);
}
switch(lab[i][j].sign)
{
case 0: setfillstyle(SOLID_FILL,WHITE);break;
case 1: setfillstyle(SOLID_FILL,RED); break;
case 2: setfillstyle(SOLID_FILL,RED);break;
case 3: setfillstyle(SOLID_FILL,GREEN);break;
case 4: setfillstyle(SOLID_FILL,CYAN);break;
case 5: setfillstyle(SOLID_FILL,MAGENTA);break;
case 6: setfillstyle(SOLID_FILL,BROWN);break;
case 7: setfillstyle(SOLID_FILL,LIGHTGRAY);break;
case 8: setfillstyle(SOLID_FILL,DARKGRAY);break;
case 9: setfillstyle(SOLID_FILL,LIGHTBLUE);break;
case 10: setfillstyle(SOLID_FILL,LIGHTGREEN);break;
case 11: setfillstyle(SOLID_FILL,LIGHTCYAN);break;
case 12: setfillstyle(SOLID_FILL,LIGHTRED);break;
case 13: setfillstyle(SOLID_FILL,LIGHTMAGENTA);break;
case 14: setfillstyle(SOLID_FILL,YELLOW);break;
}
if(lab[i][j].sign!=0 && lab[i][j].sign!=1 && lab[i][j].sign!=2)
bar(lab[i][j].left+2,lab[i][j].top+2,lab[i][j].right-2,lab[i][j].bottom-2);
else
bar(lab[i][j].left,lab[i][j].top,lab[i][j].right,lab[i][j].bottom);
}
/*生成容器函数*/
void initialcontainer()
{
int i,j,leftx=100,topy=50,rightx=110,bottomy=60;
for(i=0;i<22;i++)
for(j=0;j<42;j++)
{
lab[i][j].left=leftx+j*10;
lab[i][j].top=topy+i*10;
lab[i][j].right=rightx+j*10;
lab[i][j].bottom=bottomy+i*10;
lab[i][j].sign=0;
}
for(i=0;i<22;i++)
{
lab[i][0].sign=1;
lab[i][41].sign=1;
}
for(j=0;j<42;j++)
{
lab[0][j].sign=1;
lab[21][j].sign=1;
}
for(i=0;i<22;i++)
for(j=0;j<42;j++)
showcontainer(i,j);
}
void initialsnake()
{/*初始化蛇身*/
int i;
for(i=3;i<LONG;i++)
snake[i].sign=0;
snake[0].i=10;
snake[0].j=3;
snake[0].sign=2; /*蛇头*/
snake[1].i=10; /*蛇身*/
snake[1].j=2;
snake[1].sign=3;
snake[2].i=10;
snake[2].j=1;
snake[2].sign=4;
}
void showsnake()
{/*显示蛇*/
int n,i,j;
for(n=0;n<LONG && snake[n].sign!=0;n++)
{
i=snake[n].i;
j=snake[n].j;
lab[i][j].sign=snake[n].sign;
showcontainer(i,j);
}
}
void cleansnake()
{/*擦除蛇*/
int n,i,j;
for(n=0;n<LONG && snake[n].sign!=0;n++)
{
i=snake[n].i;
j=snake[n].j;
lab[i][j].sign=0;
showcontainer(i,j);
}
}
void createfood()
{/*创建食物*/
int n,i,j,flag;
srand((int)time(0));
do{
flag=1;
i=rand()%20+1;
j=rand()%40+1;
if(lab[i][j].sign!=0) flag=0;
}while(flag==0);
lab[i][j].sign=rand()%12+3;
showcontainer(i,j);
}
void showmark()
{/*显示分数和等及*/
char b[10],*p=b;
p=itoa(mark,p,10);
setfillstyle(SOLID_FILL,BLUE);
bar(200,20, 400, 40);
moveto(210,25);
outtext("score:");
outtext(p);
p=itoa(grade,p,10);
outtext(" grade:");
outtext(p);
}
int run(int i,int j)
{
int n;
if(i<1 || i>20 || j<1 || j>40)
return 0;/*碰壁*/
for(n=0;n<LONG && snake[n].sign!=0;n++)
if(snake[n].i==i && snake[n].j==j)
return 0;/*碰到自己*/
if(lab[i][j].sign!=0 && lab[i][j].sign!=1 && lab[i][j].sign!=2)
{/*遇到食物*/
snake[max].sign=lab[i][j].sign;
max++;
lab[i][j].sign=0;
mark=mark+10*grade;
if((max-3)%10==0 && grade<10) grade++;
showmark();
createfood();
}
cleansnake();
for(n=max;n>=1;n--)
{
snake[n].i=snake[n-1].i;
snake[n].j=snake[n-1].j;
}
snake[0].i=i;
snake[0].j=j;
showsnake();
return 1;
}
void pause()
{/*暂停*/
setfillstyle(SOLID_FILL,BLUE);
bar(120,300, 480, 350);
moveto(130,320);
outtext("Press any key to start...");
while(!bioskey(0)) sleep(1);
bar(120,300, 480, 350);
moveto(130,320);
outtext("Press SPACE to pause,ESC to exit...");
}
int main(void)
{
int i=10,j=3,flag=1,n,m;
int key=RIGHT,keytemp,keyreve=LEFT;
mark=0;
grade=1;
max=3;
initialize();/*初始化图形显示*/
setbkcolor(LIGHTBLUE);
setcolor(RED);
initialcontainer();
initialsnake();
showsnake();
createfood();
createfood();
createfood();
showmark();
pause();
while(flag==1)
{
if(bioskey(1))
keytemp=bioskey(0);
if(keytemp==ESC)
break;
if(keytemp==SPACE)
{
pause();
keytemp=key;
}
if((keytemp==UP || keytemp==DOWN || keytemp==RIGHT || keytemp==LEFT)&&keytemp!=keyreve)
key=keytemp;
switch(key)
{
case UP: i--;keyreve=DOWN;break;
case DOWN: i++;keyreve=UP;break;
case RIGHT: j++;keyreve=LEFT;break;
case LEFT: j--;keyreve=RIGHT;break;
}
flag=run(i,j);
keytemp=0;
for(m=11-grade;m>=0 && flag==1;m--)/*加速及延时*/
{
if(bioskey(1))
keytemp=bioskey(0);
if(keytemp==key)
{
switch(key)
{
case UP: i--;break;
case DOWN: i++;break;
case RIGHT: j++;break;
case LEFT: j--;break;
}
flag=run(i,j);
my_delay(20);
keytemp=0;
}
else
my_delay(4);
}
}
setfillstyle(SOLID_FILL,BLUE);
bar(120,300, 480, 350);
moveto(130,310);
outtext("GAME OVER!");
moveto(130,330);
outtext("Press any key to exit...");
sleep(2);
fflush(stdin);
getch();
closegraph();/*关闭图形显示*/
return 0;
}