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