前言

我是三天打鱼两天晒网的闲人,今天跟着课程视频学习c++的数组的运用.

准备好游戏用到的图片资源

代码逻辑实现

#include
#include
#include
#include
using namespace std;
//设置画布大小
#define SCREEN_WIDTH 960
#define SCREEN_HEIGHT 768
//设置图片大小
#define RATIO 61
//设置地图大小
#define LINE 9
#define COLUMN 12
//设置起始位置
#define START_X 120
#define START_Y 110
//宏定义控健 上 下 左 右,'q'退出
#define KEY_UP	'w'
#define KEY_LEFT	'a'
#define KEY_RIGHT	'd'
#define KEY_DOWN    's'
#define KEY_QUIT    'q'
//小人的结构体
typedef struct _POS _POS;
//定义道具枚举值
typedef enum _PROPS _PROPS;
//定义游戏控健枚举值
typedef enum _DIRETION _DIRETION;
//小人移动目标位置是否合法
#define isVald(pos) pos.x>=0 && pos.x < LINE && pos.y >= 0 && pos.y < COLUMN
//小人的结构体
struct _POS {
int x;//小人所在二维数组的行
int y;//小人所在二维数组的列
};
//定义道具枚举值
enum _PROPS {
WALL,//0是墙
FLOOR,//1是地板
BOX_DES,//2是箱子目的地
MAN,//3是人
BOX,//4是箱子
HIT,//5是箱子命中目的地
ALL
};
//定义游戏控健枚举值
enum _DIRETION {
UP,
DOWN,
LEFT,
RIGHT
};
//设置图片数组
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, 0, 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, 0, 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 },
};
bool isGameOver() {
for (int i = 0; i < LINE; i++) {
for (int j = 0; j < COLUMN; j++) {
if (map[i][j] == BOX_DES) {
return false;
}
}
}
return true;
}
void gameOverScene(IMAGE *bg) {
putimage(0, 0, bg);
settextcolor(YELLOW);
RECT res = { 0,0,SCREEN_WIDTH,SCREEN_HEIGHT };
setbkmode(TRANSPARENT);
settextstyle(50, 0, _T("宋体"));
drawtext(_T("ヾ( ̄ー ̄)X(^▽^)ゞ"),&res,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
//改变展示道具
void changeMan(_POS *pos, _PROPS prop) {
//修改地图值
map[pos->x][pos->y] = prop;
//修改道具图片
putimage(pos->y * RATIO + START_X, pos->x * RATIO + START_Y, &images[prop]);
}
//上一次的目的位置是不是箱子目的地
bool isBoxDesc = false;
//实现游戏控健移动
void gameControl(_DIRETION direct) {
struct _POS nest_pos = man;
struct _POS nest_nest_pos = man;
switch (direct) {
case UP:
nest_pos.x--;
nest_nest_pos.x -= 2;
break;
case DOWN:
nest_pos.x++;
nest_nest_pos.x += 2;
break;
case LEFT:
nest_pos.y--;
nest_nest_pos.y -= 2;
break;
case RIGHT:
nest_pos.y++;
nest_nest_pos.y += 2;
break;
default:
break;
}
if (isVald(nest_pos) &&(map[nest_pos.x][nest_pos.y] == FLOOR || map[nest_pos.x][nest_pos.y] == BOX_DES)) {//人的前方是地板或者箱子目的地
//如果上一次的目标位置是箱子目的地,那么小人移走后的位置要变回箱子目的地
enum _PROPS old_props = isBoxDesc ? BOX_DES : FLOOR;
//记录目标位置是不是箱子目的地
isBoxDesc = map[nest_pos.x][nest_pos.y] == BOX_DES ? true : false;
changeMan(&nest_pos, MAN);//小人移动一格
changeMan(&man, old_props);//小人原来的位置变成地板
man = nest_pos;
} else if (isVald(nest_nest_pos) && (map[nest_pos.x][nest_pos.y] == BOX)) {//人的前方是箱子
//两种情况,箱子前面是地板或者箱子前面是目的地
if (map[nest_nest_pos.x][nest_nest_pos.y] == FLOOR) {
changeMan(&nest_nest_pos, BOX);//箱子移动一格
changeMan(&nest_pos, MAN);//小人移动一格
changeMan(&man, FLOOR);//小人原来的位置变成地板
man = nest_pos;
} else if (map[nest_nest_pos.x][nest_nest_pos.y] == BOX_DES) {
changeMan(&nest_nest_pos, HIT);//箱子移动一格
changeMan(&nest_pos, MAN);//小人移动一格
changeMan(&man, FLOOR);//小人原来的位置变成地板
man = nest_pos;
}
}
}
int main(void) {
IMAGE bg_image;
IMAGE over_image;
//初始化画布大小
initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
//设置背景
loadimage(&bg_image, _T("image/blackground.bmp"), 960, 768, true);
putimage(0, 0, &bg_image);
loadimage(&over_image, _T("image/3.jpg"), 960, 768, true);
//加载道具图片
loadimage(&images[WALL], _T("image/wall_right.bmp"), RATIO, RATIO, true);
loadimage(&images[FLOOR], _T("image/floor.bmp"), RATIO, RATIO, true);
loadimage(&images[BOX_DES], _T("image/des.bmp"), RATIO, RATIO, true);
loadimage(&images[MAN], _T("image/man.bmp"), RATIO, RATIO, true);
loadimage(&images[BOX], _T("image/box.bmp"), RATIO, RATIO, true);
loadimage(&images[HIT], _T("image/box.bmp"), RATIO, RATIO, true);
for (int i = 0; i < LINE; i++) {
for (int j = 0; j < COLUMN; j++) {
if (map[i][j] == MAN) {
//值为3的就是小人的位置
man.x = i;
man.y = j;
}
putimage(j * RATIO + START_X, i * RATIO + START_Y, &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;
}
if (isGameOver()) {
gameOverScene(&over_image);
//quit = true;
}
}
Sleep(100);
} while (!quit);
system("pause");
//释放资源
closegraph();
return 0;
}

效果展示

C++数组实现推箱子游戏插图
C++数组实现推箱子游戏插图(1)

[2024-06-04]

本站无任何商业行为
个人在线分享 » C++数组实现推箱子游戏
E-->