写一个小游戏推箱子的简单的c语言代码,并对每行代码进行注释,最后阐述运行原理及思路,同时说出怎么控制移动
时间: 2024-05-16 14:12:51 浏览: 17
以下是一个简单的推箱子游戏的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define ROW 10
#define COL 10
int map[ROW][COL] = {
{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,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,1,1,1,1,1,1,1,1,1},
};
int playerRow = 1; // 玩家所在的行
int playerCol = 1; // 玩家所在的列
void draw() { // 绘制地图
system("cls"); // 清屏
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (map[i][j] == 1) {
printf("#");
} else if (i == playerRow && j == playerCol) {
printf("@");
} else if (map[i][j] == 2) {
printf("$");
} else {
printf(" ");
}
}
printf("\n");
}
}
int main() {
draw(); // 绘制地图
char input;
while (1) {
input = getch(); // 获取用户输入
switch (input) {
case 'w': // 上移
if (map[playerRow - 1][playerCol] == 0) { // 判断上方是否是空地
playerRow--;
} else if (map[playerRow - 1][playerCol] == 2) { // 判断上方是否是箱子
if (map[playerRow - 2][playerCol] == 0) { // 判断箱子上方是否是空地
map[playerRow - 2][playerCol] = 2; // 移动箱子
map[playerRow - 1][playerCol] = 0; // 移动玩家
playerRow--;
}
}
break;
case 's': // 下移
if (map[playerRow + 1][playerCol] == 0) { // 判断下方是否是空地
playerRow++;
} else if (map[playerRow + 1][playerCol] == 2) { // 判断下方是否是箱子
if (map[playerRow + 2][playerCol] == 0) { // 判断箱子下方是否是空地
map[playerRow + 2][playerCol] = 2; // 移动箱子
map[playerRow + 1][playerCol] = 0; // 移动玩家
playerRow++;
}
}
break;
case 'a': // 左移
if (map[playerRow][playerCol - 1] == 0) { // 判断左方是否是空地
playerCol--;
} else if (map[playerRow][playerCol - 1] == 2) { // 判断左方是否是箱子
if (map[playerRow][playerCol - 2] == 0) { // 判断箱子左方是否是空地
map[playerRow][playerCol - 2] = 2; // 移动箱子
map[playerRow][playerCol - 1] = 0; // 移动玩家
playerCol--;
}
}
break;
case 'd': // 右移
if (map[playerRow][playerCol + 1] == 0) { // 判断右方是否是空地
playerCol++;
} else if (map[playerRow][playerCol + 1] == 2) { // 判断右方是否是箱子
if (map[playerRow][playerCol + 2] == 0) { // 判断箱子右方是否是空地
map[playerRow][playerCol + 2] = 2; // 移动箱子
map[playerRow][playerCol + 1] = 0; // 移动玩家
playerCol++;
}
}
break;
case 'q': // 退出游戏
exit(0);
}
draw(); // 绘制地图
}
return 0;
}
```
代码注释:
1. `#include <stdio.h>`:标准输入输出库,包含了一些常用的函数,如 `printf`。
2. `#include <stdlib.h>`:标准库,包含了一些通用的函数,如 `system`。
3. `#include <conio.h>`:控制台输入输出库,包含了一些控制台相关的函数,如 `getch`。
4. `#define ROW 10`:预编译指令,定义了行数为10。
5. `#define COL 10`:预编译指令,定义了列数为10。
6. `int map[ROW][COL] = {...}`:定义了一个二维数组,表示地图。1 表示墙,0 表示空地,2 表示箱子。
7. `int playerRow = 1;`:定义了玩家所在的行数为1。
8. `int playerCol = 1;`:定义了玩家所在的列数为1。
9. `void draw() {...}`:绘制地图的函数。
10. `int main() {...}`:主函数。
11. `draw()`:绘制地图。
12. `char input`:定义了一个字符变量,用于存储用户输入。
13. `while (1) {...}`:无限循环,直到用户退出游戏。
14. `input = getch()`:获取用户输入。
15. `switch (input) {...}`:根据用户输入进行相应的操作。
16. `case 'w': {...}`:用户按下了上箭头键。
17. `case 's': {...}`:用户按下了下箭头键。
18. `case 'a': {...}`:用户按下了左箭头键。
19. `case 'd': {...}`:用户按下了右箭头键。
20. `case 'q': {...}`:用户按下了Q键,退出游戏。
21. `draw()`:绘制地图。
运行原理及思路:
推箱子游戏的运行原理比较简单,就是根据用户的输入来移动玩家和箱子,然后重新绘制地图。具体步骤如下:
1. 定义地图,包括墙、空地和箱子。
2. 绘制地图,包括墙、空地、玩家和箱子。
3. 获取用户输入。
4. 根据用户输入移动玩家和箱子,并判断是否胜利。
5. 重新绘制地图。
6. 重复步骤3-5,直到用户退出游戏。
移动的控制可以通过监听键盘输入,根据不同的按键进行不同的操作。比如按下上箭头键,就将玩家向上移动,如果上方是箱子,就判断箱子上方是否是空地,如果是,就将箱子和玩家向上移动。移动之后,重新绘制地图。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](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)