推箱子小游戏实现思路

文章描述了一个基于C++的推箱子游戏的实现思路,使用三维数组存储地图和目标位置,通过按键控制角色和箱子移动。游戏逻辑包括角色和箱子的移动判断,检查箱子是否到达目标位置,以及计步和计时功能。当所有箱子到达目标位置后,游戏进入下一关,直至所有关卡完成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总体实现思路:用一个三维数组来存储推箱子的地图,第一维是关卡,第二三维表示每张地图。再使用一个三维数组来存储箱子的目的位置。在箱子移动方面:通过按键来控制移动方向,主要是预测角色下一步的位置,若是下一步位置为空则可以直接移动,若是下一步位置为箱子,则继续判断箱子的落点,若落点为空位置,则可以移动,为墙或者为箱子则不能移动。若是所有箱子都到了目的位置,则完成本关,进入下一关。同时需要记录步数以及所花时间。
备注:0为空位置,1为墙壁,2为角色,3为箱子,4为目标位置

主要实现代码:

static long long t1 = 0;
long long t2;
static int second = 0;
static int map[][10][10] = {
	{{1,1,1,1,1,1,1,1,1,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,3,0,0,0,0,1},
	{1,0,0,0,0,0,3,0,0,1},
	{1,0,0,0,0,0,0,2,0,1},
	{1,0,0,0,4,4,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1}},
		{{1,1,1,1,1,1,1,1,1,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,3,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,3,0,0,0,0,2,0,1},
	{1,0,0,0,4,4,0,0,0,1},
	{1,0,0,0,4,0,0,0,0,1},
	{1,0,0,0,0,0,0,3,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1}},
		{{1,1,1,1,1,1,1,1,1,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,3,3,0,0,2,0,1},
	{1,0,0,3,4,4,4,0,0,1},
	{1,0,0,0,4,3,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1}}

};
static int targetMap[][10][10] = {
		{{1,1,1,1,1,1,1,1,1,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,4,4,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1}},	
		{{1,1,1,1,1,1,1,1,1,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,2,0,1},
	{1,0,0,0,4,4,0,0,0,1},
	{1,0,0,0,4,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1}},
		{{1,1,1,1,1,1,1,1,1,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,2,0,1},
	{1,0,0,0,4,4,4,0,0,1},
	{1,0,0,0,4,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,0,0,0,0,0,0,0,0,1},
	{1,1,1,1,1,1,1,1,1,1}}

};
static int playerX = 4;
static int playerY = 7;
static int flag = 0; //成功标志
static int printFlag = 1;
static int stepCount = 0;
int count[3] = {2,3,4};
static int level = 0;
void draw_picture();
void PushBox() {
	ImGui_SetNextWindowPos(0, 20);
	ImGui_SetNextWindowSize(800, 300);

	ImGui_Begin(u8"推箱子", NULL, NULL);
	draw_picture();
	if (!flag) {
		int dirX = 0, dirY = 0;
		if (ImGui_IsKeyPressed(ImGuiKey_A)) dirY = -1;
		if (ImGui_IsKeyPressed(ImGuiKey_D)) dirY = 1;
		if (ImGui_IsKeyPressed(ImGuiKey_W)) dirX = -1;
		if (ImGui_IsKeyPressed(ImGuiKey_S)) dirX = 1;
		if (dirX != 0 || dirY != 0) {
			stepCount++;
			int nx = playerX + dirX;
			int ny = playerY + dirY;

			if (map[level][nx][ny] == 3 && map[level][nx + dirX][ny + dirY] != 1 && map[level][nx + dirX][ny + dirY] != 3) {
				map[level][nx + dirX][ny + dirY] = map[level][nx][ny];
				map[level][nx][ny] = map[level][playerX][playerY];
				map[level][playerX][playerY] = 0;

				playerX = nx;
				playerY = ny;
			}
			if (map[level][nx][ny] == 0|| map[level][nx][ny] == 4) {
				map[level][nx][ny] = map[level][playerX][playerY];
				map[level][playerX][playerY] = 0;
				playerX = nx;
				playerY = ny;
			}
			if (targetMap[level][playerX + (-dirX)][playerY + (-dirY)] == 4 &&
				map[level][playerX + (-dirX)][playerY + (-dirY)] == 0) {
				map[level][playerX + (-dirX)][playerY + (-dirY)] = 4;
			}
			draw_picture();
		}
		int sum = 0;
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				if (map[level][i][j] == 3 && targetMap[level][i][j] == 4) sum++;
			}
		}
		if (sum == count[level]) {
			playerX = 4;
			playerY = 7;
			if (level == 2) {
				flag = 1;
				PrintLog(u8"游戏结束!");
			}
			if (level < 2) {
				level++;
				flag = 0;
				stepCount = 0;
				second = 0;
			}

		}
	}
	t2 = GetTickCount64();
	if (t2 - t1 > 1000) {
		t1 = t2;
		second++;
	}
	ImGui_End();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值