59.【C++迷宫小游戏(超详细,有手就行)】

#include 
#include       //system函数
#include         //getch()函数
using namespace std;
int x = 1, y = 1;
char map[25][61] =
{
	"000000000000000000000000000000000000000000000000000000000000",
	"0!000000         0000000000000000000000000000000000000000000",
	"0 000000 0000000 0000000000000000000         000000000000000",
	"0 000000 0000000               00000 0000000 000000000000000",
	"0 000000 0000000 0000000000000 00000 0000000         0000000",
	"0        0000000 0000000000000       00000000000000000000000",
	"0000000000000000 000000000000000000000000000000000          ",      //出口
	"000              0000000           000000000000000 000000000",
	"000 000000000000 0000000 000000000 000000000000000 000000000",
	"000 00           0000000 0         00000000        000000000",
	"000 00 00000000000000000 0 0000000 00000000 0000000000000000",
	"000 00 00000000000       0 0000000 00000000 0000 00000000000",
	"000 00 00000000000 0000000 0000000          0000 00000000000",
	"000 00 00000000000 000000  00000000 000000000000 00000000000",
	"00  00             000000 000000000 000000000000 00000000000",
	"000000 00000000000 000000 000000000 000000000000 000000    0",
	"00     00000000000                    0000000000 000000 0000",
	"00 000000000000000 0000000 0000000000 0000000000 000000 0000",
	"00 000000          0000000 0000000000 0000000000 000000 0000",
	"00 000000 0000000000000000 0000000000 0000000000        0000",
	"00 000000 0000000000000000 0000000000 0000000000 00000000000",
	"00 000000 000000           0000000000 0000000000 00000000000",
	"00        00000000 0000000 0000000000            00000000000",
	"000000000000000000 0000000          000000000000000000000000",
	"000000000000000000000000000000000000000000000000000000000000",
	
};
void key_w()
{
	if (map[x - 1][y] != '0')          //判断是否与上方相撞
	{
		map[x][y] = ' ';              
		x--;                           //进行位移准备
		map[x][y] = '!';               //  开始位移       
	}
}
void key_s()
{
	if (map[x + 1][y] != '0')
	{
		map[x][y] = ' ';
		x++;
		map[x][y] = '!';
	}
}
void key_a()
{
	if (map[x][y - 1] != '0')
	{
		map[x][y] = ' ';
		y--;
		map[x][y] = '!';
	}
}
void key_d()
{
	if (map[x][y + 1] != '0')
	{
		map[x][y] = ' ';
		y++;
		map[x][y] = '!';
	}
}

int main()
{
	cout << "**********************欢迎来到迷宫大挑战**********************" << endl<<endl<<endl<<endl;
	cout << "W----上移" << "        " << "S----下移" << "       " << "A----左移" << "     " << "D---右移" << endl<<endl<<endl<<endl;
	int p = 6, q = 60;
	char ch;
	for (int i = 0; i < 25; i++)
	{
		for (int j = 0; j < 60; j++)
		{
			cout << map[i][j];
		}
		cout << endl;
	}
	while (x != p || y != q)    //不能是&&,因为while遇到true的时候,会出去
	{
		ch = _getch();          //获取您输入的第一个字符,在c是getch()**加粗样式**
		switch (ch)
		{
		case 's':key_s(); break;
		case 'w':key_w(); break;
		case 'a':key_a(); break;
		case 'd':key_d(); break;
		default:
			cout << "您的指令会出现错误!" << endl;
		}
		system("cls");      //清屏

		for (int i = 0; i < 25; i++)       //目的是得出改变后的图像
		{
			for (int j = 0; j < 60; j++)
			{
				cout << map[i][j];
			}
			cout << endl;
		}
	}
	cout << "恭喜您冲关成功!" << endl;
	system("pasue");            //目的是锁屏
	return 0;

}

59.【C++迷宫小游戏(超详细,有手就行)】_第1张图片

如果我讲的不够详细,您可以私信我。我会进一步向您阐述!
【关于getchar()的小专题】
getchar()会的到你手动输入的一个字符,然后把这个字符放到一个缓冲区,直到你按下换行键,才会进行操作.

#include 
using namespace std;
int main()
{
	long nc;
	nc = 0;
	while (getchar() != '\n')    //getchar()得到一个字符并放在缓冲区,当按下回车键时才会运行
	{
		++nc;
		cout << "仨孩子" << endl;
	}
	cout << " " << nc << endl;

}

59.【C++迷宫小游戏(超详细,有手就行)】_第2张图片

你可能感兴趣的:(c++,c语言,蓝桥杯)