关于C语言的

#define N 200
#include <graphics.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define Esc 0x011b
int i,key;
int score=0;
int gamespeed=50000;
struct Food
{
int x;
int y;
int yes;
}food;
struct Snake
{
int x[N];
int y[N];
int node;
int direction;
int life;
}snake;
void Init(void);
void Close(void);
void DrawK(void);
void GameOver(void);
void GamePlay(void);
void PrScore(void);
void main()
{
Init();
DrawK();
GamePlay();
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\TC");
cleardevice();
}
void DrawK(void)
{
setbkcolor(LIGHTGREEN);
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49);
rectangle(i,451,i+10,460);
}
for(i=40;i<450;i+=10)
{
rectangle(50,i,59,i+10);
rectangle(601,i,610,i+10);
}
}
void GamePlay(void)
{
/*randomize();*/
food.yes=1;
snake.life=0;
snake.direction=1;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.node=2;
PrScore();
while(1)
{
while(!kbhit())
{
if(food.yes==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.yes=0;
}
if(food.yes==0)
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch(snake.direction)
{
case 1:snake.x[0]+=10;break;
case 2:snake.x[0]-=10;break;
case 3:snake.y[0]-=10;break;
case 4:snake.y[0]+=10;break;
}
for(i=3;i<snake.node;i++)
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
}
void PrScore(void)
{
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);
}
void Close(void)
{
getch();
closegraph();
}贪吃蛇源程序
这个程序在VC++中执行时提示#include <graphics.h>有问题
请问应该怎么调试?

VC++编译环境没有graphics.h头文件,你必须在TC环境下运行!另外你的程序缺少GameOver()函数,而且void GamePlay(void)函数中缺少多个右花括号}。
给你一个编得不错的贪吃蛇,在TC环境下运行!

#include <stdlib.h>
#include <graphics.h>
#include <bios.h>
#include <dos.h>
#include <conio.h>
#define Enter 7181
#define ESC 283
#define UP 18432
#define DOWN 20480
#define LEFT 19200
#define RIGHT 19712
void Initgra(void);
void TheFirstBlock(void);
void DrawMap(void);
void Initsnake(void);
void Initfood(void);
void Snake_Headmv(void);
void Flag(int,int,int,int);
void GameOver(void);
void Snake_Bodymv(void);
void Snake_Bodyadd(void);
void PrntScore(void);
void Win(void);
void TheSecondBlock(void);
void Food(void);
void Dsnkorfd(int,int,int);
void Delay(int);
struct Snake
{int x;int y;int color;}Snk[200];
struct Food
{int x;int y;int color;}Fd;
int flag1=1,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,
checkx,checky,num=2,key=0,Times=0,Score,Hscore,Snkspeed;
char Sco[2];
void main()
{ Initgra();
TheFirstBlock();
while(1)
{DrawMap();
Snake_Headmv();
GameOver();
if(key==Enter)
{cleardevice();
Initsnake();
Initfood();
key=0;
Hscore=1;
}
Snake_Bodymv();
Snake_Bodyadd();
PrntScore();
Win();
if(key==ESC)
break;
TheSecondBlock();
Food();
Delay(Snkspeed);
}
closegraph();
}
void Initgra()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc");
}
void TheFirstBlock()
{setcolor(11);
settextstyle(0,0,4);
outtextxy(100,220,"The First Block");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
Initsnake();
Initfood();
Score=0;
Hscore=1;
Snkspeed=1;
}
else goto loop;
}
void DrawMap()
{line(10,10,470,10);
line(470,10,470,470);
line(470,470,10,470);
line(10,470,10,10);
line(480,20,620,20);
line(620,20,620,460);
line(620,460,480,460);
line(480,460,480,20);
}
void Initsnake()
{randomize();
num=2;
Snk[0].x=random(440);
Snk[0].x=Snk[0].x-Snk[0].x%20+50;
Snk[0].y=random(440);
Snk[0].y=Snk[0].y-Snk[0].y%20+50;
Snk[0].color=4;
Snk[1].x=Snk[0].x;
Snk[1].y=Snk[0].y+20;
Snk[1].color=4;
}
void Initfood()
{randomize();
Fd.x=random(440);
Fd.x=Fd.x-Fd.x%20+30;
Fd.y=random(440);
Fd.y=Fd.y-Fd.y%20+30;
Fd.color=random(14)+1;
}
void Snake_Headmv()
{if(bioskey(1))
{key=bioskey(0);
switch(key)
{case UP:Flag(1,0,0,0);break;
case DOWN:Flag(0,1,0,0);break;
case LEFT:Flag(0,0,1,0);break;
case RIGHT:Flag(0,0,0,1);break;
default:break;
}
}
if(flag1)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].y-=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag2)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].y+=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag3)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].x-=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag4)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].x+=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
}
void Flag(int a,int b,int c,int d)
{flag1=a;flag2=b;flag3=c;flag4=d;}
void GameOver()
{int i;
if(Snk[0].x<20||Snk[0].x>460||Snk[0].y<20||Snk[0].y>460)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
key=bioskey(0);
}
else for(i=3;i<num;i++)
{if(Snk[0].x==Snk[i].x&&Snk[0].y==Snk[i].y)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
key=bioskey(0);
}
}
}
void Snake_Bodymv()
{int i,s,t;
for(i=1;i<num;i++)
{Dsnkorfd(checkx,checky,Snk[i].color);
Dsnkorfd(Snk[i].x,Snk[i].y,0);
s=Snk[i].x;
t=Snk[i].y;
Snk[i].x=checkx;
Snk[i].y=checky;
checkx=s;
checky=t;
}
}
void Food()
{if(flag5)
{randomize();
Fd.x=random(440);
Fd.x=Fd.x-Fd.x%20+30;
Fd.y=random(440);
Fd.y=Fd.y-Fd.y%20+30;
Fd.color=random(14)+1;
flag5=0;
}
Dsnkorfd(Fd.x,Fd.y,Fd.color);
}
void Snake_Bodyadd()
{if(Snk[0].x==Fd.x&&Snk[0].y==Fd.y)
{if(Snk[num-1].x>Snk[num-2].x)
{num++;
Snk[num-1].x=Snk[num-2].x+20;
Snk[num-1].y=Snk[num-2].y;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].x<Snk[num-2].x)
{num++;
Snk[num-1].x=Snk[num-2].x-20;
Snk[num-1].y=Snk[num-2].y;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].y>Snk[num-2].y)
{num++;
Snk[num-1].x=Snk[num-2].x;
Snk[num-1].y=Snk[num-2].y+20;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].y<Snk[num-2].y)
{num++;
Snk[num-1].x=Snk[num-2].x;
Snk[num-1].y=Snk[num-2].y-20;
Snk[num-1].color=Fd.color;
}
flag5=1;
Score++;
}
}
void PrntScore()
{if(Hscore!=Score)
{randomize();
setcolor(random(14)+1);
rectangle(485,155,615,195);
setcolor(11);
settextstyle(0,0,3);
outtextxy(490,160,"SCORE");
setcolor(2);
rectangle(520,200,550,240);
floodfill(530,220,2);
Sco[0]=(char)(Score+48);
Sco[1]='\0';
Hscore=Score;
setcolor(4);
settextstyle(3,0,3);
outtextxy(530,200,Sco);
}
}
void Win()
{if(Score==10)
Times++;
if(Times==2)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"You Win");
key=bioskey(0);
}
}
void TheSecondBlock()
{if(Score==10)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(100,220,"The Second Block");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
Initsnake();
Initfood();
Score=0;
Snkspeed=9;
key=0;
}
else goto loop;
}
}
void Dsnkorfd(int x,int y,int color)
{setcolor(color);
setfillstyle(1,color);
circle(x,y,10);
floodfill(x,y,color);
}
void Delay(int times)
{int i;
for(i=1;i<=times;i++)
delay(15000);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-01-10
这个程序是按C言语中的函数库编的。graphics.h是基于dos的图形库,是turbo c/c++的专门库的一个头文件,用来定义borland绘图功能的函数。
要调试的话,改的太多了。一时半会我也改不过来。
VC也有自带的绘图函数库。有时间会去给你改改。
第2个回答  2020-01-13
相似回答