主题:一段运行不了贪吃蛇C代码
这段程序的图形界面能运行,只是蛇怎么也出不来,中间有显示器驱动函数。小生做了一晚上了,怎么也弄不明白,麻烦哪位高手能不厌其烦的把小生改写下,由于比较简单,就没加注解了。如果运行不成功,功劳就白费了具体代码如下:
#include <stdio.h>
#include <graphics.h>
#include <time.h> //所需头文件
#include <dos.h>
#include <bios.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 GamePlay(void);
void GameOver(int score);
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;
};
void main(void)
{
InitGraph();
DrawFence();
GamePlay();
closegraph();
}
void InitGraph(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(BLUE);
setcolor(WHITE);
settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
outtextxy(170,150,"Greedy Snake");
outtextxy(249,254,"Ready?");
setcolor(BLUE);
}
void DrawFence(void)
{
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);
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)
{
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!=10)
{
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-i];
}
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.y[0]>455)
{
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(snake.life==1) break;
key=bioskey(0);
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);
settextstyle(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 <stdio.h>
#include <graphics.h>
#include <time.h> //所需头文件
#include <dos.h>
#include <bios.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 GamePlay(void);
void GameOver(int score);
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;
};
void main(void)
{
InitGraph();
DrawFence();
GamePlay();
closegraph();
}
void InitGraph(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
setbkcolor(BLUE);
setcolor(WHITE);
settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
outtextxy(170,150,"Greedy Snake");
outtextxy(249,254,"Ready?");
setcolor(BLUE);
}
void DrawFence(void)
{
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);
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)
{
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!=10)
{
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-i];
}
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.y[0]>455)
{
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(snake.life==1) break;
key=bioskey(0);
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);
settextstyle(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);
}