贪吃蛇(简单版)欢迎指正

#include<stdio.h>
#include<conio.h>
#include<windows.h>

#define high 40  //定义游戏界面大小
#define wide 50
char str[11] = { '0' };//变量 str[8]=i str[9]=j str[3]food_x  str[4]food_y  str[5] 长度控制
int element[high][wide] = { 0 };//0空格  -8食物  1蛇头 >1蛇身
int max, direct = 8, food = 4;
int bady_x=0, bady_y=0;
void gotoxy(int x, int y)//刷新屏幕
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
void hidecursor()//隐藏光标
{
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void eatfood()//刷新食物的位置
{
	str[4] = rand() % (high - 6) + 2;
	str[3] = rand() % (wide - 6) + 2;
	element[str[4]][str[3]] = -8;
}
void start()//初始化一些基本元素
{
	
	str[0] = high / 2;
	str[1] = wide / 2;
	str[5] = 1;
	str[6] = 0;
	str[7] = 0;
	for (str[8] = 0; str[8] < wide-1; str[8]++)
	{
		element[0][str[8]] = -6;
		element[high-1][str[8]] = -6;
	}
	for (str[9] = -1; str[9] < high; str[9]++)
	{
		element[str[9]][1] = -6;
		element[str[9]][wide-1] = -6;
		
	}
	element[str[0]][str[1]] = 1;
	element[str[0]+1][str[1]] = 2;
	element[str[0]+2][str[1]] = 3;
	eatfood();
}
void show()//显示图像
{
	gotoxy(1, 0);
	hidecursor();
	for (str[8] = 0; str[8] < high+5; str[8]++)//输出显示的图案
	{
		for (str[9] = 0; str[9] < wide; str[9]++)
		{
			if (element[str[8]][str[9]] > 1)
				printf("*");
			else if (element[str[8]][str[9]] == 1)
				printf("o");
			else if (element[str[8]][str[9]] == -6)
				printf("*");
			else if (element[str[8]][str[9]] == -8)
				printf("C");
			else if (element[str[8]][str[9]] == 0)
				printf(" ");
			

		}
		printf("\n");
	}
	Sleep(100);
}
void snakemove()//实现蛇的移动
{
	int head_x, head_y;
	for (str[8] = 0; str[8] < high - 1; str[8]++)
		for (str[9] = 0; str[9] < wide - 1; str[9]++)
			if (element[str[8]][str[9]] > 0)
				element[str[8]][str[9]]++;
			//蛇的移动的实现,是将蛇的所有元素都加一,然后将最大的元素变为空格,并依靠food来控制蛇的长度
	
	for (str[8] = 0; str[8] < high - 1; str[8]++)
		for (str[9] = 0; str[9] < wide - 1; str[9]++)
			if (element[str[8]][str[9]] > 0)
			{
				
				if (element[str[8]][str[9]]>=food)
				{
					bady_y= str[8];
					bady_x= str[9];
				}
				if (element[str[8]][str[9]] == 2)
				{
					head_y = str[8];
					head_x = str[9];
				}
				
			}
	element[bady_y][bady_x] = 0;
	int newhead_x, newhead_y;	
			if (direct == 8)//控制蛇移动的方向
			{
				newhead_y = head_y - 1;
				newhead_x = head_x;

			}
			if (direct == 4)
			{
				newhead_y = head_y;
				newhead_x = head_x - 1;
			}
			if (direct == 2)
			{
				newhead_y = head_y + 1;
				newhead_x = head_x;
			}
			if (direct == 6)
			{
				newhead_y = head_y;
				newhead_x = head_x + 1;
			}
			
			if (newhead_y == str[4] && newhead_x == str[3])
			{
				element[str[3]][str[4]] = 0;  // 产生一个新的食物
				str[3] = rand() % (high - 6) + 2;
				str[4] = rand() % (wide - 6) + 2;
				element[str[4]][str[3]] = -8;
				str[5] = 3;
				str[6]++;
				food++;
				if (direct == 8)
				{
					element[bady_y][bady_x] = str[5];
					str[5]++;
					
					
				}
				if (direct == 4)
				{
					element[bady_y][bady_x] = str[5];
					str[5]++;
					
				}
				if (direct == 2)
				{
					element[bady_y][bady_x] = str[5];
					str[5]++;
					
				}
				if (direct == 6)
				{
					element[bady_y][bady_x] = str[5];
					str[5]++;
					
				}

			}
			
			if (newhead_y < 1 || newhead_x < 1 || newhead_y >= high - 1 || newhead_x >= wide - 1 )//判断蛇是否碰撞
			{
				printf("游戏失败!\n");
				Sleep(2000);
				system("pause");
				exit(0);
			}
			if (element[newhead_y][newhead_x] > 2)
			{
				printf("游戏失败!\n");
				Sleep(2000);
				system("pause");
				exit(0);
			}
			else element[newhead_y][newhead_x] = 1;
}
void updatewithout()
{
	snakemove();
	
	
}
void updatewith()//控制蛇的运动,利用wasd来控制蛇的移动方向
{
	char input;
	if (_kbhit())
	{
		input = _getch();
		if (input == 'a'&&direct!=6)
		{
			direct = 4;
			snakemove();
		}
		if (input == 's'&&direct!=8)
		{
			direct = 2;
			snakemove();
		}
		if (input == 'd'&&direct!=4)
		{
			direct = 6;
			snakemove();
		}
		if (input == 'w'&&direct!=2)
		{
			direct = 8;
			snakemove();
		}
	}

}
int main()
{
	start();
	while (1)
	{
		show();
		updatewith();
		updatewithout();
	}
}

在这个程序中,蛇的长度我用食物来控制 ,与大多贪吃蛇都不同。

发布了29 篇原创文章 · 获赞 24 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/yyk219/article/details/79989269