自学三个月编写简单走迷宫游戏

代码如下:

#include<stdio.h>
#include<Windows.h>
int main()
{	
	int i = 0;
	char a[50][50] = {	"######",
						"#o #  ",
						"# ## #",
						"#  # #",  
						"##   #",  
						"######", };
	
	printf("w:上n");
	printf("s:下n");
	printf("a:左n");
	printf("d:右n");
	printf("点击空格开启游戏n");
	int x=1, y=1, l=1, p=5;//x,y记录小球初始的行列数;l,p记录出口位置
	char ch;
	ch = getche();
		while (ch == ' ')
		{
			for (i = 0; i <= 5; i++)
			{
				system("color 1b");
				puts(a[i]);
			}
			break;//防止无限打印
		}
		char m;
		
		while (x!=l||y!=p)
		{
			m = getch();
			if (m == 's')//读入操作
			{
				if (a[x + 1][y] != '#')
				{
					a[x][y] = ' ';//将原来的点初始化为空格
					x++;
					a[x][y] = 'o';//将移动后的点转换为物体,注意要用‘’
					system("cls");//清屏
					for (i = 0; i <= 5; i++)
					{
						puts(a[i]);
						system("color 1b");
					}//清屏之后再次打印
				}

			}
			if (m == 'w')//读入操作
			{
				if (a[x-1][y] != '#')
				{
					a[x][y] = ' ';//将原来的点初始化为空格
					x--;
					a[x][y] = 'o';//将移动后的点转换为物体,注意要用‘’
					system("cls");//清屏
					for (i = 0; i <= 5; i++)
					{
						puts(a[i]);
						system("color 1b");
					}//清屏之后再次打印
				}

			}
			if (m == 'a')//读入操作
			{
				if (a[x][y-1] != '#')
				{
					a[x][y] = ' ';//将原来的点初始化为空格
					y--;
					a[x][y] = 'o';//将移动后的点转换为物体,注意要用‘’
					system("cls");//清屏
					for (i = 0; i <= 5; i++)
					{
						puts(a[i]);
						system("color 1b");
					}//清屏之后再次打印
				}

			}
			if (m == 'd')//读入操作
			{
				if (a[x ][y+1] != '#')
				{
					a[x][y] = ' ';//将原来的点初始化为空格
					y++;
					a[x][y] = 'o';//将移动后的点转换为物体,注意要用‘’
					system("cls");//清屏
					for (i = 0; i <= 5; i++)
					{
						puts(a[i]);
						system("color 1b");
					}//清屏之后再次打印
				}

			}
		
		}
		system("color 2f");
		printf("游戏成功n");
		Sleep(5000);
	return 0;
}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>