c语言推箱子代码源250行
时间: 2023-10-30 18:05:08 浏览: 67
推箱子C语言源代码
5星 · 资源好评率100%
以下是一个简单的 C 语言推箱子游戏的代码,共计约 250 行:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define WIDTH 20
#define HEIGHT 10
#define MAX_LEVEL 5
const char MAP_WALL = '#';
const char MAP_FLOOR = ' ';
const char MAP_BOX = '$';
const char MAP_DEST = '.';
const char MAP_PLAYER = '@';
int level = 1;
char map[MAX_LEVEL][HEIGHT][WIDTH + 1] = {
{
"###################",
"#@ #",
"# $ #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"###################",
},
{
"###################",
"# #",
"# #",
"# $ #",
"# #",
"# #",
"# #",
"# @ #",
"# #",
"###################",
},
{
"###################",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# $ #",
"# @ #",
"###################",
},
{
"###################",
"# #",
"# #",
"# $ #",
"# #",
"# #",
"# #",
"# @ #",
"# #",
"###################",
},
{
"###################",
"# #",
"# #",
"# #",
"# #",
"# #",
"# $ #",
"# @ #",
"# #",
"###################",
},
};
int playerX, playerY;
void drawMap() {
system("cls");
printf("Level %d:\n", level);
for (int i = 0; i < HEIGHT; i++) {
printf("%s\n", map[level - 1][i]);
}
}
void move(int dx, int dy) {
int newX = playerX + dx;
int newY = playerY + dy;
if (map[level - 1][newY][newX] == MAP_WALL) {
return;
}
if (map[level - 1][newY][newX] == MAP_BOX) {
int newBoxX = newX + dx;
int newBoxY = newY + dy;
if (map[level - 1][newBoxY][newBoxX] == MAP_WALL
|| map[level - 1][newBoxY][newBoxX] == MAP_BOX) {
return;
}
map[level - 1][newBoxY][newBoxX] = MAP_BOX;
}
map[level - 1][playerY][playerX] = MAP_FLOOR;
map[level - 1][newY][newX] = MAP_PLAYER;
playerX = newX;
playerY = newY;
drawMap();
}
int main() {
drawMap();
while (1) {
int key = getch();
switch (key) {
case 'w': move(0, -1); break;
case 'a': move(-1, 0); break;
case 's': move(0, 1); break;
case 'd': move(1, 0); break;
case 'r':
drawMap();
break;
case 'n':
if (level < MAX_LEVEL) {
level++;
playerX = playerY = -1;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (map[level - 1][i][j] == MAP_PLAYER) {
playerX = j;
playerY = i;
break;
}
}
if (playerX >= 0) {
break;
}
}
drawMap();
}
break;
case 'p':
if (level > 1) {
level--;
playerX = playerY = -1;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (map[level - 1][i][j] == MAP_PLAYER) {
playerX = j;
playerY = i;
break;
}
}
if (playerX >= 0) {
break;
}
}
drawMap();
}
break;
case 'q':
exit(0);
}
int boxCount = 0, destCount = 0;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (map[level - 1][i][j] == MAP_BOX) {
boxCount++;
}
if (map[level - 1][i][j] == MAP_DEST) {
destCount++;
}
}
}
if (boxCount == 0) {
if (level == MAX_LEVEL) {
printf("Congratulations! You have completed all levels!\n");
exit(0);
} else {
printf("Congratulations! You have completed level %d!\n", level);
level++;
playerX = playerY = -1;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (map[level - 1][i][j] == MAP_PLAYER) {
playerX = j;
playerY = i;
break;
}
}
if (playerX >= 0) {
break;
}
}
drawMap();
}
}
}
return 0;
}
```
这个游戏使用了简单的 ASCII 字符表示地图,玩家可以通过输入 WASD 控制角色移动,将箱子推到指定位置即可过关。该代码仅供参考,可以根据自己的需求进行修改和优化。
阅读全文