基本信息
源码名称:c++ 贪吃蛇 小游戏源码(支持难度级别)
源码大小:6.89KB
文件格式:.cpp
开发语言:C/C++
更新时间:2019-08-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


#include<iostream>
#include <conio.h>
#include<windows.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include <cstdlib>
#include <time.h>
#include <conio.h>
#include <queue>
using namespace std;

void color(int col)
{
	HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE));
	SetConsoleTextAttribute(hConsole, col);
}
inline void shuchu(int y, int x, int p)
{
	HANDLE hOut;
	COORD pos = { 0, 0 };
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	pos.X = x   x;
	pos.Y = y;
	SetConsoleCursorPosition(hOut, pos);
	if (p == '□')
		printf("  ");
	else if (p == '■')
		printf("■");
	else if (p == 2)
	{
		color(8);
		printf("●");
		color(7);
	}
	else if (p == 3)
	{
		color(4);
		printf("●");
		color(7);
	}
}
void chushihua(int map[][39])
{
	int i;
	for (i = 0; i < 39; i  )
	{
		map[0][i] = map[i][0] = map[38][i] = map[i][38] = 0x7fffffff;
	}
	int j;
	for (i = 1; i < 38; i  )
		for (j = i; j < 38; j  )
			map[i][j] = map[j][i] = 0;

}
void xinxi(int y, int x, const char* p)
{
	HANDLE hOut;
	COORD pos = { 0, 0 };
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hOut, pos);
	printf("%s", p);
	pos.X = 0;
	pos.Y = 0;
}
void xinxi2(int y, int x, int p)
{
	HANDLE hOut;
	COORD pos = { 0, 0 };
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hOut, pos);
	printf("%d  ", p);
	pos.X = 0;
	pos.Y = 0;
}
void showkuangjia()
{
	int i;
	HANDLE hOut;
	COORD pos = { 0, 0 };
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	for (i = 0; i < 39; i  )
		printf("★");

	for (int j = 1; j < 38; j  )
	{
		pos.X = 0;
		pos.Y = j;
		SetConsoleCursorPosition(hOut, pos);
		printf("★");
		pos.X = 76;
		pos.Y = j;
		SetConsoleCursorPosition(hOut, pos);
		printf("★");
	}
	pos.X = 0;
	pos.Y = 38;
	SetConsoleCursorPosition(hOut, pos);
	for (i = 0; i < 39; i  )
		printf("★");
	xinxi(5, 83, "采蘑菇的白娘子");
	xinxi(7, 86, "得分");
	xinxi(8, 87, "0      ");
	xinxi(10, 83, "BY 青春微凉");
}

void bimu()
{
	int i, j;
	HANDLE hOut;
	COORD pos = { 0, 0 };
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	pos.X = 2;
	for (i = 1; i < 38; i  )
	{
		pos.Y = i;
		SetConsoleCursorPosition(hOut, pos);
		for (j = 1; j < 38; j  )
		{
			printf("■");
			Sleep(1);
		}
	}
}

typedef struct
{
	int x, y;
}node;

void kaimu(int map[][39])
{
	queue<node>que;
	int i, j;
	int dirx[] = { 0,1,1,1,0,-1,-1,-1 };
	int diry[] = { -1,-1,0,1,1,1,0,-1 };
	map[19][19] = 1;
	node now, next;
	now.x = 19;
	now.y = 19;
	que.push(now);
	while (!que.empty())
	{
		now = que.front();
		que.pop();
		shuchu(now.y, now.x, '□');
		Sleep(1);
		map[now.y][now.x] = 1;
		for (i = 0; i < 8; i  )
		{
			next.x = now.x   dirx[i];
			next.y = now.y   diry[i];
			if (map[next.y][next.x] == 0)
			{
				map[next.y][next.x] = 1;
				que.push(next);
			}
		}
	}
}
void snakelocal(int* y, int* x, int* D, int map[][39])
{
	srand(time(0));
	*y = rand() % 25   5;
	*x = rand() % 25   5;
	*D = rand() % 4;
	map[*y][*x] = 100;
	shuchu(*y, *x, '■');
	if (*D == 2)
	{
		map[*y - 1][*x] = 101;
		shuchu(*y - 1, *x, '■');
	}
	else if (*D == 3)
	{
		map[*y][*x   1] = 101;
		shuchu(*y, *x   1, '■');
	}
	else if (*D == 0)
	{
		map[*y   1][*x] = 101;
		shuchu(*y   1, *x, '■');
	}
	else
	{
		map[*y][*x - 1] = 101;
		shuchu(*y, *x - 1, '■');
	}
}
inline void yidong(int y, int x)
{
	shuchu(y, x, '■');
}
void quwei(int map[][39], int y, int x)
{
	shuchu(y, x, '□');
	map[y][x] = 1;
}
void DFS(int map[][39], int y, int x, int num)
{
	int dirx[] = { 0,1,0,-1 };
	int diry[] = { -1,0,1,0 };
	int i;
	for (i = 0; i < 4; i  )
	{
		if (map[y   diry[i]][x   dirx[i]] == num)
		{
			map[y][x] = num;
			y  = diry[i];
			x  = dirx[i];
			DFS(map, y, x, num   1);
			return;
		}
	}
	quwei(map, y, x);

}
void mogu(int map[][39])
{
	srand(time(0));
	int x, y;
	while (1)
	{
		x = rand() % 29   5;
		y = rand() % 29   5;
		if (map[y][x] < 100)
		{
			map[y][x] = 2;
			shuchu(y, x, 2);
			return;
		}
	}

}
void DFS_add(int map[][39], int y, int x, int num)
{
	int dirx[] = { 0,1,0,-1 };
	int diry[] = { -1,0,1,0 };
	int i;
	for (i = 0; i < 4; i  )
	{

		if (map[y   diry[i]][x   dirx[i]] == num)
		{
			map[y][x] = num;
			y  = diry[i];
			x  = dirx[i];
			DFS_add(map, y, x, num   1);
			return;
		}
	}
	map[y][x] = num;
	shuchu(y, x, '■');
}
void dumogu(int map[][39])
{
	int i, j;
	for (i = 1; i < 39; i  )
		for (j = 1; j < 39; j  )
			if (map[i][j] == 3)
			{
				map[i][j] = 1;
				shuchu(i, j, '□');
			}
	srand(time(0));
	int x, y, k = 0, tem = rand() % 6   1;
	while (k   < tem)
	{
		x = rand() % 29   5;
		y = rand() % 29   5;
		if (map[y][x] == 1)
		{
			map[y][x] = 3;
			shuchu(y, x, 3);
		}
	}
}
void kaishi(int map[][39], int Y, int X, int Dir, int ms)
{
	int dirx[] = { 0,1,0,-1 };
	int diry[] = { -1,0,1,0 };
	int defen = 0;
	queue<int>anjian;
	anjian.push(Dir);
	int kk = ms - '0'   1;
	ms = -30 * (ms - '0')   300;

	while (1)
	{
		mogu(map);
		while (map[Y   diry[Dir]][X   dirx[Dir]] == 1)
		{
			if (GetAsyncKeyState(VK_UP))
				anjian.push(0);
			if (GetAsyncKeyState(VK_DOWN))
				anjian.push(2);
			if (GetAsyncKeyState(VK_LEFT))
				anjian.push(3);
			if (GetAsyncKeyState(VK_RIGHT))
				anjian.push(1);

			if (map[Y   diry[Dir]][X   dirx[Dir]] > 100)
				break;
			yidong(Y   diry[Dir], X   dirx[Dir]);
			DFS(map, Y, X, 101);
			map[Y][X] = 101;
			Y  = diry[Dir];
			X  = dirx[Dir];
			map[Y][X] = 100;
			Sleep(ms);

			if (!anjian.empty())
			{
				Dir = anjian.front();
				anjian.pop();
			}
		}
		if (map[Y   diry[Dir]][X   dirx[Dir]] == 2)
		{
			map[Y   diry[Dir]][X   dirx[Dir]] = 100;
			shuchu(Y   diry[Dir], X   dirx[Dir], '■');
			DFS_add(map, Y, X, 101);
			map[Y][X] = 101;
			Y  = diry[Dir];
			X  = dirx[Dir];
			dumogu(map);
			defen = defen   5 * kk;
			xinxi2(8, 87, defen);

		}
		else
		{
			HANDLE hOut;
			COORD pos = { 0, 0 };
			hOut = GetStdHandle(STD_OUTPUT_HANDLE);
			pos.X = 34;
			pos.Y = 15;
			SetConsoleCursorPosition(hOut, pos);
			color(4);
			printf("Game Over");
			if (map[Y   diry[Dir]][X   dirx[Dir]] == 3)
			{
				pos.X = 34;
				pos.Y = 16;
				SetConsoleCursorPosition(hOut, pos);
				printf("GAME OVER!");
			}
			else if (map[Y   diry[Dir]][X   dirx[Dir]] == 0x7fffffff)
			{
				pos.X = 34;
				pos.Y = 16;
				SetConsoleCursorPosition(hOut, pos);
				printf("GAME OVER!");
			}
			else if (map[Y   diry[Dir]][X   dirx[Dir]] == 101)
			{
				pos.X = 34;
				pos.Y = 16;
				SetConsoleCursorPosition(hOut, pos);
				printf("GAME OVER!");
			}
			else
			{
				pos.X = 34;
				pos.Y = 16;
				SetConsoleCursorPosition(hOut, pos);
				printf("GAME OVER!");
			}

			color(15);
			Sleep(1000);
			pos.X = 0;
			pos.Y = 0;
			SetConsoleCursorPosition(hOut, pos);
			return;
		}
	}
}
int main()
{
	int map[39][39];
	int ms;
	while (1)
	{
		chushihua(map);
		showkuangjia();
		bimu();
		xinxi(5, 20, "红蘑菇是有毒的,千万不要碰哦!");
		xinxi(7, 25, "请输入难度级别(0~9):");
		while (putchar(ms = getch()), ms<'0' || ms>'9')
		{
			xinxi(8, 25, "输入错误,请重新输入:");
		}
		kaimu(map);
		int X, Y, Dir;
		snakelocal(&Y, &X, &Dir, map);
		kaishi(map, Y, X, Dir, ms);
	}
}