推箱子(C语言版)-创新互联

推箱子这个小游戏,当初自己也写了很久

成都创新互联公司长期为千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为名山企业提供专业的成都网站设计、做网站,名山网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。

这个游戏:就是人将所有箱子推到目的地,便算是成功

主要思路:

1.用二维数组和枚举绘制地图(如果要增加关卡,就用三维数组(里面存放的元素是多个二维数组),这里我写了三关)

2.用数组的位置来写人和箱子的移动,再配合switch语句,就可以实现用键盘操控人的移动。

3.游戏结束的标志:空地上没有箱子

源码在这,如有需要,请自行领取。

#include   // 推箱子的几个元素;
#include //  0 空地   1 墙  2 目的地   3 箱子 4 玩 5 箱子在目的地  6 玩家在目的地 
#include//_getch()函数的头文件   //用_getch函数,如果用getchar(),每次移动要回车键  // 上 72  下 80  左75   右77

enum Mine
{
	space,
	wall,
	destination,
	box,
	player,
};
int level = 0;
int map[3][10][10] =
{
	{
		{0, 0, 0, 1, 1, 1 ,1 ,1, 0, 0},
		{0, 0, 0, 1, 0, 0 ,2 ,1, 0, 0},
		{0, 0, 0, 1, 0, 3 ,0 ,1, 0, 0},
		{0, 1, 1, 1, 0, 0 ,0 ,1, 1, 1},
		{0, 1, 0, 2, 3 ,4 ,3, 0, 2, 1},
		{0, 1, 1, 1, 0, 3 ,0 ,1, 1, 1},
		{0, 0, 0, 1, 0, 2 ,0 ,1, 0, 0},
		{0, 0, 0, 1, 0, 0 ,0 ,1, 0, 0},
		{0, 0, 0, 1, 1, 1 ,1 ,1, 0, 0},
		{0, 0, 0, 0, 0, 0 ,0 ,0, 0, 0},
	},

	{
		{1, 1, 1, 1, 1, 1 ,1 ,1, 1, 1},
		{1, 2, 0, 0, 0, 0 ,0 ,0, 0, 1},
		{1, 0, 0, 0, 0, 0 ,3 ,0, 1, 0},
		{1, 0, 0, 3, 0, 0 ,0 ,0, 0, 1},
		{1, 0, 0, 3, 0 ,4 ,0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0 ,1 ,0, 0, 1},
		{1, 2, 1, 1, 0, 0 ,1 ,0, 2, 1},
		{1, 1, 1, 0, 1, 0 ,1 ,0, 1, 0},
		{0, 0, 0, 0, 1, 0 ,1 ,0, 1, 0},
		{0, 0, 0, 0, 1, 1 ,1 ,1, 0, 0},
	},

	{
		{0, 0, 0, 1, 1, 1 ,1 ,0, 0, 0},
		{0, 0, 1, 0, 0, 0 ,0 ,1, 0, 0},
		{0, 1, 0, 3, 0, 3 ,0 ,0, 1, 0},
		{1, 2, 0, 0, 0, 0 ,1 ,0, 0, 1},
		{1, 1, 1, 1, 2 ,0 ,1, 0, 0, 1},
		{1, 0, 0, 1, 1, 1 ,1 ,0, 0, 1},
		{1, 2, 0, 0, 0, 4 ,0 ,0, 0, 1},
		{0, 1, 0, 0, 3, 0 ,3 ,0, 1, 0},
		{0, 0, 1, 0, 0, 0 ,2 ,1, 0, 0},
		{0, 0, 0, 1, 1, 1 ,1 ,0, 0, 0},
	}

};

void drawmap()
{
	for (int i = 0; i< 10; ++i)
	{
		for (int j = 0; j< 10; ++j)
		{
			switch (map[level][i][j])
			{
			case space:
				printf("  ");
				break;
			case wall:
				printf("▓ ");
				break;
			case destination:
				printf("☆");
				break;
			case box:
				printf("□");
				break;
			case player:
				printf("♀");
				break;
			case box + destination:
				printf("★");
				break;
			case player + destination:
				printf("♂");
				break;
			}
		}
		printf("\n");
	}
}
void playgame()
{
	int i = 0, j = 0;
	for (i = 0; i< 10; i++)
	{
		for (j = 0; j< 10; j++)
		{
			if (map[level][i][j] == player || map[level][i][j] == player + destination)
			{
				goto end;       //break跳出一层循环,用  goto end; end:; 跳出所有的循环
			}
		}
	}
end:;
	int  key = _getch();
	switch (key)
	{
		case 'w':      // 上
		case 'W':
		case  72:
			if (map[level][i - 1][j] ==  space || map[level][i - 1][j] ==  destination)
			{
				map[level][i - 1][j] += player;
				map[level][i][j] -= player;
			}
			else
			{
				if (map[level][i - 1][j] == box || map[level][i - 1][j] == box + destination)
				{
					if (map[level][i - 2][j] == space || map[level][i - 2][j] == destination)
					{
						map[level][i - 2][j] += box;
						map[level][i - 1][j] = map[level][i - 1][j] - box + player;
						map[level][i - 1][j] -=  player;
					}
				}
			}
			break;

		case 's':     //  下
		case 'S':
		case 80:
			if (map[level][i + 1][j] == space || map[level][i + 1][j] == destination)
			{
				map[level][i + 1][j] = map[level][i + 1][j] + player;
				map[level][i][j] = map[level][i][j] - player;
			}
			else
			{
				if (map[level][i + 1][j] == box || map[level][i + 1][j] == box + destination)
				{
					if (map[level][i + 2][j] == space || map[level][i + 2][j] == destination)
					{
						map[level][i + 2][j] = map[level][i + 2][j] + box;
						map[level][i + 1][j] = map[level][i + 1][j] - box + player;
						map[level][i + 1][j] = map[level][i + 1][j] - player;
					}
				}
			}
			break;
		case 'a':     //  左
		case 'A':
		case 75:
			if (map[level][i][j - 1] == space || map[level][i][j - 1] == destination)
			{
				map[level][i][j - 1] += player;
				map[level][i][j] -= player;
			}
			else
			{
				if (map[level][i][j - 1] == box || map[level][i][j - 1] == box + destination)
				{
					if (map[level][i][j - 2] == space || map[level][i][j - 2 ] == destination)
					{
						map[level][i][j - 2] += box;
						map[level][i][j - 1] = map[level][i][j - 1] - box + player;
						map[level][i][j - 1] -= player;
					}
				}
			}			
			break;

		case 'd':      // 右
		case 'D':
		case 77:
			if (map[level][i][j + 1] == space || map[level][i][j + 1] == destination)
			{
				map[level][i][j + 1] += player;
				map[level][i][j] -= player;
			}
			else
			{
				if (map[level][i][j + 1] == box || map[level][i][j + 1] == box + destination)
				{
					if (map[level][i][j + 2] == space || map[level][i][j + 2] == destination)
					{
						map[level][i][j + 2] += box;
						map[level][i][j + 1] = map[level][i][j + 1] - box + player;
						map[level][i][j + 1] -= player;
					}
				}
			}
			break;
	}
}
bool deduce_success()
{
	for (int a = 0; a< 10; a++)
	{
		for (int b = 0; b< 10; b++)
		{
			if (map[level][a][b] == box)
			{
				return false;
			}
		}
	}
	return true;
}

int main()
{
	//cols 长  lines宽
	system("mode con cols=25 lines=15");

	while (1)
	{
		system("cls");
		drawmap();

		if (deduce_success())
		{
			level++;
			if (level >2)
			{
				printf("你赢了");
				printf("不愧是地表最强的人!!!!!!");
				printf("恭喜通关!!!!!!");
				break;
			}
		}

		playgame();
	}

	

	getchar();

	return 0;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


文章名称:推箱子(C语言版)-创新互联
标题来源:http://csdahua.cn/article/ceoiec.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流