写一个C++,实现推箱子
时间: 2024-03-24 13:41:42 浏览: 20
推箱子是一个比较经典的游戏,下面是一个简单的 C++代码实现:
```c++
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;
const int MAX_ROW = 10;
const int MAX_COL = 10;
const int WALL = -1;
const int SPACE = 0;
const int BOX = 1;
const int TARGET = 2;
const int PLAYER = 3;
int map[MAX_ROW][MAX_COL] = {
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, 0, 0, 0, -1, 0, 0, 0, 0, -1},
{ -1, 0, 0, 0, -1, 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, 0, 0, 0, 2, 0, -1},
{ -1, 0, 0, 0, 0, 0, 0, 1, 0, -1},
{ -1, 0, 0, 0, 0, 0, 0, 3, 0, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
};
int player_row = 8;
int player_col = 7;
void draw_map() {
system("cls");
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j < MAX_COL; j++) {
if (map[i][j] == WALL) {
cout << "#";
} else if (map[i][j] == SPACE) {
cout << " ";
} else if (map[i][j] == BOX) {
cout << "$";
} else if (map[i][j] == TARGET) {
cout << "O";
} else if (map[i][j] == PLAYER) {
cout << "@";
}
}
cout << endl;
}
}
bool is_win() {
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j < MAX_COL; j++) {
if (map[i][j] == BOX) {
return false;
}
}
}
return true;
}
void move_box(int row, int col, int dir) {
if (dir == 0) { // 上
if (map[row-1][col] == SPACE) {
map[row-1][col] = BOX;
map[row][col] = SPACE;
} else if (map[row-1][col] == TARGET) {
map[row-1][col] = BOX;
map[row][col] = SPACE;
}
} else if (dir == 1) { // 下
if (map[row+1][col] == SPACE) {
map[row+1][col] = BOX;
map[row][col] = SPACE;
} else if (map[row+1][col] == TARGET) {
map[row+1][col] = BOX;
map[row][col] = SPACE;
}
} else if (dir == 2) { // 左
if (map[row][col-1] == SPACE) {
map[row][col-1] = BOX;
map[row][col] = SPACE;
} else if (map[row][col-1] == TARGET) {
map[row][col-1] = BOX;
map[row][col] = SPACE;
}
} else if (dir == 3) { // 右
if (map[row][col+1] == SPACE) {
map[row][col+1] = BOX;
map[row][col] = SPACE;
} else if (map[row][col+1] == TARGET) {
map[row][col+1] = BOX;
map[row][col] = SPACE;
}
}
}
void move_player(int dir) {
if (dir == 0) { // 上
if (map[player_row-1][player_col] == SPACE) {
map[player_row-1][player_col] = PLAYER;
map[player_row][player_col] = SPACE;
player_row--;
} else if (map[player_row-1][player_col] == BOX) {
if (map[player_row-2][player_col] == SPACE || map[player_row-2][player_col] == TARGET) {
move_box(player_row-1, player_col, dir);
map[player_row-1][player_col] = PLAYER;
map[player_row][player_col] = SPACE;
player_row--;
}
}
} else if (dir == 1) { // 下
if (map[player_row+1][player_col] == SPACE) {
map[player_row+1][player_col] = PLAYER;
map[player_row][player_col] = SPACE;
player_row++;
} else if (map[player_row+1][player_col] == BOX) {
if (map[player_row+2][player_col] == SPACE || map[player_row+2][player_col] == TARGET) {
move_box(player_row+1, player_col, dir);
map[player_row+1][player_col] = PLAYER;
map[player_row][player_col] = SPACE;
player_row++;
}
}
} else if (dir == 2) { // 左
if (map[player_row][player_col-1] == SPACE) {
map[player_row][player_col-1] = PLAYER;
map[player_row][player_col] = SPACE;
player_col--;
} else if (map[player_row][player_col-1] == BOX) {
if (map[player_row][player_col-2] == SPACE || map[player_row][player_col-2] == TARGET) {
move_box(player_row, player_col-1, dir);
map[player_row][player_col-1] = PLAYER;
map[player_row][player_col] = SPACE;
player_col--;
}
}
} else if (dir == 3) { // 右
if (map[player_row][player_col+1] == SPACE) {
map[player_row][player_col+1] = PLAYER;
map[player_row][player_col] = SPACE;
player_col++;
} else if (map[player_row][player_col+1] == BOX) {
if (map[player_row][player_col+2] == SPACE || map[player_row][player_col+2] == TARGET) {
move_box(player_row, player_col+1, dir);
map[player_row][player_col+1] = PLAYER;
map[player_row][player_col] = SPACE;
player_col++;
}
}
}
}
int main() {
while (true) {
draw_map();
if (is_win()) {
cout << "You win!" << endl;
break;
}
char input = getch();
switch (input) {
case 'w': move_player(0); break;
case 's': move_player(1); break;
case 'a': move_player(2); break;
case 'd': move_player(3); break;
}
}
return 0;
}
```
这个代码中,使用2D数组存储了地图数据,通过移动人物和箱子来达到游戏目标。其中,WASD键用于控制人物的移动,达到推动箱子的效果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)