c语言推箱子完整代码
时间: 2023-11-07 10:47:36 浏览: 80
当然,下面是一个简单的 C 语言推箱子游戏的完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX_ROW 10
#define MAX_COL 10
// 定义地图元素类型
typedef enum {
EMPTY,
WALL,
BOX,
TARGET,
PLAYER
} element;
// 定义地图结构体
typedef struct {
int row;
int col;
} position;
// 初始化地图
void initializeMap(element map[MAX_ROW][MAX_COL], position *player, position targets[], int *numTargets) {
element initialMap[MAX_ROW][MAX_COL] = {
{WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL, WALL},
{WALL, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL},
{WALL, EMPTY, BOX, BOX, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL},
{WALL, EMPTY, BOX, TARGET, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL},
{WALL, PLAYER, EMPTY, EMPTY, EMPTY, TARGET, EMPTY, BOX, EMPTY, WALL},
{WALL, EMPTY, BOX, BOX, EMPTY, EMPTY, TARGET, TARGET, TARGET, WALL},
{WALL, EMPTY, BOX, EMPTY, TARGET, EMPTY, TARGET, PLAYER, EMPTY, WALL},
{WALL, EMPTY, BOX, EMPTY, TARGET, BOX, TARGET, TARGET, EMPTY, WALL},
{WALL, EMPTY, BOX, TARGET, TARGET, TARGET, TARGET, TARGET ,EMPTY ,WALL},
{WALL ,WALL ,WALL ,WALL ,WALL ,WALL ,WALL ,WALL ,WALL ,WALL}
};
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j < MAX_COL; j++) {
map[i][j] = initialMap[i][j];
if (map[i][j] == PLAYER) {
player->row = i;
player->col = j;
}
if (map[i][j] == TARGET) {
targets[*numTargets].row = i;
targets[*numTargets].col = j;
(*numTargets)++;
}
}
}
}
// 渲染地图
void renderMap(element map[MAX_ROW][MAX_COL]) {
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j < MAX_COL; j++) {
switch(map[i][j]) {
case WALL:
printf("#");
break;
case EMPTY:
printf(" ");
break;
case BOX:
printf("$");
break;
case TARGET:
printf(".");
break;
case PLAYER:
printf("@");
break;
}
}
printf("\n");
}
}
// 移动玩家
void movePlayer(element map[MAX_ROW][MAX_COL], position *player, int row, int col) {
int newRow = player->row + row;
int newCol = player->col + col;
if (map[newRow][newCol] == EMPTY || map[newRow][newCol] == TARGET) {
map[player->row][player->col] = (map[player->row][player->col] == TARGET) ? TARGET : EMPTY;
player->row = newRow;
player->col = newCol;
map[player->row][player->col] = PLAYER;
}
else if (map[newRow][newCol] == BOX) {
int newBoxRow = newRow + row;
int newBoxCol = newCol + col;
if (map[newBoxRow][newBoxCol] == EMPTY || map[newBoxRow][newBoxCol] == TARGET) {
map[player->row][player->col] = (map[player->row][player->col] == TARGET) ? TARGET : EMPTY;
player->row = newRow;
player->col = newCol;
map[player->row][player->col] = PLAYER;
map[newBoxRow][newBoxCol] = (map[newBoxRow][newBoxCol] == TARGET) ? BOX : TARGET;
}
}
}
int main() {
element map[MAX_ROW][MAX_COL];
position player;
position targets[MAX_ROW * MAX_COL];
int numTargets = 0;
initializeMap(map, &player, targets, &numTargets);
while (1) {
system("cls");
renderMap(map);
if (numTargets == 0) {
printf("You win!\n");
break;
}
char input = getch();
switch(input) {
case 'w':
movePlayer(map, &player, -1, 0);
break;
case 's':
movePlayer(map, &player, 1, 0);
break;
case 'a':
movePlayer(map, &player, 0, -1);
break;
case 'd':
movePlayer(map, &player, 0, 1);
break;
case 'q':
printf("Goodbye!\n");
exit(0);
}
}
return 0;
}
```
这段代码实现了一个简单的推箱子游戏。你可以使用WASD键来控制玩家的移动,目标是将所有的箱子推到目标位置上。运行代码后,地图会显示在控制台上,直至完成游戏或按下Q键退出。