C++实现《走迷宫》小游戏

原文链接: www.baidu.com
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<conio.h>
int main() {
	char a[50][50] = { "######",
						"#O #  ",
						"# ## #",
						"#  # #",
						"##   #",
						"######"};
	int  i, x, y, p, q;
	x = 1; y = 1; p = 1; q = 5;  //出生点和出口点
	char ch;
	for (i = 0; i <=5; i++)    //循环6次
		puts(a[i]);   //输出得到6行
	while (x!=p||y!=q)   //除了出口位置以外都可以移动
	{
		ch = _getch();      //赋值字符
		if (ch == 's')    //方向“下”键,,移动方向已二维坐标系为准
		{  
			if (a[x + 1][y] != '#')
			{//当碰到墙壁则不移动,不然就变成穿墙外挂了
				a[x][y] = ' '; //无墙时则移动
				x++;
				a[x][y] = 'O';
			}
		}
		if (ch=='w')
		{
			if (a[x - 1][y] != '#')
			{
				a[x][y] = ' ';
				x--;
				a[x][y] = 'O';
			}
		}
		if (ch=='a')
		{
			if(a[x][y-1]!='#')
			{
				a[x][y] = ' ';
				y--;
				a[x][y] = 'O';
			}
		}
		if (ch=='d')
		{
			if(a[x][y + 1] != '#')
			{
				a[x][y] = ' ';
				y++;
				a[x][y] = 'O';
			}
		}
		system("cls");    //清屏 ,,因为每次移动要清楚之前的位置,,可以理解为 刷新
		for (i = 0; i <= 5; i++)   //给每行加6个字符
			puts(a[i]);   //得到一行6个字符串
	}
	system("cls");
	printf("恭喜你通关了\n");
		Sleep(5000);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/YeLuoSiLiuNian/article/details/102768951