项目需求:
实现一款推箱子游戏,效果如下图所示 , 具体规则:
1. 箱子只能推动而不能拉动;
2. 如果箱子前一格是地板或箱子目的地,则可以推动一个箱子往前走一格,如果箱子已经在
箱子目的地则不能再推动
3.推箱子的小人不能从箱子目的地上直接穿过
4. 注意不要把箱子推到死角上,不然就无法再推动它了;
5. 所有箱子都成功推到箱子目的地,游戏结束,过关成功!
图形库 : www.easyx.cn
————#include<graphics.h> #include<iostream> #include<stdlib.h> #include<string> #include<conio.h> using namespace std; #define RATIO 61 #define SCREEN_WIDTH 960 //画布宽度 列 #define SCREEN_HEIGHT 768 //画布高度 行 //控制键 上、下、左、右 控制方向,'q' 退出 #define KEY_UP 'w' //char 'a' #define KEY_LEFT 'a' #define KEY_RIGHT 'd' #define KEY_DOWN 's' #define KEY_QUIT 'q' #define START_X 100 #define START_y 150 #define isValid(next_pos) next_pos.x >= 0 && next_pos.x < LINE && next_pos.y >= 0 && next_pos.x < COLUMU #define LINE 9 #define COLUMU 12 IMAGE images[6]; enum _PROPS { WALL,//墙 FLOOR,//地板 BOX_DES,//箱子目的地 MAN,//小人 BOX,//箱子 HIT,//箱子命中目标 ALL }; //游戏地图 int map[LINE][COLUMU] = { { 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 }, }; /************************************************* * 判断游戏是否结束,如果不存在箱子目的地,代表游戏结束 * 输入: 无 * 返回值: * true - 游戏结束 false 游戏继续 ***************************************************/ bool isGameOver() { for (int i = 0; i < LINE; i++) { for (int j = 0; j < COLUMU; j++) { if (map[i][j] == BOX_DES) { return false; } } } return true; } /************************************************* * 游戏结束 * 输入: * bg - 背景图片变量的指针 * 返回值: 无 ***************************************************/ void gameOver(IMAGE* bg) { putimage(0, 0, bg); settextcolor(WHITE); RECT rec = { 0,0,SCREEN_WIDTH,SCREEN_HEIGHT }; settextstyle(20, 0, _T("宋体")); drawtext(_T("恭喜通关"), &rec, DT_CENTER | DT_VCENTER | DT_SINGLELINE); } struct _POS { int x;//小人所在二维数据的行 int y;//小人所在二维数据的列 }; struct _POS man; /********************************************** *改变游戏地图视图中一格对应道具并重新显示 * 输入: * line——道具在地图数组的行下标 * Column——道具在地图数组的列下标 * prop——道具的类型 * 返回值 无 **********************************************/ void changeMap(struct _POS*pos,enum _PROPS prop) { map[pos->x][pos->y] = prop; putimage(START_X + pos->y * RATIO, START_y + pos->x * RATIO, &images[prop]); } //控制方向 enum _DIRECTION { UP, DOWN, LEFT, RIGHT }; /********************************************** *实现游戏四个方向(上、下、左、右)的控制 * 输入: * direct - 人前进方向 * 输出: 无 **********************************************/ void gameControl(enum _DIRECTION direct) { struct _POS next_pos=man; struct _POS next_next_pos = man; switch (direct) { case UP: next_pos.x--; next_next_pos.x -= 2; break; case DOWN: next_pos.x++; next_next_pos.x += 2; break; case LEFT: next_pos.y--; next_next_pos.y -= 2; break; case RIGHT: next_pos.y++; next_next_pos.y += 2; break; } //人的前方是地板 if (isValid(next_pos) && map[next_pos.x][next_pos.y] == FLOOR) { changeMap(&next_pos, MAN);//小人前进一格 changeMap(&man, FLOOR); man = next_pos; } //人的前方是箱子 else if (isValid(next_next_pos) && map[next_pos.x][next_pos.y] == BOX) { if (map[next_next_pos.x][next_next_pos.y] == FLOOR) { changeMap(&next_next_pos, BOX); changeMap(&next_pos, MAN);//小人前进一格 changeMap(&man, FLOOR); man = next_pos; } else if (map[next_next_pos.x][next_next_pos.y] == BOX_DES) { changeMap(&next_next_pos, HIT); changeMap(&next_pos, MAN);//小人前进一格 changeMap(&man, FLOOR); man = next_pos; } } } int main(void) { IMAGE big_img; //搭台唱戏 initgraph(SCREEN_WIDTH, SCREEN_HEIGHT); loadimage(&big_img,_T("blackground.bmp"),960,768,true); putimage(0, 0, &big_img); //加载道具图标 loadimage(&images[WALL], _T("wall_right.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 < COLUMU; j++) { putimage(START_X + j * RATIO, START_y + i * RATIO, &images[map[i][j]]); //在控制方向时,我们需要获得小人的当前位置 if (map[i][j] == MAN) { //小人的属性有2个,X 和 Y 坐标控制方向,需要结构体,其他方式不好控制,定义结构体并赋值 man.x= i; //小人 坐标x man.y = j; //小人 坐标y } } } //游戏环节 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()) { gameOver(&big_img); quit = true; } } } while (quit == false);//!quit system("pause"); closegraph(); return 0; }
————————————
C++推箱子游戏
最新推荐文章于 2023-08-23 20:55:14 发布