c++贪吃蛇小游戏(已经测试成功)---附截图

放假期间写了一个c++贪吃小蛇的程序(欢迎大佬们指正)

贪吃蛇我们都玩过,就是小蛇去吃到食物
在写代码之前需要下载(下载easyx链接: https://easyx.cn/.
具体思路:

  1. 定义一个坐标结构体(用来储存食物和蛇结构的坐标),当然了也要了解c++程序中的
    坐标轴是怎样的我我画了一个简易的坐标轴c++贪吃蛇小游戏(已经测试成功)---附截图_第1张图片
    2,用循环不断去绘画和清理(意思就初始化图像,画好初始小蛇的坐标和和食物的坐标,然后通过whil循环不断的清理和绘制–例如小蛇的头部初始的坐标是(150,160),向右移动一下就是(160,160)然后把原先的坐标清理掉–这里绘制蛇是用easyx的rectangle的函数绘制也可以用circl绘制都行)
    3,当小蛇头的坐标碰到食物的坐标的时候(相当于吃掉它–这里需要一点想象力)然后清理掉旧食物的坐标,并用函数产生新的坐标(bool函数判断)
    具体的可以参考我的代码
#include
#include
#include

//音乐头文件
#include
#pragma comment(lib,"winmm.lib")

using namespace std;
#define N 100 //定义snake的个数
#define size 10//每一格snake之间的距离

enum Ch{up = 72,down = 80,left1 = 75,right1 = 77};
double s = 0;
//定义结构体coor 食物和蛇的坐标
struct coor
{
	int x;
	int y;
};

 typedef struct Snake
{
	int n;//有n节
	int x[N];//snake节点的坐标.x
	int y[N];//snake节点的坐标.y
	struct coor szb[N];//100节snake的坐标
	Ch ch;//定义方向
}snake;

 snake snake1;



typedef struct FOod
{
	struct  coor fzb;//定义食物的坐标
	int ieat;//判断食物是否被吃掉
}Food;

Food food;
void snakemove()
{

	for (int i = snake1.n; i > 0; i--)
	{
		snake1.szb[i].y = snake1.szb[i - 1].y;
		snake1.szb[i].x = snake1.szb[i - 1].x;
	}
	switch (snake1.ch)
	{
	case up:
		snake1.szb[0].y -= size;
		break;
	case down:
		snake1.szb[0].y += size; 
		break;
	case left1:
		snake1.szb[0].x -= size; 
		break;
	case right1:
		snake1.szb[0].x += size; 
		break;
	default:
		break;
	}
}
void drowsnake()
{
	system("cls");
	cleardevice();
	for (int i = 0; i < snake1.n; i++)
	{
	setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255, ));
	fillrectangle(snake1.szb[i].x, snake1.szb[i].y, snake1.szb[i].x + size, snake1.szb[i].y + size);
	}
	fillrectangle(food.fzb.x, food.fzb.y, food.fzb.x +size, food.fzb.y + size);
	settextcolor(WHITE);
	outtextxy(650, 220, _T("贪吃小蛇"));
}

void direction()//方向
{
	char key;
	key = _getch();
	switch (key)
	{
	case up:
		if (snake1.ch != down)
		snake1.ch=up;
		break;
	case down:
		if (snake1.ch != up)
			snake1.ch = down;
		break;
	case left1:
		if (snake1.ch != right1)
		
			snake1.ch = left1;
		break;
	case right1:
		if (snake1.ch != left1)
		
			snake1.ch = right1;
		break;
	}
}

void creatfood()
{
	int flag = 0;
	while (1)
	{
		food.fzb.x = ((1+rand() % 63) * 10);
		food.fzb.y = ((1+rand() % 47) * 10);
		for (int i = 0; i < snake1.n; i++)
		{
			if (snake1.szb[i].x == food.fzb.x && snake1.szb[i].y == food.fzb.y)
			{
				flag = 1;
				break;
			}
		}
		if (flag)
		{
			continue;
		}else
		{
			food.ieat = false;
			break;
		}
	}
}
void initgame()//初始化游戏
{
	mciSendString(_T("open 1.mp3"), 0, 0, 0);
	mciSendString(_T("play 1.mp3"), 0, 0, 0);
	initgraph(800, 480);

	snake1.n = 3;//初始化节数
	snake1.ch = right1;
	snake1.szb[0].x = 300;
	snake1.szb[0].y = 100;
	snake1.szb[1].x = 290;
	snake1.szb[1].y = 100;
	snake1.szb[2].x = 280;
	snake1.szb[2].y = 100;

	food.ieat = true;//吃了食物
	
}
void eat_food()
{
	if (food.fzb.x == snake1.szb[0].x && food.fzb.y == snake1.szb[0].y)
	{
		snake1.n++;
		s += 10;
		food.ieat = true;
	}
}
int game_over()
{
	if (snake1.szb[0].x < 0 || snake1.szb[0].y < 0 || snake1.szb[0].x>620 || snake1.szb[0].y>460)
	{
		return 1;
	}

	for (int i = snake1.n - 1; i > 0; i--)
	{
		if (snake1.szb[0].x == snake1.szb[i].x && snake1.szb[0].y == snake1.szb[i].y)
		{
			return 1;
		}
	}
}

void loading_img()
{
	IMAGE img1;
	loadimage(&img1, _T("D:\\2.jpg"), 640, 480);
	putimage(0, 0, &img1,SRCERASE);
	fillrectangle(0, 0, 10, 480);
	fillrectangle(0, 0, 640, 10);
	fillrectangle(630, 10, 640, 480);
	fillrectangle(0, 470, 640, 480);
}
int main()
{
	initgame();//初始化游戏
	while (1)
	{

		if (food.ieat == true)
		{
			creatfood();
		}
		loading_img();
		snakemove();
		drowsnake();

		eat_food();
		if (game_over())
		{
			break;
		}
		Sleep(0);
		if (_kbhit())
		{
			direction();
		}
	}
	return 0;
}

c++贪吃蛇小游戏(已经测试成功)---附截图_第2张图片
运行的时候可能没有图片,因为它是本地的,然后我本地运行这个程序的时候有点一闪一闪的但不是很严重,请大哥指正(觉得我写的不错就点个赞鼓励我这个菜鸟吧,c++想交流的可以留言,互粉一下都可以啊)

你可能感兴趣的:(c++)