C语言实例教程:撞不死的贪吃蛇
贪吃蛇源码 (C语言精简版呀,撞不死的哦)
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<bios.h>
#include<stdlib.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==RIGHT&&board[y][x+1]!=1)right();
else if(body->direction==DOWN&&board[y+1][x]!=1)down();
else if(body->direction==LEFT&&board[y][x-1]!=1)left();
else if(body->direction==UP&&board[y-1][x]!=1)up();
return 0;
}
void print() /*在屏幕上显示蛇*/
{int i,j,x=0,y=0;
for(i=1;i<21;i++)
for(j=1;j<21;j++)
board[i][j]=0;
for(i=0;i<20;i++)
{x=body[i].x;
y=body[i].y;
board[y][x]=1;
}
board[food.y][food.x]=2;
for(i=1;i<21;i++)
for(j=1;j<21;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;i<21;i++)
for(j=0;j<21;j++)
board[i][j]=0;
for(i=0;i<22;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,body[2].y=2;
makefood();
print();
getch();
for(i=0;i<30000;i++)
{
move(&body[0]);
getdirection();
flag=eatfood();
if(flag==TRUE){print();makefood();};
print();
for(j=0;j<second;j++)delay(200);
}
line(0,0,400,400);
getch();
}
void right()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].x++;
}
void down()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].y++;
}
void left()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].x--;
}
void up()
{int i;
for(i=snakelength-1;i>0;i--)
{body[i].x=body[i-1].x;
body[i].y=body[i-1].y;
}
body[0].y--;
}
void getdirection()
{int key=0;
if(bioskey(1)!=0)
key=bioskey(0);
switch(key)
{case VK_UP: if(body[0].direction!=DOWN)body[0].direction=UP;break;
case VK_RIGHT: if(body[0].direction!=LEFT)body[0].direction=RIGHT;break;
case VK_DOWN: if(body[0].direction!=UP)body[0].direction=DOWN;break;
case VK_LEFT: if(body[0].direction!=RIGHT)body[0].direction=LEFT;break;
case VK_ESC: exit(0);
}
}
void makefood()
{int i,j;
food.x=0;
food.y=0;
print();
food.x=random(20);
food.y=random(20);
for(;;)
{ if(board[food.y][food.x]!=0)
{
food.x=random(20);
food.y=random(20);
}
else break;
}
food.direction=body[0].direction;
board[food.y][food.x]=2;
}
int eatfood()
{int i=FALSE;
int x=body[0].x,y=body[0].y;
if(body[0].direction==UP&&board[y-1][x]==2)
{ snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
if(body[0].direction==DOWN&&board[y+1][x]==2)
{ snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
if(body[0].direction==LEFT&&board[y][x-1]==2)
{ snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
if(body[0].direction==RIGHT&&board[y][x+1]==2)
{ snakelength++;
for(i=snakelength-1;i>0;i--){body[i].x=body[i-1].x;body[i].y=body[i-1].y;}
body[0].x=food.x;
body[0].y=food.y;
food.x=0;
food.y=0;
i=TRUE;
}
return i;
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |