Xiao_YiXYZ 2021-05-01 17:24 采纳率: 100%
浏览 110
已采纳

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

#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条回答 默认 最新

  • 源代码大师 博客专家认证 2021-05-03 16:48
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因