贪吃蛇
#include<graphics.h>#include<stdlib.h>
#include<time.h>
#include<dos.h>
#define MAX_JOINTS 200
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define MV_RIGHT 1
#define MV_LEFT 2
#define MV_UP 3
#define MV_DOWN 4
void InitGraph(void);
void DrawFence(void);
void GameOver(int score);
void GamePlay(void);
void PrScore(int score);
struct Food
{
int x;
int y;
int addFood;
};
struct Snake
{
int x[MAX_JOINTS];
int y[MAX_JOINTS];
int joint;
int direction;
int life;
};
main()
{
InitGraph();
DrawFence();
GamePlay();
dlosegraph();
}
void InitGraph(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
sleardevice();
setbkcolor(BLUE);
cleardevice();
setcolor(WHITE);
settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
outtextxy(170,150,"Greedy Snake");
outtextxy(219,254,"Ready?");
setcolor(BLUE);
sleardevice();
}
void DrawFence(viod)
{
int i;
setbkcolor(LIGHTGREEN);
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50,i<=600;i+=10)
{
rectangle(i,40,i+10,49);
rectanhle(i,451,i+10,460);
}
for (i=40;i<=450,i+=10)
{
rectanhle(50,i,59,i+10);
rectanhle(601,i,610,i+10);
}
}
void GamePlay(void)
{
int i,key;
int gamespeed=22000;
int score=0;
struct Food food;
struct Snake snake;
food.addFood=1;
snake.life=0;
snake.direction=MV_RIGHT;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.joint=2;
PrScore(score);
srand(time(NULL));
while(1)
{
while(!kbhit())
{
if(food.addFood==1)
{
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0)
{
food.x++;
}
while(food.y%10!=0)
{
food.y++;
}
food.addFood=0;
}
if(food.addFood==0)
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.joint-1;i>0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch(snake.direction)
{
case MV_RIGHT:snake.x[0]+=10;break;
case MV_LEFT:snake.x[0]-=10;break;
case MV_UP:snake.y[0]-=10;break;
case MV_DOWN:snake.y[0]+=10;break;
}
for(i=3;i<snake.joint;i++)
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver(score);
snake.life=1;
break;
}
}
if(snake.x[0]<55||snake.x[0]>595
||snake.y[0]<55||snake.u[0])
{
GameOver(score);
snake.life=1;
}
if(snake.life==1) break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10)
snake.x[snake.joint]=-20;snake.y[snake.joint]=-20;
snake.joint++;
food.addFood=1;
score+=10;
PrScore(score);
}
setcolor(4);
for(i=0;i<snake.joint;i++)
{
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
}
delay(gamespeed);
setcolor(0);
rectangle(snake.x[snake.joint-1],snake.y[snake.joint-1],snake.x[snake.joint-1]+10,snake.y[snake.joint-1]-10);
}
if(key==ESC) break;
else if(key==UP&&snake.direction!=4)
snake.direction=MV_UP;
else if(key==RIGHT&&snake.direction!=MV_LEFT)
snake.direction=MV_RIGHT;
else if(key==LEFT&&snake.direction!=MV_RIGHT)
snake.direction=MV_LEFT;
else if(key==DOWN&&snake.direction!=MV_UP)
snake.direction=MV_DOWN;
}
}
void GameOver(int score)
{
cleardevice();
PrScore(score);
setcolor(RED);
serrextstle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
void PrScore(int score)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
}
照着书上打的代码,但是一编译就显示#include<graphics.h>这个库文件有问题,我的编译环境是codeblocks,大神帮忙看看该如何改动好