2021-05-01 17:24

# 我做的这个贪吃蛇的蛇速度太快怎么办？

``````#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#define N 100
#define SIZE 10
enum Ch{up,down,left,right};

struct coor{
int x;
int y;
};
struct p{
int l;
struct coor zb[N];
Ch fx;
};
struct food{
struct coor fzb;
bool eat;
};
struct food f;
struct p py;
void g()
{
initgraph(640,480);
py.l=3;
py.fx=right;
py.zb[0].x=100;
py.zb[0].y=100;
py.zb[1].x=90;
py.zb[1].y=100;
py.zb[2].x=80;
py.zb[2].y=100;
f.eat=true;
}
void op()
{
system("cls");
cleardevice();
printf("蛇长:%d\n",py.l);
printf("蛇方向:%d\n",py.fx);
for(int i=0;i<py.l;i++)
{
printf("第%d节蛇的坐标%d %d\n",i,py.zb[i].x,py.zb[i].y);
circle(py.zb[i].x,py.zb[i].y,5);
}
circle(f.fzb.x,f.fzb.y,5);
}

void yd2()
{
if(py.fx==right)
{
py.zb[0].x+=SIZE;
}
if(py.fx==left)
{
py.zb[0].x-=SIZE;
}
if(py.fx==up)
{
py.zb[0].y-=SIZE;
}
if(py.fx==down)
{
py.zb[0].y+=SIZE;
}
for (int j=py.l;j>0;j--)
{
py.zb[j].x=py.zb[j-1].x;
py.zb[j].y=py.zb[j-1].y;
}
}
void ef()
{
if(f.fzb.x==py.zb[0].x&&f.fzb.y==py.zb[0].y){
py.l++;
f.eat=true;
}
}
void yd()
{
char key = getch();
switch(key)
{
case 'w':
if(py.fx!=down)
{
py.fx=up;
}
break;
case 's':
if(py.fx!=up)
{
py.fx=down;
}
break;
case 'a':
if(py.fx!=right)
{
py.fx=left;
}
break;
case 'd':
if(py.fx!=left)
{
py.fx=right;
}
break;
}
}
void rf(){
while(1){
int flag=0;
f.fzb.x=rand()%64*10;
f.fzb.y=rand()%48*10;
for(int i=0;i<py.l;i++){
if(py.zb[i].x==f.fzb.x &&
py.zb[i].y==f.fzb.y){
flag=1;
break;
}
}
if(flag)
{
continue;
}
else
{
f.eat=false;
break;
}
}
}
int over()
{
if(py.zb[0].x>640||py.zb[0].x<0||py.zb[0].y>480||py.zb[0].y<0)
{
return 1;
}
for(int i=py.l-1;i>0;i--)
{
if(py.zb[0].x==py.zb[i].x&&py.zb[0].y==py.zb[i].y)
{
return 1;
}
}
return 0;
}
int main()
{
g();
Sleep(10);
while(1)
{
op();
yd2();
if(kbhit()){
yd();
}
if (over())
{
break;
}

if(f.eat==true){
rf();
}
ef();

}
return 0;
}``````
• 写回答
• 好问题 提建议
• 关注问题
• 收藏
• 邀请回答

#### 2条回答默认 最新

• C和C++技术博客 2021-05-03 16:48
已采纳

C和C++完整教程：https://blog.csdn.net/it_xiangqiang/category_10581430.html

已采纳该答案
评论
解决 无用
打赏 举报
• Xiao_YiXYZ 2021-05-01 17:25

已经修了好久了，本人萌新，要死了QAQ

评论
解决 无用
打赏 举报