用c++ 写一个推箱子小游戏
时间: 2023-06-11 19:06:14 浏览: 134
推箱子小游戏是一个比较经典的游戏,以下是一个简单的推箱子小游戏 C 代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define ROW 10
#define COLUMN 10
#define WALL '#' //墙
#define MAN 'M' //人
#define BOX '$' //箱子
#define DEST 'D' //目的地
#define SPACE ' ' //空地
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
int map[ROW][COLUMN] = {
{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}
};
void display() {
int i,j;
system("cls"); //清空屏幕
for (i = 0; i < ROW; i++) {
for (j = 0; j < COLUMN; j++) {
switch (map[i][j]) {
case 1: printf("%c", WALL); break;
case 2: printf("%c", MAN); break;
case 3: printf("%c", BOX); break;
case 4: printf("%c", DEST); break;
case 0: printf("%c", SPACE); break;
default: break;
}
}
printf("\n");
}
printf("Use Arrow keys to move the man.\n");
printf("Press Q to quit.");
}
void move_man(int x, int y) {
if (map[x][y] == 0 || map[x][y] == 4) {
map[x][y] = 2;
map[x+1][y] = 0;
}
}
void move_box(int x, int y, int dx, int dy) {
if (map[x][y] == 3 && map[dx][dy] == 0) {
map[x][y] = 2;
map[dx][dy] = 3;
map[x+1][y] = 0;
}
}
int check_win() {
int i,j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COLUMN; j++) {
if (map[i][j] == 3) {
return 0;
}
}
}
return 1;
}
int main() {
int man_x = 1, man_y = 1;
int c = 0;
display();
while (1) {
c = getch();
switch (c) {
case UP: {
if (map[man_x-1][man_y] == 0 || map[man_x-1][man_y] == 4) {
move_man(man_x-1, man_y);
man_x--;
} else if (map[man_x-1][man_y] == 3 && (map[man_x-2][man_y] == 0 || map[man_x-2][man_y] == 4)) {
move_box(man_x-1, man_y, man_x-2, man_y);
move_man(man_x-1, man_y);
man_x--;
}
break;
}
case DOWN: {
if (map[man_x+1][man_y] == 0 || map[man_x+1][man_y] == 4) {
move_man(man_x+1, man_y);
man_x++;
} else if (map[man_x+1][man_y] == 3 && (map[man_x+2][man_y] == 0 || map[man_x+2][man_y] == 4)) {
move_box(man_x+1, man_y, man_x+2, man_y);
move_man(man_x+1, man_y);
man_x++;
}
break;
}
case LEFT: {
if (map[man_x][man_y-1] == 0 || map[man_x][man_y-1] == 4) {
move_man(man_x, man_y-1);
man_y--;
} else if (map[man_x][man_y-1] == 3 && (map[man_x][man_y-2] == 0 || map[man_x][man_y-2] == 4)) {
move_box(man_x, man_y-1, man_x, man_y-2);
move_man(man_x, man_y-1);
man_y--;
}
break;
}
case RIGHT: {
if (map[man_x][man_y+1] == 0 || map[man_x][man_y+1] == 4) {
move_man(man_x, man_y+1);
man_y++;
} else if (map[man_x][man_y+1] == 3 && (map[man_x][man_y+2] == 0 || map[man_x][man_y+2] == 4)) {
move_box(man_x, man_y+1, man_x, man_y+2);
move_man(man_x, man_y+1);
man_y++;
}
break;
}
case 'q':
case 'Q': {
exit(0);
break;
}
default: break;
}
if (check_win()) {
printf("You Win!\n");
getch(); //等待用户输入
exit(0);
}
display();
}
return 0;
}
```
这个游戏代码比较简单,主要实现了人物移动和箱子移动的功能。游戏地图用二维数组存储,1表示墙,0表示空地,2表示人物,3表示箱子,4表示目的地。通过 getch() 函数获取用户输入,根据用户输入来移动人物和箱子,每次移动后都重新绘制地图。最后,判断是否达到胜利条件,如果是则输出胜利信息。
阅读全文