大一党c语言初学。。关于贪吃蛇问题
用的编译器是vs2012。。。。有两个问题想请教各位大大。。。。第一、蛇吃了第一个食物,第二个食物就开始随机出现乱跑,根本吃不到第二个食物。
第二、围墙画出来在蛇的运动范围内同时会出现竖型条纹
程序代码:#define N 200
typedef struct snake S;
typedef struct food F;
struct snake
{
int x[N];
int y[N];
int node;
int life;
int direction;
int speed;
};
struct food
{
int x,y;
int flag;//是否改变食物的位置
};
S initsnake();//初始化蛇
F outfood();//初始化食物
void printfwall();//画墙
void drawsnake(S s);//画蛇
void drawfood(F f);//画食物
S updatesnake(S s);//修改蛇数据
S updatsnakedirection(S s);//修改蛇运动方向
F updadtfood(F f);//修改食物数据
int towall(S s);//判断蛇撞墙没
int toitself(S s);//判断蛇撞自己没
int eatfood(S s,F f);//判断吃食物
void score(int score);//设置分数
void gameover();//游戏结束
void gameplay();//游戏数据
#include<graphics.h>
void drawfood(F f)
{
setcolor(RED);
rectangle(f.x,f.y,f.x+10,f.y-10);
}
void printfwall()
{
int i;
setcolor(YELLOW);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49);
rectangle(i,45,i+10,460);
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10);
rectangle(601,i,610,i+10);
}
}
void drawsnake(S s)
{
int i;
setcolor(BLUE);
for(i=0;i<s.node;i++)
{
rectangle(s.x[i],s.y[i],s.x[i]+10,s.y[i]-10);
}
}
int towall(S s)
{
if(s.x[0]<55||s.x[0]>595||s.y[0]<55||s.y[0]>455)
return 1;
else
return 0;
}
int toitself(S s)
{
int i;
for(i=3;i<s.node;i++)
{
if(s.x[i]==s.x[0]&&s.y[i]==s.y[0])
{
return 1;
}
}
return 0;
}
int eatfood(S s,F f)
{
if(s.x[0]==f.x&&s.y[0]==f.y)
{
return 1;
}
return 0;
}
S initsnake()
{
S s;
s.life=0;
s.direction=1;
s.x[0]=110;
s.y[0]=70;
s.x[1]=100;
s.y[1]=70;
s.node=1;
return s;
}
F outfood()
{
randomize();
F f;
f.x=random(520)+60;
f.y=random(380)+60;
while(f.x%10!=0)
{
f.x++;
}
while(f.y%10!=0)
{
f.y++;
}
f.flag=0;
return f;
}
F updadtfood(F f)
{
randomize();
if(f.flag==1)
{
f.x=random(520)+60;
f.y=random(380)+60;
while (f.x%10!=0)
{
f.x++;
}
while(f.y%10!=0)
{
f.y++;
}
}
return f;
}
S updatesnake(S s)
{
int i;
for(i=s.node-1;i>0;i--)
{
s.x[i]=s.x[i-1];
s.y[i]=s.y[i-1];
}
switch (s.direction)
{
case 1:s.x[0]+=10;break;
case 2:s.x[0]-=10;break;
case 3:s.y[0]-=10;break;
case 4:s.y[0]+=10;break;
}
return s;
}
S updatsnakedirection(S s)
{
key_msg k=getkey();
if(k.key==key_up&&s.direction!=4)
s.direction=3;
else
if(k.key==key_right&&s.direction!=2)
s.direction=1;
else
if(k.key==key_left&&s.direction!=1)
s.direction=2;
else
if(k.key==key_down&&s.direction!=3)
s.direction=4;
return s;
}
void score(int score)
{
setcolor(GREEN);
setfont(30,0,"宋体");
xyprintf(0,0,"分数","%d",score);
}
void gameplay()
{
S s;
F f;
int score=0;
s=initsnake();
f=outfood();
while (1)
{
for (;!kbhit();delay(300))
{
s=updatesnake(s);
f=updadtfood(f);
if (eatfood(s,f)==1)
{
s.node++;
f.flag=1;
score+=100;
}
if(towall(s)||toitself(s))
{
s.life=1;
exit(0);
}
cleardevice();
printfwall();
printfwall();
drawsnake(s);
drawfood(f);
}
s=updatsnakedirection(s);
}
}
void main()
{
initgraph(640,480);
setbkcolor(WHITE);
gameplay();
getch();
closegraph();
}


自顶。。。。。



没有vc 加油