走迷宫C++小游戏

本文介绍了一个基于C++编写的控制台游戏,玩家操控旅行者在随机生成的迷宫中移动,通过键盘输入(a/w/s/d)控制角色,目标是找到终点并赢得胜利。
摘要由CSDN通过智能技术生成

 

随机障碍物的迷宫,超级好玩

你将扮演一位名为「旅行者」($)的神秘角色,在自由的旅行中邂逅性格各异、能力独特的同伴们,和他们一起击败强敌,找回失散的亲人(☆)

#include<iostream>
#include<windows.h>//CursorInfo
#include<conio.h>
#include<ctime>
using namespace std;
const int N = 15;
int m[N][N], step, vis[N][N], startx, starty, endx, endy, mark;
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
void HideCursor()
{
	CONSOLE_CURSOR_INFO CursorInfo; // 定义光标信息的结构体变量
	CursorInfo.dwSize = 1;          // 赋值光标尺寸才能隐藏光标
	CursorInfo.bVisible = FALSE;    // 将光标设置为不可见
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); // 获取控制台标准输出设备的句柄
	SetConsoleCursorInfo(handle, &CursorInfo); // 设置控制台光标信息
}
void CursorJump(int x, int y)
{
	COORD pos;
	pos.X = x;
	pos.Y = y;
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(handle, pos);

}
void welcome()
{
	for (int i = 0; i < 10; i++)
	{
		cout << endl;
	}
	cout << R"(
		■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■    
		■                                                                 ■ 
		■                                                                 ■
		■                                                                 ■
		■                    在规定时间内走出迷宫                         ■
		■                    否则游戏失败                                 ■
		■                                                                 ■
		■                                                                 ■
		■                                                                 ■
		■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■  )";
	cout << "\n		";
	getch();
	system("cls");//清除控制台屏幕 
	for (int i = 0; i < 15; i++)
	{
		cout << endl;
	}
	cout << "                                 用a, w, s, d(英文)控制方向";
		getch();
	system("cls");
}
void Init()
{
	srand(time(0));
	for (int j = 0; j < N; j++)
	{
		m[0][j] = 1;
		m[N - 1][j] = 1;
	}
	for (int j = 0; j < N; j++)
	{
		m[j][0] = 1;
		m[j][N - 1] = 1;
	}
	for (int j = 0; j < (N - 5) * (N - 5); j++)
		m[rand() % (N - 2) + 1][rand() % (N - 2) + 1] = 1;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			if (m[i][j] == 1)  cout << "■ ";
			else cout << "  ";
		}
		cout << endl;
	}
}
void dfs(int x, int y, int len)
{
	if (vis[x][y] == 1)return;
	else vis[x][y] = 1;
	if (len > step)
	{
		endx = x, endy = y;
		step = len;
	}
	for (int i = 0; i < 4; i++)
	{
		if (m[x + dx[i]][y + dy[i]] == 1)continue;
		else dfs(x + dx[i], y + dy[i], len + 1);
	}

}
void Pos() {
	int flag = 0;
	for (int i = 1; i < N - 1; i++)
	{
		for (int j = 1; j < N - 1; j++)
			if (m[i][j] == 0)
			{
				for (int k = 0; k < 4; i++)
				{
					if (m[i + dx[k]][j + dy[k]] == 0)
					{
						flag = 1;
						break;
					}
				}
				if (flag == 1)
				{
					startx = i;
					starty = j;
					break;
				}
			}
		if (flag == 1)break;
	}
	if (flag == 0)
	{
		Init();
		Pos();
		return;

	}
	else
	{
		dfs(startx, starty, 0);
		CursorJump(2 * endy, endx);
		cout << "☆ ";//空格保证不会被“\b ”覆盖

	}

}
void Play()
{
	char c;
	CursorJump(starty * 2, startx);
	cout << "$";
	while (1)
	{
		int x, y, flag = 0;
		c = getch();
			switch (c)
			{
			case 'w':
				x = startx - 1, y = starty;
				break;
			case 's':
				x = startx + 1, y = starty;
				break;
			case 'a':
				x = startx, y = starty - 1;
				break;
			case 'd':
				x = startx, y = starty + 1;
				break;
			default:
				flag = 1;
				break;
			}
		if (flag == 1 || m[x][y] == 1)
			continue;
		else {
			cout << "\b ";//+++space
			CursorJump(2 * y, x);

			startx = x, starty = y;
			cout << "$";
			if (x == endx && y == endy)
			{
				CursorJump(2 * N + 1, N + 1);
				cout << "You have winned 14.514% mankind.";
			}
		}
	}
}
int main()
{
	HideCursor();
	welcome();
	Init();
	Pos();
	Play();

}

  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值