c++ greedy snake game (tested successfully)---with screenshot

During the holiday, I wrote a C++ greedy snake program (big guys are welcome to correct me)

We have all played Snake Eating, that is, the little snake eats food.
You need to download it before writing the code (download easyx link: https://easyx.cn/ .
Specific ideas:

  1. Define a coordinate structure (used to store the coordinates of food and snake structure). Of course, you must also understand what the
    coordinate axis is in the C++ program. I drew a simple coordinate axis Simple coordinates
    2 and used the loop to draw and clean up ( The meaning is to initialize the image, draw the initial coordinates of the snake and the coordinates of the food, and then continue to clean up and draw through the whil cycle-for example, the initial coordinates of the head of the snake is (150,160), move it to the right is (160,160) Then clean up the original coordinates-drawing the snake here is drawn with the easyx rectangle function or circl drawing is fine)
    3. When the coordinates of the head of the snake touch the coordinates of the food (equivalent to eating it-it is needed here) A little imagination) and then clean up the coordinates of the old food, and use the function to generate new coordinates (bool function judgment). For
    details, please refer to my code.
#include<iostream>
#include<graphics.h>
#include<conio.h>

//音乐头文件
#include<mmsystem.h>
#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;
}

Snake test c++
There may be no pictures when running, because it is local, and when I run this program locally, it flashes a bit, but it is not very serious. Please correct me (if you think I wrote well, please give me a thumbs up and encourage me as a rookie.) , C++ can leave a message if you want to communicate, you can follow each other)

Guess you like

Origin blog.csdn.net/Msyusheng/article/details/106126707