c语言推箱子源代码,给我写一个目录
时间: 2023-07-15 17:10:06 浏览: 88
这是一个简单的 C 语言推箱子游戏的源代码,包括了游戏的界面、关卡设计和游戏机制。以下是目录:
1. 头文件与宏定义
2. 结构体定义
3. 函数声明
4. 主函数
5. 游戏初始化函数
6. 游戏结束函数
7. 绘制游戏界面函数
8. 关卡设计函数
9. 玩家移动函数
10. 箱子移动函数
11. 判断游戏是否胜利函数
代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
// 宏定义
#define WALL '#'
#define BOX '*'
#define PATH ' '
#define TARGET 'o'
#define PLAYER 'P'
#define ROWS 10 // 行数
#define COLS 15 // 列数
#define LEVELS 3 // 关卡数
// 结构体定义
struct point {
int x;
int y;
};
// 函数声明
void gameInit(char levels[LEVELS][ROWS][COLS], int level, struct point *player, struct point boxes[]);
void gameEnd(char levels[LEVELS][ROWS][COLS], int level);
void drawGame(char levels[LEVELS][ROWS][COLS], int level, struct point player, struct point boxes[]);
int designLevels(char levels[LEVELS][ROWS][COLS], int level);
int movePlayer(char levels[LEVELS][ROWS][COLS], struct point *player, struct point boxes[]);
int moveBox(char levels[LEVELS][ROWS][COLS], struct point *box, struct point *player);
// 主函数
int main() {
char levels[LEVELS][ROWS][COLS] = {0}; // 存放关卡信息
struct point player; // 玩家坐标
struct point boxes[ROWS * COLS]; // 箱子坐标
int level = 0; // 当前关卡
int move; // 玩家移动方向
// 初始化游戏
gameInit(levels, level, &player, boxes);
// 游戏循环
while (1) {
// 绘制游戏界面
drawGame(levels, level, player, boxes);
// 玩家移动
move = movePlayer(levels, &player, boxes);
// 箱子移动
if (move) {
moveBox(levels, &boxes[move - 1], &player);
}
// 判断游戏是否胜利
if (designLevels(levels, level) == 0) {
printf("You Win!\n");
break;
}
}
// 结束游戏
gameEnd(levels, level);
return 0;
}
// 游戏初始化函数
void gameInit(char levels[LEVELS][ROWS][COLS], int level, struct point *player, struct point boxes[]) {
FILE *fp;
char ch;
int i, j, k;
// 读取关卡信息
fp = fopen("levels.txt", "r");
if (fp == NULL) {
printf("Cannot open file levels.txt\n");
exit(1);
}
for (k = 0; k < LEVELS; k++) {
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
ch = fgetc(fp);
while (ch == '\n' || ch == '\r') {
ch = fgetc(fp);
}
levels[k][i][j] = ch;
if (ch == PLAYER) {
player->x = i;
player->y = j;
} else if (ch == BOX) {
boxes[k * ROWS * COLS + i * COLS + j].x = i;
boxes[k * ROWS * COLS + i * COLS + j].y = j;
}
}
}
}
fclose(fp);
}
// 游戏结束函数
void gameEnd(char levels[LEVELS][ROWS][COLS], int level) {
// 清空关卡信息
int i, j, k;
for (k = 0; k < LEVELS; k++) {
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
levels[k][i][j] = 0;
}
}
}
}
// 绘制游戏界面函数
void drawGame(char levels[LEVELS][ROWS][COLS], int level, struct point player, struct point boxes[]) {
int i, j;
system("cls");
printf("Level %d\n", level + 1);
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (i == player.x && j == player.y) {
printf("%c", PLAYER);
} else if (levels[level][i][j] == WALL) {
printf("%c", WALL);
} else if (levels[level][i][j] == BOX) {
printf("%c", BOX);
} else if (levels[level][i][j] == PATH) {
printf("%c", PATH);
} else if (levels[level][i][j] == TARGET) {
printf("%c", TARGET);
}
}
printf("\n");
}
}
// 关卡设计函数
int designLevels(char levels[LEVELS][ROWS][COLS], int level) {
int i, j, count = 0;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (levels[level][i][j] == BOX) {
if (levels[level][i + 1][j] == TARGET) {
levels[level][i][j] = PATH;
levels[level][i + 1][j] = BOX;
} else if (levels[level][i - 1][j] == TARGET) {
levels[level][i][j] = PATH;
levels[level][i - 1][j] = BOX;
} else if (levels[level][i][j + 1] == TARGET) {
levels[level][i][j] = PATH;
levels[level][i][j + 1] = BOX;
} else if (levels[level][i][j - 1] == TARGET) {
levels[level][i][j] = PATH;
levels[level][i][j - 1] = BOX;
}
}
if (levels[level][i][j] == TARGET) {
count++;
}
}
}
return count;
}
// 玩家移动函数
int movePlayer(char levels[LEVELS][ROWS][COLS], struct point *player, struct point boxes[]) {
int x = player->x;
int y = player->y;
char ch = getch();
switch (ch) {
case 'w':
if (levels[0][x - 1][y] == WALL) {
break;
}
if (levels[0][x - 1][y] == BOX) {
if (levels[0][x - 2][y] == PATH || levels[0][x - 2][y] == TARGET) {
player->x--;
return 1;
}
} else {
player->x--;
}
break;
case 's':
if (levels[0][x + 1][y] == WALL) {
break;
}
if (levels[0][x + 1][y] == BOX) {
if (levels[0][x + 2][y] == PATH || levels[0][x + 2][y] == TARGET) {
player->x++;
return 2;
}
} else {
player->x++;
}
break;
case 'a':
if (levels[0][x][y - 1] == WALL) {
break;
}
if (levels[0][x][y - 1] == BOX) {
if (levels[0][x][y - 2] == PATH || levels[0][x][y - 2] == TARGET) {
player->y--;
return 3;
}
} else {
player->y--;
}
break;
case 'd':
if (levels[0][x][y + 1] == WALL) {
break;
}
if (levels[0][x][y + 1] == BOX) {
if (levels[0][x][y + 2] == PATH || levels[0][x][y + 2] == TARGET) {
player->y++;
return 4;
}
} else {
player->y++;
}
break;
case 'q':
exit(0);
break;
}
return 0;
}
// 箱子移动函数
int moveBox(char levels[LEVELS][ROWS][COLS], struct point *box, struct point *player) {
int x = box->x;
int y = box->y;
if (box->x == player->x && box->y == player->y) {
return 0;
}
if (levels[0][x][y] == TARGET) {
levels[0][x][y] = BOX;
} else {
levels[0][x][y] = PATH;
}
if (box->x < player->x) {
box->x--;
} else if (box->x > player->x) {
box->x++;
} else if (box->y < player->y) {
box->y--;
} else if (box->y > player->y) {
box->y++;
}
if (levels[0][box->x][box->y] == TARGET) {
levels[0][box->x][box->y] = BOX;
} else {
levels[0][box->x][box->y] = BOX;
}
return 0;
}
```
阅读全文