天天看点

用c++控制台实现的贪吃蛇小游戏

最近暑假写了个贪吃蛇小游戏当作大作业

在这把代码贴出来存着以后常回来看看,毕竟是写的第一个小游戏嘛嘻嘻

主菜单

用c++控制台实现的贪吃蛇小游戏

“开始游戏”界面

用c++控制台实现的贪吃蛇小游戏

“游戏界面” 。 我觉得挺low哈哈哈,控制台没办法

用c++控制台实现的贪吃蛇小游戏
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
#include<deque>
#include <stdio.h>

using namespace std;
char k;
bool u = 0, dow = 0, lef = 0, righ = 1, game = 1;
int gamedelay = 300, flag = 0, food_vertify_flag = 0;
int score = 0, gamespeed = 1;
int game_mode_flag = 1;

class point
{
public:
	int x, y;
	int get_x() { return x; }
	int get_y() { return y; }
	point(int a, int b) { x = a; y = b; }
};

class map_size
{
public:
	map_size(int a, int b) 
	{
		x = a; y = b;
		boundary_up = 1;
		boundary_down = x-1;
		boundary_left = 1;
		boundary_right = y * 2 - 2;
	}
	int x, y;
	int boundary_up;
	int boundary_down;
	int boundary_left;
	int boundary_right;

};

map_size dm(15, 30);
map_size smap(15, 30);
map_size mmap(20, 40);
map_size bmap(30, 60);




HWND hwnd = GetForegroundWindow();
int main(int argc, char *argv[])
{

	ShowWindow(hwnd, SW_MAXIMIZE); 
	SetConsoleTitle("贪吃蛇");
	void delay_msec(int msec);
	void game_pause();
	void creat_map(map_size s);
	void snake_elongate_right(deque<point>& z, HANDLE& c, COORD& v, DWORD res);
	void snake_elongate_down(deque<point>& z, HANDLE& c, COORD& v, DWORD res);
	void snake_elongate_left(deque<point>& z, HANDLE& c, COORD& v, DWORD res);
	void snake_elongate_up(deque<point>& z, HANDLE& c, COORD& v, DWORD res);
	void setfood(deque<point>& sn, point& z, HANDLE& c, COORD& v, DWORD res);
	void initializefood(point z, HANDLE& c, COORD& v, DWORD res);
	void foodclear(point& z);
	void show_scores(int a, HANDLE c, COORD& v);
	void ifwall(deque<point> sn);
	void if_eat_itself(deque<point> sn);
	void cls(HANDLE c, int bbb);
	void set_position(COORD &c1, COORD c2);
	void hidecur();
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
	DWORD mode;
	GetConsoleMode(hIn, &mode);
	mode &= ~ENABLE_QUICK_EDIT_MODE; //移除快速编辑模式  让win10控制台能响应鼠标操作而不阻塞进程
	mode &= ~ENABLE_INSERT_MODE; //移除插入模式  同上
	SetConsoleMode(hIn, mode);

	COORD pos = { 0,0 }, SNAKE__POS = { 0,0 };
	INPUT_RECORD inRec;
	DWORD numRead;
	INPUT_RECORD	mouseRec;
	DWORD			res;
	COORD		 mpos, choose_difficulty, choose_size, choose_mode;
	COORD gouzi = { 94,22 };
	COORD gouzi1 = { 94,28 };
	COORD gouzi2 = { 91,33 };

	struct the_SNAKE_pos
	{
		short int x;
		short int y;
	}SNAKE_POS[116] = {   //游戏界面大字 “SNAKE”  的坐标

		{61,9},{62,9},{63,9},{64,9},{65,9},{60,10},
		{60,14},{60,11},{66,10},{66,13},{66,14},
		{61,12},{62,12},{63,12},{64,12},{65,12},
		{61,15},{62,15},{63,15},{64,15},{65,15},

		{73,9},{73,10},{73,11},{73,12},{73,13},{73,14},{73,15},
		{83,9},{83,10},{83,11},{83,12},{83,13},{83,14},{83,15},
		{74,9},{75,10},{76,10},{76,11},{77,11},{78,12},{79,12},
		{79,13},{80,13},{80,14},{81,14},{82,15},

		{90,15},{91,14},{92,13},{93,12},{94,11},{95,10},
		{96,9},{97,10},{98,11},{99,12},{100,13},{101,14},
		{102,15},{94,13},{96,13},{98,13},

		{108,9},{108,10},{108,11},{108,12},{108,13},{108,14},{108,15},
		{109,9},{109,10},{109,11},{109,12},{109,13},{109,14},{109,15},
		{111,12},{112,11},{112,12},{112,13},{113,11},{113,13},{114,10},
		{114,14},{115,9},{115,10},{115,14},{115,15},{116,9},{116,15},

		{123,9},{124,9},{125,9},{126,9},{127,9},{128,9},{129,9},
		{123,15},{124,15},{125,15},{126,15},{127,15},{128,15},
		{123,10},{123,11},{123,13},{123,14},{130,15},{130,9},
		{123,12},{124,12},{125,12},{126,12},{127,12},{129,15},

	};


zhucaidan:
	hidecur();   //这句话很有用!能隐藏讨厌的光标
	for (size_t i = 0; i <116; i++)
	{
		SNAKE__POS.X = SNAKE_POS[i].x;
		SNAKE__POS.Y = SNAKE_POS[i].y;
		FillConsoleOutputCharacter(hOut, 'A', 1, SNAKE__POS, &res);
	}

	COORD therightpos = { 90,21 }, therightpos1 = { 90,24 }, therightpos2 = { 90,27 };
	COORD nandu1 = { 94,22 }, nandu2 = { 94,23 }, nandu3 = { 94,24 }, nandu4 = { 94,25 };
	COORD qingxuanzedituchicun = { 88,27 };
	COORD qingxuanzemoshi{ 90,32 };
	COORD themapsizepos = { 94,28 }, themapsizepos1 = { 94,29 }, themapsizepos2 = { 94,30 }; 
	COORD mode1{ 91,33 }, mode2{ 91,34 }; 
	COORD kaishi{ 93,36 };
	SetConsoleCursorPosition(hOut, therightpos);
	cout << "开始游戏"; 
	COORD trpos1 = { 90,24 }; SetConsoleCursorPosition(hOut, trpos1);
	cout << "游戏说明";
	COORD trpos2 = { 90,27 }; SetConsoleCursorPosition(hOut, trpos2);
	cout << "退出游戏";
	COORD retn = { 90,30 }, shuoming1 = { 78,21 }, shuoming2 = { 78,22 };
	

	
	while (1)//主菜单:开始游戏、游戏说明、退出游戏
	{
		
		ReadConsoleInput(hIn, &mouseRec, 1, &res);

		if (mouseRec.EventType == MOUSE_EVENT)
		{
			if (mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
			{
				mpos = mouseRec.Event.MouseEvent.dwMousePosition;
				if (mpos.X > therightpos.X-1 && mpos.X < therightpos.X+8 && mpos.Y==21)//开始游戏
				{
					cls(hOut, 10); SetConsoleCursorPosition(hOut, therightpos); 
					cout << "请选择速度" ;
					SetConsoleCursorPosition(hOut, nandu1);
					cout << "①";
					SetConsoleCursorPosition(hOut, nandu2);
					cout << "②";
					SetConsoleCursorPosition(hOut, nandu3);
					cout << "③";
					SetConsoleCursorPosition(hOut, nandu4);
					cout << "④";
					SetConsoleCursorPosition(hOut, qingxuanzedituchicun);
					cout << "请选择地图大小";
					SetConsoleCursorPosition(hOut, themapsizepos);
					cout << "小";
					SetConsoleCursorPosition(hOut, themapsizepos1);
					cout << "中";
					SetConsoleCursorPosition(hOut, themapsizepos2);
					cout << "大";
					SetConsoleCursorPosition(hOut, kaishi);
					cout << "开始";
					SetConsoleCursorPosition(hOut, qingxuanzemoshi);
					cout << "请选择模式";
					SetConsoleCursorPosition(hOut, mode1);
					cout << "经典模式";
					SetConsoleCursorPosition(hOut, mode2);
					cout << "无尽模式";
					set_position(gouzi, nandu1); SetConsoleCursorPosition(hOut, gouzi);
					cout << "√";
					set_position(gouzi1, themapsizepos); SetConsoleCursorPosition(hOut, gouzi1);
					cout << "√";
					set_position(gouzi2, mode1); SetConsoleCursorPosition(hOut, gouzi2);
					cout << "√";
					while (1) //选择难度
					{
						ReadConsoleInput(hIn, &mouseRec, 1, &res);
						
						if (mouseRec.EventType == MOUSE_EVENT)
						{
							if (mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
							{
								mpos = mouseRec.Event.MouseEvent.dwMousePosition;
								
								 if (mpos.X >93 && mpos.X < 96 && mpos.Y == 22) 
								 {
									 gamedelay = 300;  
									 SetConsoleCursorPosition(hOut, gouzi); 
									 cout << "  "; 
									 set_position(gouzi, nandu1);
									 SetConsoleCursorPosition(hOut, gouzi);
									 cout << "√";
								}
								 if (mpos.X > 93 && mpos.X < 96 && mpos.Y == 23)
								 {
									 gamedelay = 150; 
									 SetConsoleCursorPosition(hOut, gouzi);
									 cout << "  "; 
									 set_position(gouzi, nandu2);
									 SetConsoleCursorPosition(hOut, gouzi);
									 cout << "√"; 
								}
								 if (mpos.X > 93 && mpos.X < 96 && mpos.Y == 24) 
								 {
									 gamedelay = 75; 
									 SetConsoleCursorPosition(hOut, gouzi);
									 cout << "  ";
									 set_position(gouzi, nandu3);
									 SetConsoleCursorPosition(hOut, gouzi);
									 cout << "√";
								}
								 if (mpos.X > 93 && mpos.X < 96 && mpos.Y == 25) 
								 {
									 gamedelay = 37; 
									 SetConsoleCursorPosition(hOut, gouzi);
									 cout << "  ";
									 set_position(gouzi, nandu4);
									 SetConsoleCursorPosition(hOut, gouzi);
									 cout << "√";
								}
								 if (mpos.X > 93 && mpos.X < 96 && mpos.Y == 28) 
								 {
									 dm.x = 15; dm.y = 30;
									 dm.boundary_up = 1;
									 dm.boundary_down = dm.x - 1;
									 dm.boundary_left = 1;
									 dm.boundary_right = dm.y * 2 - 2;
									 SetConsoleCursorPosition(hOut, gouzi1);
									 cout << "  ";
									 set_position(gouzi1, themapsizepos);
									 SetConsoleCursorPosition(hOut, gouzi1);
									 cout << "√";
								 }
								 if (mpos.X > 93 && mpos.X < 96 && mpos.Y == 29)
								 {
									 dm.x = 20; dm.y = 40;
									 dm.boundary_up = 1;
									 dm.boundary_down = dm.x - 1;
									 dm.boundary_left = 1;
									 dm.boundary_right = dm.y * 2 - 2;
									 SetConsoleCursorPosition(hOut, gouzi1);
									 cout << "  ";
									 set_position(gouzi1, themapsizepos1);
									 SetConsoleCursorPosition(hOut, gouzi1);
									 cout << "√";
								 }
								 if (mpos.X > 93 && mpos.X < 96 && mpos.Y == 30) 
								 {
									 dm.x = 30; dm.y = 60;
									 dm.boundary_up = 1;
									 dm.boundary_down = dm.x - 1;
									 dm.boundary_left = 1;
									 dm.boundary_right = dm.y * 2 - 2;
									 SetConsoleCursorPosition(hOut, gouzi1);
									 cout << "  ";
									 set_position(gouzi1, themapsizepos2);
									 SetConsoleCursorPosition(hOut, gouzi1);
									 cout << "√";
								 }
								 if (mpos.X > 91 && mpos.X < 100 && mpos.Y == 33)
								 {
									 game_mode_flag = 1;
									 SetConsoleCursorPosition(hOut, gouzi2);
									 cout << "  ";
									 set_position(gouzi2, mode1);
									 SetConsoleCursorPosition(hOut, gouzi2);
									 cout << "√";
								 }
								 if (mpos.X > 91 && mpos.X < 100 && mpos.Y == 34) 
								 {
									 game_mode_flag = 2;
									 SetConsoleCursorPosition(hOut, gouzi2);
									 cout << "  ";
									 set_position(gouzi2, mode2);
									 SetConsoleCursorPosition(hOut, gouzi2);
									 cout << "√";
								 }
								 if (mpos.X > 92 && mpos.X < 97 && mpos.Y == 36) 
								 {
									 system("cls"); goto begingame;
								 }
							}
						}
					}
				}
				if (mpos.X > therightpos1.X-1 && mpos.X < therightpos1.X+8 && mpos.Y == 24)//游戏说明
				{
					cls(hOut, 10);
					 SetConsoleCursorPosition(hOut, shuoming1);
					    cout<<"通过w、s、a、d 控制蛇进行上"; 
					SetConsoleCursorPosition(hOut, shuoming2);
						cout<<"下左右移动操作;按空格键暂停";
					SetConsoleCursorPosition(hOut, retn); cout << "返回"; 				
					while (1)
					{	
						ReadConsoleInput(hIn, &mouseRec, 1, &res); 
						if (mouseRec.EventType == MOUSE_EVENT)
						{
							if (mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
							{	
								mpos = mouseRec.Event.MouseEvent.dwMousePosition; 
								if (mpos.X > retn.X-1 && mpos.X < retn.X+4 && mpos.Y == 30) {
									cls(hOut, 10);goto zhucaidan;
								}
							}
						}
					}
				}
				if (mpos.X > therightpos2.X-1 && mpos.X < therightpos2.X+8 && mpos.Y == 27)//退出游戏
				{
					return 0;
				}
			}

		}
	}
	




	//游戏主体实现
	system("cls");
	system("color fc");//改变游戏时的背景颜色
	
	for (size_t i = 0; i < dm.x; i++)//创造地图
	{
		if (i==0)
		{
			cout << " 按空格键 暂停/继续 游戏,按Esc退出游戏。" << "\n"; continue;

		}
		for (size_t j = 0; j < dm.y; j++)
		{
			if (i == 1 || j == 0 || i == dm.x-1 || j == dm.y-1)
				cout << "▅";
			else cout << "  ";
		}
		cout << "\n";
	}

	point food(5, 5); initializefood(food, hOut, pos, NULL);
	point i1(10, 10), i2(11, 10), i3(12, 10), i4(13, 10), i5(14, 10);
	deque<point> snake;   //用deque双端序列容器贮存蛇身坐标,方便在头部和尾部删减
	snake.push_back(i1); snake.push_back(i2); snake.push_back(i3); snake.push_back(i4); snake.push_back(i5); //一开始蛇身有五个元素
	for (size_t i = 0; i < 5; i++) //打印初始蛇身
	{
		pos.X = snake[i].get_x();
		pos.Y = snake[i].get_y(); FillConsoleOutputCharacter(hOut, '*', 1, pos, &res);

	}

	if (game_mode_flag == 1)   //游戏模式为经典模式
	{
		while (game)
		{
			while (righ)//right
			{
				point i6((snake.at(snake.size() - 1)).get_x() + 1, (snake.at(snake.size() - 1)).get_y()); snake.push_back(i6);
				pos.X = (snake.at(snake.size() - 1)).get_x();
				pos.Y = (snake.at(snake.size() - 1)).get_y();
				FillConsoleOutputCharacter(hOut, '*', 1, pos, &res);  /*这个函数用SetConsoleCursorPosition(hOut,pos);
																 cout<<"下左右移动操作;按空格键暂停";  代替也可以*/
				pos.X = (*(snake.begin())).get_x();
				pos.Y = (*(snake.begin())).get_y();
				FillConsoleOutputCharacter(hOut, ' ', 1, pos, &res);
				snake.pop_front();

				if_eat_itself(snake);  //傻蛇咬到自己
				ifwall(snake);   //傻蛇撞墙
				if (game == 0) break;  //如果game==0,游戏结束  return 0;

				if (flag == 1)  //食物被吃掉
				{
					setfood(snake, food, hOut, pos, res);
					flag = 0;
				}
				if ((snake.at(snake.size() - 1)).get_x() == food.x && (snake.at(snake.size() - 1)).get_y() == food.y)  //如果蛇头吃到食物
				{
					snake_elongate_right(snake, hOut, pos, res); foodclear(food);
					flag = 1; score++; show_scores(score, hOut, pos);
				}
				delay_msec(gamedelay);  //延时一段时间,实现视觉上的移动效果
				if (_kbhit())  //监测键盘输入  
				{
					k = _getch(); game_pause(); //这个自定义函数监测是否按下空格键暂停,再次按下继续游戏,定义在下面
					if (k == 's')
					{
						righ = 0; dow = 1; break;
					}
					if (k == 'w')
					{
						righ = 0; u = 1; break;
					}
					if ((int)k == 27)  //是否按了Esc健
					{
						return 0;
					}
				}

			}
			while (dow) //down
			{
				point i6((snake.at(snake.size() - 1)).get_x(), (snake.at(snake.size() - 1)).get_y() + 1); snake.push_back(i6);
				pos.X = (snake.at(snake.size() - 1)).get_x();
				pos.Y = (snake.at(snake.size() - 1)).get_y();
				FillConsoleOutputCharacter(hOut, '*', 1, pos, &res);
				pos.X = (*(snake.begin())).get_x();
				pos.Y = (*(snake.begin())).get_y();
				FillConsoleOutputCharacter(hOut, ' ', 1, pos, &res);
				snake.pop_front();
				if_eat_itself(snake);
				ifwall(snake);
				if (game == 0) break;

				if (flag == 1)
				{
					setfood(snake, food, hOut, pos, res); flag = 0;
				}
				if ((snake.at(snake.size() - 1)).get_x() == food.x && (snake.at(snake.size() - 1)).get_y() == food.y)
				{
					snake_elongate_down(snake, hOut, pos, res); foodclear(food);
					flag = 1; score++; show_scores(score, hOut, pos);
				}
				delay_msec(gamedelay);
				if (_kbhit())
				{
					k = _getch(); game_pause();
					if (k == 'a')
					{
						dow = 0; lef = 1; break;
					}
					if (k == 'd')
					{
						dow = 0; righ = 1;  break;
					}
					if ((int)k == 27)
					{
						return 0;
					}
				}

			}
			while (lef) //left
			{
				point i6((snake.at(snake.size() - 1)).get_x() - 1, (snake.at(snake.size() - 1)).get_y()); snake.push_back(i6);
				pos.X = (snake.at(snake.size() - 1)).get_x();
				pos.Y = (snake.at(snake.size() - 1)).get_y();
				FillConsoleOutputCharacter(hOut, '*', 1, pos, &res);
				pos.X = (*(snake.begin())).get_x();
				pos.Y = (*(snake.begin())).get_y();
				FillConsoleOutputCharacter(hOut, ' ', 1, pos, &res);
				snake.pop_front();
				if_eat_itself(snake);
				ifwall(snake);
				if (game == 0) break;

				if (flag == 1)
				{
					setfood(snake, food, hOut, pos, res); flag = 0;
				}
				if ((snake.at(snake.size() - 1)).get_x() == food.x && (snake.at(snake.size() - 1)).get_y() == food.y)
				{
					snake_elongate_left(snake, hOut, pos, res); foodclear(food);
					flag = 1; score++; show_scores(score, hOut, pos);
				}
				delay_msec(gamedelay);
				if (_kbhit())
				{
					k = _getch(); game_pause();
					if (k == 'w')
					{
						lef = 0; u = 1; break;
					}
					if (k == 's')
					{
						lef = 0; dow = 1;  break;
					}
					if ((int)k == 27)
					{
						return 0;
					}
				}

			}
			while (u) //up
			{
				point i6((snake.at(snake.size() - 1)).get_x(), (snake.at(snake.size() - 1)).get_y() - 1); snake.push_back(i6);
				pos.X = (snake.at(snake.size() - 1)).get_x();
				pos.Y = (snake.at(snake.size() - 1)).get_y();
				FillConsoleOutputCharacter(hOut, '*', 1, pos, &res);
				pos.X = (*(snake.begin())).get_x();
				pos.Y = (*(snake.begin())).get_y();
				FillConsoleOutputCharacter(hOut, ' ', 1, pos, &res);
				snake.pop_front();
				if_eat_itself(snake);
				ifwall(snake);
				if (game == 0) break;

				if (flag == 1)
				{
					setfood(snake, food, hOut, pos, res); flag = 0;
				}
				if ((snake.at(snake.size() - 1)).get_x() == food.x && (snake.at(snake.size() - 1)).get_y() == food.y)
				{
					snake_elongate_up(snake, hOut, pos, res); foodclear(food);
					flag = 1; score++; show_scores(score, hOut, pos);
				}
				delay_msec(gamedelay);
				if (_kbhit())
				{
					k = _getch(); game_pause();
					if (k == 'a')
					{
						u = 0; lef = 1; break;
					}
					if (k == 'd')
					{
						u = 0; righ = 1;  break;
					}
					if ((int)k == 27)
					{
						return 0;
					}
				}

			}



		}
	}

	if (game_mode_flag == 2)
	{
		while (game)
		{
			while (righ)//right
			{				
				
				if ((snake.at(snake.size() - 1)).get_x() + 1==dm.boundary_right)
				{
					point i6(dm.boundary_left + 1, (snake.at(snake.size() - 1)).get_y());
					snake.push_back(i6);
				}
				else 
				{	
					point i6((snake.at(snake.size() - 1)).get_x() + 1, (snake.at(snake.size() - 1)).get_y()); 
					snake.push_back(i6); 
				}

				pos.X = (snake.at(snake.size() - 1)).get_x();
				pos.Y = (snake.at(snake.size() - 1)).get_y();
				FillConsoleOutputCharacter(hOut, '*', 1, pos, &res);
				pos.X = (*(snake.begin())).get_x();
				pos.Y = (*(snake.begin())).get_y();
				FillConsoleOutputCharacter(hOut, ' ', 1, pos, &res);
				snake.pop_front();

				if_eat_itself(snake);
				
				if (game == 0) break;

				if (flag == 1)
				{
					setfood(snake, food, hOut, pos, res);
					flag = 0;
				}
				if ((snake.at(snake.size() - 1)).get_x() == food.x && (snake.at(snake.size() - 1)).get_y() == food.y)
				{
					snake_elongate_right(snake, hOut, pos, res); foodclear(food);
					flag = 1; score++; show_scores(score, hOut, pos);
				}
				delay_msec(gamedelay);
				if (_kbhit())
				{
					k = _getch(); game_pause();
					if (k == 's')
					{
						righ = 0; dow = 1; break;
					}
					if (k == 'w')
					{
						righ = 0; u = 1; break;
					}
					if ((int)k == 27)
					{
						return 0;
					}
				}

			}
			while (dow) //down
			{
				if ((snake.at(snake.size() - 1)).get_y() + 1 == dm.boundary_down)
				{
					point i6((snake.at(snake.size() - 1)).get_x(), dm.boundary_up + 1);
					snake.push_back(i6);
				}
				else
				{
					point i6((snake.at(snake.size() - 1)).get_x(), (snake.at(snake.size() - 1)).get_y() + 1); 
					snake.push_back(i6);
				}
		
				pos.X = (snake.at(snake.size() - 1)).get_x();
				pos.Y = (snake.at(snake.size() - 1)).get_y();
				FillConsoleOutputCharacter(hOut, '*', 1, pos, &res);
				pos.X = (*(snake.begin())).get_x();
				pos.Y = (*(snake.begin())).get_y();
				FillConsoleOutputCharacter(hOut, ' ', 1, pos, &res);
				snake.pop_front();
				if_eat_itself(snake);
				
				if (game == 0) break;

				if (flag == 1)
				{
					setfood(snake, food, hOut, pos, res); flag = 0;
				}
				if ((snake.at(snake.size() - 1)).get_x() == food.x && (snake.at(snake.size() - 1)).get_y() == food.y)
				{
					snake_elongate_down(snake, hOut, pos, res); foodclear(food);
					flag = 1; score++; show_scores(score, hOut, pos);
				}
				delay_msec(gamedelay);
				if (_kbhit())
				{
					k = _getch(); game_pause();
					if (k == 'a')
					{
						dow = 0; lef = 1; break;
					}
					if (k == 'd')
					{
						dow = 0; righ = 1;  break;
					}
					if ((int)k == 27)
					{
						return 0;
					}
				}

			}
			while (lef) //left
			{
				if ((snake.at(snake.size() - 1)).get_x() - 1 == dm.boundary_left)
				{
					point i6(dm.boundary_right-1, (snake.at(snake.size() - 1)).get_y());
					snake.push_back(i6);
				}
				else
				{
					point i6((snake.at(snake.size() - 1)).get_x() - 1, (snake.at(snake.size() - 1)).get_y()); 
					snake.push_back(i6);
				}
				
				pos.X = (snake.at(snake.size() - 1)).get_x();
				pos.Y = (snake.at(snake.size() - 1)).get_y();
				FillConsoleOutputCharacter(hOut, '*', 1, pos, &res);
				pos.X = (*(snake.begin())).get_x();
				pos.Y = (*(snake.begin())).get_y();
				FillConsoleOutputCharacter(hOut, ' ', 1, pos, &res);
				snake.pop_front();
				if_eat_itself(snake);
				
				if (game == 0) break;

				if (flag == 1)
				{
					setfood(snake, food, hOut, pos, res); flag = 0;
				}
				if ((snake.at(snake.size() - 1)).get_x() == food.x && (snake.at(snake.size() - 1)).get_y() == food.y)
				{
					snake_elongate_left(snake, hOut, pos, res); foodclear(food);
					flag = 1; score++; show_scores(score, hOut, pos);
				}
				delay_msec(gamedelay);
				if (_kbhit())
				{
					k = _getch(); game_pause();
					if (k == 'w')
					{
						lef = 0; u = 1; break;
					}
					if (k == 's')
					{
						lef = 0; dow = 1;  break;
					}
					if ((int)k == 27)
					{
						return 0;
					}
				}

			}
			while (u) //up
			{
				if ((snake.at(snake.size() - 1)).get_y() - 1 == dm.boundary_up)
				{
					point i6((snake.at(snake.size() - 1)).get_x(), dm.boundary_down - 1);
					snake.push_back(i6);
				}
				else
				{
					point i6((snake.at(snake.size() - 1)).get_x(), (snake.at(snake.size() - 1)).get_y() - 1); 
					snake.push_back(i6);
				}

				
				pos.X = (snake.at(snake.size() - 1)).get_x();
				pos.Y = (snake.at(snake.size() - 1)).get_y();
				FillConsoleOutputCharacter(hOut, '*', 1, pos, &res);
				pos.X = (*(snake.begin())).get_x();
				pos.Y = (*(snake.begin())).get_y();
				FillConsoleOutputCharacter(hOut, ' ', 1, pos, &res);
				snake.pop_front();
				if_eat_itself(snake);
				
				if (game == 0) break;

				if (flag == 1)
				{
					setfood(snake, food, hOut, pos, res); flag = 0;
				}
				if ((snake.at(snake.size() - 1)).get_x() == food.x && (snake.at(snake.size() - 1)).get_y() == food.y)
				{
					snake_elongate_up(snake, hOut, pos, res); foodclear(food);
					flag = 1; score++; show_scores(score, hOut, pos);
				}
				delay_msec(gamedelay);
				if (_kbhit())
				{
					k = _getch(); game_pause();
					if (k == 'a')
					{
						u = 0; lef = 1; break;
					}
					if (k == 'd')
					{
						u = 0; righ = 1;  break;
					}
					if ((int)k == 27)
					{
						return 0;
					}
				}

			}



		}
	}




	system("cls");
	pos.X = 25; pos.Y = 25;
	SetConsoleCursorPosition(hOut, pos);
	cout << "your score is : " << score << "\n";
	pos.X = 26; pos.Y = 26;
	SetConsoleCursorPosition(hOut, pos);
	cout << "gameover";

	system("pause");
	return 0;
}


void delay_msec(int msec)  //自定义的延时函数
{
	clock_t now = clock();
	while (clock() - now < msec) {}

}

void snake_elongate_right(deque<point>& z, HANDLE& c, COORD& v, DWORD res)
{
	point i7((z.at(z.size() - 1)).get_x() + 1, (z.at(z.size() - 1)).get_y()); z.push_back(i7);
	v.X = (z.at(z.size() - 1)).get_x();
	v.Y = (z.at(z.size() - 1)).get_y();
	FillConsoleOutputCharacter(c, '*', 1, v, &res);

}

void snake_elongate_down(deque<point>& z, HANDLE& c, COORD& v, DWORD res)
{
	point i7((z.at(z.size() - 1)).get_x(), (z.at(z.size() - 1)).get_y() + 1); z.push_back(i7);
	v.X = (z.at(z.size() - 1)).get_x();
	v.Y = (z.at(z.size() - 1)).get_y();
	FillConsoleOutputCharacter(c, '*', 1, v, &res);

}

void snake_elongate_left(deque<point>& z, HANDLE& c, COORD& v, DWORD res)
{
	point i7((z.at(z.size() - 1)).get_x() - 1, (z.at(z.size() - 1)).get_y()); z.push_back(i7);
	v.X = (z.at(z.size() - 1)).get_x();
	v.Y = (z.at(z.size() - 1)).get_y();
	FillConsoleOutputCharacter(c, '*', 1, v, &res);

}

void snake_elongate_up(deque<point>& z, HANDLE& c, COORD& v, DWORD res)
{
	point i7((z.at(z.size() - 1)).get_x(), (z.at(z.size() - 1)).get_y() - 1); z.push_back(i7);
	v.X = (z.at(z.size() - 1)).get_x();
	v.Y = (z.at(z.size() - 1)).get_y();
	FillConsoleOutputCharacter(c, '*', 1, v, &res);

}


void initializefood(point z, HANDLE& c, COORD& v, DWORD res)
{
	v.X = z.get_x(); v.Y = z.get_y();
	SetConsoleCursorPosition(c, v);
	FillConsoleOutputCharacter(c, '$', 1, v, &res);

}

void setfood(deque<point>& sn, point& z, HANDLE& c, COORD& v, DWORD res)//放置食物
{
	z.x = rand() % (dm.y * 2 - 4) + 2; delay_msec(30); z.y = rand() % (dm.x - 3) + 2;

	for (size_t i = 0; i < sn.size(); i++)
	{
		if (z.x == sn[i].x&&z.y == sn[i].y)
		{
			food_vertify_flag = 1; break;//食物在蛇身上生成
		}
	}

	while (food_vertify_flag == 1)
	{
		z.x = rand() % (dm.y * 2 - 4)+ 2; delay_msec(30); z.y = rand() % (dm.x-3) + 2;
		for (size_t i = 0; i < sn.size(); i++)
		{
			if (z.x == sn[i].x&&z.y == sn[i].y)
			{
				food_vertify_flag = 1; break;//食物在蛇身上生成
			}
			else food_vertify_flag = 0;
		}

	}

	v.X = z.x; v.Y = z.y;
	FillConsoleOutputCharacter(c, '$', 1, v, &res);
}

void foodclear(point& z)//食物被吃后清除其位置信息
{
	z.x = -1; z.y = -1;
}

void show_scores(int a, HANDLE c, COORD& v)
{
	v.X = 50; v.Y = 0;
	SetConsoleCursorPosition(c, v);
	cout << "score:  " << a ;
}

void ifwall(deque<point> sn) //是否撞墙
{
	if (sn.at(sn.size() - 1).x == dm.boundary_left || sn.at(sn.size() - 1).x == dm.boundary_right || sn.at(sn.size() - 1).y == dm.boundary_up || sn.at(sn.size() - 1).y == dm.boundary_down)
	{
		game = 0;
	}

}

void if_eat_itself(deque<point> sn) //是否咬到自己
{
	for (size_t i = 0; i < sn.size() - 2; i++)
	{
		if (sn[i].x == sn.at(sn.size() - 1).x&&sn[i].y == sn.at(sn.size() - 1).y)
		{
			game = 0; break;
		}
	}

}

void cls(HANDLE c, int clear_row_num)
{
	for (size_t i = 0; i < clear_row_num; i++)
	{
		COORD start_clear_pos = { 70,21+i } ;
		SetConsoleCursorPosition(c, start_clear_pos);
		cout << "                                                                                 ";
	}
}

void game_pause()
{
	if ((int)k == 32)
	{
		while (1) 
		{ if (_kbhit()) 
			{ k = _getch(); 
				if ((int)k == 32) break; 	
			} 
			Sleep(20); 
		}
	}
	
}


void creat_map(map_size s)
{
	for (size_t i = 0; i < s.y; i++)//创造地图
	{
		if (i == 0)
		{
			cout << " 按空格键 暂停/继续 游戏,按Esc退出游戏。" << "\n"; continue;

		}
		for (size_t j = 0; j < s.x; j++)
		{
			if (i == 1 || j == 0 || i == s.y-1 || j == s.x-1)
				cout << "▅";
			else cout << "  ";
		}
		cout << "\n";
	}

}


void set_position(COORD &c1, COORD c2)
{
	c1.X = c2.X - 4;
	c1.Y = c2.Y;

}

void hidecur()
{
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cci;
	GetConsoleCursorInfo(hOut, &cci);
	cci.bVisible = false;
	SetConsoleCursorInfo(hOut, &cci);
}
           

代码量有点多,不过流畅性还行

继续阅读