#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<bios.h>
#include<time.h>
/*The wall-point (x0,y0) to (x1,y1)*/
#define X0 50
#define Y0 50
#define X1 560
#define Y1 400
/*COLOR USE*/
#define BCOLOR BLACK
#define SCOLOR BLUE
#define FCOLOR GREEN
#define MAXN 100
#define PASS 80
#define NSPEED 7000
/*bioskey*/
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define SPACE 0x3920
#define uSP 0x1675
#define USP 0x1655
#define dSP 0x2064
#define DSP 0x2044
#define F1 0x3b00
long unsigned int SPEED=NSPEED; /*The speed of the snake move*/
int fx=X1-50,fy=Y1-50; /*The position of the food*/
int key,sflag,sflag2;
void *buf;
void initgp();
void help();
void writeWord();
void end();
void drawWall();
void drawSnake();
void drawlip();
void putfood();
void playSnake();
void initSnake();
void PrScore();
void Redraw(char);
void music(enum fremusic);
void fastRectDraw(int,int,int);
enum GameState checkkey();
enum fremusic{EAT_FOOD,PLAY,FAIL,WIN};
enum direction{up,down,left,right};
enum GameState{Gon,Gstop,Gend};
struct se{
int x[MAXN],y[MAXN];
int node;
enum direction dir;
char alive;
}*snake;
/********************************** MAIN **********************************/
int main(void)
{
initgp();
playSnake();
end();
return 0;
}
/********************************PLAY THE GAME*********************************/
void playSnake()
{
char temp,str1[20];
int i,j;
enum GameState state;
state=Gon;
while(state!=Gend)
{
temp=snake->node-1;
/******************************************************
Recieve the state information for the game:
1.Does the snake get the food?(eat or not)
2.Does the snake hit the wall?(game over or not)
3.Does the snake hit its tail?(game over or not)
*****************************************************/
/*1. food*/
if(snake->x[temp]==fx&&snake->y[temp]==fy)
{
snake->node++;
if(snake->node==PASS+2)
{state=Gend;goto ENDOUT;}
PrScore();
putfood();
music(EAT_FOOD);
}
/*2. wall*/
for(i=X0;i<=X1;i=i+10)
for(j=0;j<snake->node;j++)
if(snake->x[j]==i&&(snake->y[j]==Y0||snake->y[j]==Y1))
{state=Gend;goto ENDOUT;}
for(i=Y0;i<=Y1;i=i+10)
for(j=0;j<snake->node;j++)
if(snake->y[j]==i&&(snake->x[j]==X0||snake->x[j]==X1))
{state=Gend;goto ENDOUT;}
/*3. tail*/
if(snake->node>2)
for(i=0;i<snake->node-2;i++)
if(snake->x[i]==snake->x[temp]&&snake->y[i]==snake->y[temp])
{state=Gend;goto ENDOUT;}
/********************************************************
Recieve the key-press information from The GamePlayer
1.whice key does the GamePlayer press
2.how to deal with those information
********************************************************/
state=checkkey();
if(state==Gstop)
{
settextstyle(0,0,2);
setcolor(WHITE);
outtextxy(240,200,"Stop");
while(bioskey(1)||bioskey(0)!=SPACE);
}
else
{
settextstyle(0,0,2);
setcolor(BCOLOR);
outtextxy(240,200,"Stop");
}
/**************************
Redraw the scence
Include:wall,snake and food
***************************/
if(temp<(*snake).node-1)/*It meams that the snake get a food*/
Redraw(1);
else Redraw(0);
music(PLAY);
ENDOUT:;
}
}
/*Recevie the key-press from the GamePlayer*/
enum GameState checkkey()
{
if(bioskey(1))
{
key=bioskey(0);
switch(key)
{
case ESC:return Gend;
case SPACE:return Gstop;
case LEFT:if(snake->dir!=right) snake->dir=left;break;
case RIGHT:if(snake->dir!=left) snake->dir=right;break;
case UP:if(snake->dir!=down) snake->dir=up;break;
case DOWN:if(snake->dir!=up) snake->dir=down;break;
case uSP:
case USP:if(SPEED>NSPEED/3){SPEED=SPEED-NSPEED/3;sflag2=sflag++;PrScore();}
break;
case dSP:
case DSP:if(SPEED<NSPEED*3){SPEED=SPEED+NSPEED/2;sflag2=sflag--;PrScore();}
break;
case F1:help();break;
default:break;
}
}
return Gon;
}
/******************* THE TOOL FOR DRAWING SCENE ***************************/
/*Draw the Wall*/
void drawWall()
{
int i,color;
color=rand()%16;
if(color==BCOLOR) color++;
for(i=X0;i<=X1;i+=10)
{
fastRectDraw(i,Y0,color);
fastRectDraw(i,Y1,color);
}
for(i=Y0;i<=Y1;i+=10)
{
fastRectDraw(X0,i,color);
fastRectDraw(X1,i,color);
}
}
/*Draw the Snake*/
void drawSnake()
{
int i;
for(i=0;i<snake->node-1;i++)
fastRectDraw(snake->x[i],snake->y[i],SCOLOR);
i=snake->node-1;
setfillstyle(SOLID_FILL,SCOLOR);
bar(snake->x[i],snake->y[i],snake->x[i]+10,snake->y[i]+10);
}
/*Draw the food*/
void putfood()
{
char i,flag;
fastRectDraw(fx,fy,BCOLOR);
srand((unsigned int)time(NULL));
fx=rand()%((X1-X0-10)/10)+X0/10+1;
fy=rand()%((Y1-Y0-10)/10)+Y0/10+1;
fx=fx*10;fy=fy*10;
i=0;
while(i!=snake->node)
{
for(i=0;i<snake->node;i++)
{
if(fx==snake->x[i]&&fy==snake->y[i])
break;
}
if(i!=snake->node)
{
fx=rand()%((X1-X0-10)/10)+X0/10+1;
fy=rand()%((Y1-Y0-10)/10)+Y0/10+1;
fx=fx*10;fy=fy*10;
}
}
fastRectDraw(fx,fy,FCOLOR);
}
/*Output the Score*/
void PrScore()
{
char str1[20],str2[10],str3[10];
settextstyle(0,0,1);
sprintf(str1,"Score %3d",(snake->node-3)*10);
sprintf(str2,"Food %3d",PASS+3-snake->node);
sprintf(str3,"Speed %2d",sflag2);
setcolor(BCOLOR);
outtextxy(X0,40,str1);
outtextxy(X0,30,str2);
outtextxy(X0+100,30,str3);
sprintf(str1,"Score %3d",(snake->node-2)*10);
sprintf(str2,"Food %3d",PASS+2-snake->node);
sprintf(str3,"Speed %2d",sflag);
setcolor(YELLOW);
outtextxy(X0,40,str1);
setcolor(FCOLOR);
outtextxy(X0,30,str2);
setcolor(RED);
outtextxy(X0+100,30,str3);
outtextxy(X0+100,40,"F1 help");
}
/*draw a hot lip*/
void drawlip()
{
int size;
char lip[]={
/*-- 调入了一幅图像:香吻.bmp --*/
/*-- 宽度x高度=64x64 --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00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