C语言最简洁的贪吃蛇源代码.doc
文本预览下载声明
C语言最简洁的贪吃蛇源代码.txt每天早上起床都要看一遍“福布斯”富翁排行榜,如果上面没有我的名字,我就去上班。谈钱不伤感情,谈感情最他妈伤钱。我诅咒你一辈子买方便面没有调料包。
#includegraphics.h
#includeconio.h
#includedos.h
#includebios.h
#includestdlib.h
#define STATIC 0
#define TRUE 1
#define FALSE 0
#define UP 1
#define RIGHT 2
#define DOWN 3
#define LEFT 4
#define VK_LEFT 0x4b00 /*上下左右键的值*/
#define VK_RIGHT 0x4d00
#define VK_DOWN 0x5000
#define VK_UP 0x4800
#define VK_ESC 0x011b
int board[22][22];
int snakelength=0;
struct snake{
public:
int x=0;
int y=0;
int direction;
}body[20];
snake food;
void makefood();/*产生一个食物*/
int eatfood(); /*蛇吃掉食物*/
void right(); /*上下左右的函数了*/
void down();
void left();
void up();
void getdirection(); /*判断蛇的方向*/
move(snake *body)/*让蛇动起来*/
{int x=body[0].x,y=body[0].y;
if(body-direction==RIGHTboard[y][x+1]!=1)right();
else if(body-direction==DOWNboard[y+1][x]!=1)down();
else if(body-direction==LEFTboard[y][x-1]!=1)left();
else if(body-direction==UPboard[y-1][x]!=1)up();
return 0;
}
void print() /*在屏幕上显示蛇*/
{int i,j,x=0,y=0;
for(i=1;i21;i++)
for(j=1;j21;j++)
board[i][j]=0;
for(i=0;i20;i++)
{x=body[i].x;
y=body[i].y;
board[y][x]=1;
}
board[food.y][food.x]=2;
for(i=1;i21;i++)
for(j=1;j21;j++)
{if(board[i][j]==1)
{setfillstyle(SOLID_FILL,WHITE);
bar(j*15,i*15,j*15+13,i*15+13);
}
if(board[i][j]==0)
{setfillstyle(SOLID_FILL,BLACK);
bar(j*15,i*15,j*15+13,i*15+13);
}
if(board[i][j]==2)
{setfillstyle(SOLID_FILL,RED);
bar(j*15,i*15,j*15+13,i*15+13);
}
}
}
main(int second=0)
{
int gdriver=CGAC0,gmode;
initgraph(gdriver,gmode,c:\\tc\\bgi);/*BGI文件夹的路径,我的是c:\tc\bgi,这里得自己改下*/
randomize();
int i,j;
int flag;
long time=100000;
for(i=0;i21;i++)
for(j=0;j21;j++)
board[i][j]=0;
for(i=0;i22;i++)
{board[0][i]=1;board[21][i]=1;board[i][0]=1;board[i][21]=1;}
snakelength=3;
body[0].x=3,body[0].y=2,body[0].direction=RIGHT;
body[1].x=2,body[1].y=2;
body[2].x=1,bod
显示全部