推箱子游戏的实现
开发环境的搭建
- 因为我的这里使用的是vs2019所以就用2019来演示
- 首先百度首页搜索easyx
- 找到官网
- 下载好之后直接打开安装就可以了
- 对照自己的版本直接安装就可以了
这样开发环境就搭建好了
游戏实现
#include
#include
#include
#include
#include
using namespace std;
#define RATIO 61
#define SCREEN_WIDTH 960
#define SCREEN_HEIGHT 768
#define KEY_UP 'w'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#define KEY_DOWN 's'
#define KEY_QUIT 'q'
#define LINE 9
#define COLUMN 12
#define START_X 100
#define START_Y 150
enum _PROPS {
WALL,
FLOOR,
BOX_DES,
MAN,
BOX,
HIT,
ALL
};
enum _DIRECTION
{
UP,
DOWN,
LEFT,
RIGHT
};
struct _POS {
int x;
int y;
};
IMAGE images[ALL];
struct _POS man;
int map[LINE][COLUMN] =
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
{ 0, 1, 4, 1, 0, 2, 1, 0, 2, 1, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0 },
{ 0, 1, 0, 2, 0, 1, 1, 4, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 3, 1, 1, 1, 4, 1, 0 },
{ 0, 1, 2, 1, 1, 4, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
void changeMap(int line, int column, enum _PROPS prop) {
map[line][column] = prop;
putimage(START_X + column * RATIO, START_Y + line * RATIO, &images[prop]);
}
void gameControl(enum _DIRECTION direct) {
int x = man.x;
int y = man.y;
if (direct = UP) {
if ((x - 1) >= 0 && map[x - 1][y] == FLOOR) {
changeMap(x - 1, y, MAN);
man.x = x - 1;
changeMap(x, y, FLOOR);
}
}
else if (direct == DOWN) {
if ((x + 1) < LINE && map[x + 1][y] == FLOOR) {
changeMap(x + 1, y, MAN);
man.x = x + 1;
changeMap(x, y, FLOOR);
}
}
else if (direct == LEFT) {
if ((y - 1) >= 0 && map[x][y - 1] == FLOOR) {
changeMap(x, y - 1, MAN);
man.y = y - 1;
changeMap(x, y, FLOOR);
}
}
else if (direct == RIGHT) {
if ((y + 1) < COLUMN && map[x][y + 1] == FLOOR) {
changeMap(x, y + 1, MAN);
man.y = y + 1;
changeMap(x, y, FLOOR);
}
}
}
int main(void) {
IMAGE bg_img;
initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
loadimage(&bg_img, _T("blackground.bmp"), SCREEN_WIDTH, SCREEN_HEIGHT, true);
putimage(0, 0, &bg_img);
loadimage(&images[WALL], _T("wall.bmp"), RATIO, RATIO, true);
loadimage(&images[FLOOR], _T("floor.bmp"), RATIO, RATIO, true);
loadimage(&images[BOX_DES], _T("des.bmp"), RATIO, RATIO, true);
loadimage(&images[MAN], _T("man.bmp"), RATIO, RATIO, true);
loadimage(&images[BOX], _T("box.bmp"), RATIO, RATIO, true);
loadimage(&images[HIT], _T("box.bmp"), RATIO, RATIO, true);
for (int i = 0; i < LINE; i++) {
for (int j = 0; j < COLUMN; j++) {
if (map[i][j] == MAN) {
man.x = i;
man.y = j;
}
putimage(START_X + j * RATIO, START_Y + i * RATIO, &images[map[i][j]]);
}
}
bool quit = false;
do {
if (_kbhit()) {
char ch = _getch();
if (ch == KEY_UP) {
gameControl(UP);
}
else if (ch == KEY_DOWN) {
gameControl(DOWN);
}
else if (ch == KEY_LEFT) {
gameControl(LEFT);
}
else if (ch == KEY_RIGHT) {
gameControl(RIGHT);
}
else if (ch == KEY_QUIT) {
quit = true;
}
}
Sleep(100);
} while (quit = false);
system("pause");
return 0;
}