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

代码如下:

#include<stdio.h>
#include<conio.h>
#include<stdlib.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();
printf("\n");
		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;
}

  • 34
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 22
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值