本人刚学C不久,编了这个小游戏在windows2000+ccdos+tc2,0下编译通过!
欢迎大家提意见!附件里有编译好的可执行文件和源码!
/*============================================================
贪食蛇游戏V1.0
============================================================*/
#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 100/*定义最大蛇身长度!*/
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 showcontainer(int i,int j)
{/*显示容器函数*/
switch(lab[i][j].sign)
{
case 0: setfillstyle(SOLID_FILL,LIGHTBLUE);break;
case 1: setfillstyle(SOLID_FILL,RED); break;
case 2: setfillstyle(SOLID_FILL,LIGHTCYAN);break;
}
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=1;
snake[1].i=10;
snake[1].j=2;
snake[1].sign=2;
snake[2].i=10;
snake[2].j=1;
snake[2].sign=2;
}
void showsnake()
{/*显示蛇*/
int n,i,j;
for(n=0;n<LONG && snake[n].sign!=0;n++)
{
switch(snake[n].sign)
{
case 1:setfillstyle(SOLID_FILL,BLUE);break;
case 2:setfillstyle(SOLID_FILL,GREEN);break;
}
i=snake[n].i;
j=snake[n].j;
bar(lab[i][j].left,lab[i][j].top,lab[i][j].right,lab[i][j].bottom);
}
}
void cleansnake()
{/*擦除蛇*/
int n,i,j;
for(n=0;n<LONG && snake[n].sign!=0;n++)
{
setfillstyle(SOLID_FILL,LIGHTBLUE);
i=snake[n].i;
j=snake[n].j;
bar(lab[i][j].left,lab[i][j].top,lab[i][j].right,lab[i][j].bottom);
}
}
void createfood()
{/*创建食物*/
int n,i,j,flag;
srand((int)time(0));
do{
flag=1;
i=rand()%20+1;
j=rand()%40+1;
for(n=0;n<LONG && snake[n].sign!=0;n++)
if(i==snake[n].i && j==snake[n].j)
{
flag=0;
break;
}
}while(flag==0);
lab[i][j].sign=2;
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;/*碰到自己*/
cleansnake();
if(lab[i][j].sign==2)
{/*遇到食物*/
snake[max].sign=2;
max++;
lab[i][j].sign=0;
mark=mark+100*grade;
if((max-3)%10==0) grade++;
showmark();
createfood();
}
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(1)) 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;
int key=RIGHT,keytemp,keyreve=LEFT;
mark=0;
grade=1;
max=3;
initialize();/*初始化图形显示*/
setbkcolor(LIGHTBLUE);
setcolor(RED);
initialcontainer();
initialsnake();
showsnake();
createfood();
showmark();
pause();
while(flag==1)
{
if(bioskey(1))
keytemp=bioskey(0);
if(keytemp==ESC)
break;
if(keytemp==SPACE)
{
pause();
keytemp=key;
}
else
{
if(keytemp==UP || keytemp==DOWN || keytemp==RIGHT || keytemp==LEFT)
if(keytemp!=key && 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);
for(n=0;n<20-grade*2;n++)
delay(9000);/*关于这个函数的使用,我一直弄不明白,按照资料说明这应该暂停了9秒钟,但是我在实际编译中,只暂字了不到十分之一秒~晕*/
}
}
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(5);
fflush(stdin);
getch();
closegraph();/*关闭图形显示*/
return 0;
}