• C++学习day--24 推箱子游戏图像化开发


    环境要求:

    1、VS2015以上

    2、成功安装并配置图形库

    项目注意事项:代码复制好以后,把下面的字符集改为多字节字符集

    1 节 项目需求

    实现一款推箱子游戏,效果如下图所示 , 具体规则:
    1. 箱子只能推动而不能拉动;
    2. 如果箱子前一格是地板或箱子目的地,则可以推动一个箱子往前走一格,如果箱子已经在
    箱子目的地则不能再推动;
    3. 推箱子的小人不能从箱子目的地上直接穿过;
    4. 注意不要把箱子推到死角上,不然就无法再推动它了;
    5. 所有箱子都成功推到箱子目的地,游戏结束,过关成功!

    2 项目实现

    2.1 模块划分

    作用: 1. 化繁为简 2. 适合团队协作 3. 高质量代码)

    2.1.1 地图初始化

    搭台唱戏
    戏台坐标系 (650 x 650)

    地图表示:

     

    使用二维数组
    1、游戏道具显示(墙、箱子、箱子目的地、小人、地板)
    2、便于程序在游戏过程中进行逻辑判断和控制小人向前一步的动作控制
    3、 判断游戏结果
    道具表示:
    : 0 ,地板 : 1 ,箱子目的地 : 2, 小人 : 3, 箱子 : 4, 箱子命中目标 : 5
    编码实现:
    1. #include <graphics.h>
    2. #include <iostream>
    3. #include <stdlib.h>
    4. #include <string>
    5. using namespace std;
    6. #define RATIO 61
    7. #define SCREEN_WIDTH 960
    8. #define SCREEN_HEIGHT 768
    9. #define LINE 9
    10. #define COLUMN 12
    11. #define START_X 100
    12. #define START_Y 150
    13. IMAGE images[6];
    14. /*游戏地图*/
    15. int map[LINE][COLUMN] = {
    16. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    17. { 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
    18. { 0, 1, 4, 1, 0, 2, 1, 0, 2, 1, 0, 0 },
    19. { 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0 },
    20. { 0, 1, 0, 2, 0, 1, 1, 4, 1, 1, 1, 0 },
    21. { 0, 1, 1, 1, 0, 3, 1, 1, 1, 4, 1, 0 },
    22. { 0, 1, 2, 1, 1, 4, 1, 1, 1, 1, 1, 0 },
    23. { 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0 },
    24. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    25. };
    26. int main(void) {
    27. IMAGE bg_img;
    28. //搭台唱戏
    29. initgraph(SCREEN_WIDTH, SCREEN_HEIGHT);
    30. loadimage(&bg_img, _T("blackground.bmp"), SCREEN_WIDTH,
    31. SCREEN_HEIGHT, true);
    32. putimage(0, 0, &bg_img);
    33. //加载道具图标
    34. loadimage(&images[0], _T("wall.bmp"), RATIO, RATIO, true);
    35. loadimage(&images[1], _T("floor.bmp"), RATIO, RATIO, true);
    36. loadimage(&images[2], _T("des.bmp"), RATIO, RATIO, true);
    37. loadimage(&images[3], _T("man.bmp"), RATIO, RATIO, true);
    38. loadimage(&images[4], _T("box.bmp"), RATIO, RATIO, true);
    39. loadimage(&images[5], _T("box.bmp"), RATIO, RATIO, true);
    40. for (int i = 0; i < LINE; i++) {
    41. for (int j = 0; j < COLUMN; j++) {
    42. putimage(START_X + j * RATIO, START_Y + i * RATIO,
    43. &images[map[i][j]]);
    44. }
    45. }
    46. system("pause");
    47. return 0;
    48. }

    2.1.2 热键控制

    热键定义:

    => a
    => s
    => w
    => d
    退出 => q
    1. #include <conio.h>
    2. //控制键 上、下、左、右 控制方向,'q' 退出
    3. #define KEY_UP 'w' //char 'a'
    4. #define KEY_LEFT 'a'
    5. #define KEY_RIGHT 'd'
    6. #define KEY_DOWN 's'
    7. #define KEY_QUIT 'q'
    8. //游戏环节
    9. bool quit = false;
    10. do {
    11. if (_kbhit()) { //玩家按键
    12. char ch = _getch();
    13. if (ch == KEY_UP) {
    14. gameControl(UP);
    15. }
    16. else if (ch == KEY_DOWN) {
    17. gameControl(DOWN);
    18. }
    19. else if (ch == KEY_LEFT) {
    20. gameControl(LEFT);
    21. }
    22. else if (ch == KEY_RIGHT) {
    23. gameControl(RIGHT);
    24. }
    25. else if (ch == KEY_QUIT) {
    26. quit = true;
    27. }
    28. }
    29. Sleep(100);
    30. } while (quit == false); //!quit

    2.1.3 推箱子控制

    1. /**********************************************
    2. *实现游戏四个方向(上、下、左、右)的控制
    3. * 输入:
    4. * direct - 人前进方向
    5. * 输出: 无
    6. **********************************************/
    7. void gameControl(enum _DIRECTION direct) {
    8. struct _POS next_pos = man;
    9. struct _POS next_next_pos = man;
    10. switch (direct) {
    11. case UP:
    12. next_pos.x--;
    13. next_next_pos.x -= 2;
    14. break;
    15. case DOWN:
    16. next_pos.x++;
    17. next_next_pos.x += 2;
    18. break;
    19. case LEFT:
    20. next_pos.y--;
    21. next_next_pos.y -= 2;
    22. break;
    23. case RIGHT:
    24. next_pos.y++;
    25. next_next_pos.y += 2;
    26. break;
    27. }
    28. //宏展开 next_pos.x>=0 && next_pos.x<LINE && next_pos.y>=0 &&
    29. next_pos.y < COLUMN
    30. if (isValid(next_pos) && map[next_pos.x][next_pos.y] == FLOOR) {//人的前方是地板
    31. changeMap(&next_pos, MAN); //小人前进一格
    32. changeMap(&man, FLOOR);
    33. man = next_pos;
    34. }
    35. else if (isValid(next_next_pos) && map[next_pos.x][next_pos.y] ==
    36. BOX) {//人的前方是箱子
    37. //两种情况,箱子前面是地板或者是箱子目的地
    38. if (map[next_next_pos.x][next_next_pos.y] == FLOOR) {
    39. changeMap(&next_next_pos, BOX);
    40. changeMap(&next_pos, MAN); //小人前进一格
    41. changeMap(&man, FLOOR);
    42. man = next_pos;
    43. }
    44. else if (map[next_next_pos.x][next_next_pos.y] == BOX_DES) {
    45. changeMap(&next_next_pos, HIT);
    46. changeMap(&next_pos, MAN); //小人前进一格
    47. changeMap(&man, FLOOR);
    48. man = next_pos;
    49. }
    50. }
    51. }

    2.1.4 游戏结束

    1. /**********************************************
    2. *判断游戏是否结束,如果不存在任何一个箱子目的地,就代表游戏结束
    3. *输入: 无
    4. *返回值:
    5. * true - 游戏结束 false - 游戏继续
    6. **********************************************/
    7. bool isGameOver() {
    8. for (int i = 0; i < LINE; i++) {
    9. for (int j = 0; j < COLUMN; j++) {
    10. if (map[i][j] == BOX_DES) return false;
    11. }
    12. }
    13. return true;
    14. }
    15. /**********************************************
    16. *游戏结束场景,在玩家通关后显示
    17. *输入:
    18. * bg - 背景图片变量的指针
    19. *返回值: 无
    20. **********************************************/
    21. void gameOverScene(IMAGE* bg) {
    22. putimage(0, 0, bg);
    23. settextcolor(WHITE);
    24. RECT rec = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
    25. settextstyle(20, 0, _T("宋体"));
    26. drawtext(_T("恭喜您~ \n 您终于成为了一个合格的搬箱子老司机!"),&rec, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    27. }
    28. //main 函数中
    29. if (isGameOver()) {
    30. gameOverScene(&bg_img);
    31. quit = true;
    32. }

     3、完整代码实现:

    box_main.cpp:

    1. #pragma once
    2. #include"box_man.h"
    3. int main() {
    4. initgraph(WEIGHT, HEIGHT);
    5. IMAGE bg_img;
    6. //参数分别表示图片变量的地址,图片的路径,图片的高度,宽度,是否要拉伸
    7. //用_T()是为了进行编码转换,也可以更改项目属性——高级——多字符集
    8. loadimage(&bg_img, _T("blackground.bmp"), WEIGHT, HEIGHT, true);//加载图片到内存
    9. putimage(0, 0, &bg_img);//把图片显示到窗口,参数分别表示坐标x,坐标y(从左上角开始计算),图片变量地址
    10. //加载道具
    11. loadimage(&images[WALL], _T("wall_right.bmp"), RATIO, RATIO, true);//
    12. loadimage(&images[FLOOR], _T("floor.bmp"), RATIO, RATIO, true);//地板
    13. loadimage(&images[BOX_DES], _T("des.bmp"), RATIO, RATIO, true);//目的地
    14. loadimage(&images[MAN], _T("man.jpg"), RATIO, RATIO, true);//小人
    15. loadimage(&images[BOX], _T("box.jpg"), RATIO, RATIO, true);//箱子
    16. loadimage(&images[HIT], _T("des.jpg"), RATIO, RATIO, true);//目标命中,地图刚开始时没有数字5的,因此这个到后面判断游戏才发挥作用
    17. //加载到地图上
    18. for (int i = 0; i < LINE; i++) {
    19. for (int j = 0; j < COLUMN; j++) {
    20. if (map1[i][j] == MAN) {//找到小人的位置
    21. man.x = i;
    22. man.y = j;
    23. }
    24. putimage(WEIGHT_BIAS + j * RATIO, HEIGHT_BIAS + i * RATIO, &images[map1[i][j]]);//核心代码段!!这个代码非常漂亮,利用行列坐标直接求出图片的位置
    25. }
    26. }
    27. //进入游戏环节
    28. bool quit = false;
    29. do
    30. {
    31. if (_kbhit()) //玩家有敲击键盘的操作
    32. {
    33. char ch = _getch(); //获取敲击的热键
    34. switch (ch)
    35. {
    36. case 'w':
    37. gameControl(UP);
    38. break;
    39. case 'a':
    40. gameControl(LEFT);
    41. break;
    42. case 's':
    43. gameControl(DOWN);
    44. break;
    45. case 'd':
    46. gameControl(RIGHT);
    47. break;
    48. case 'q':
    49. quit = true;
    50. break;
    51. default:
    52. break;
    53. }
    54. Sleep(100); //因为一直在循环,一直在消耗CPU,休眠可以节约CPU资源
    55. if (isGameOver()) { //游戏结束
    56. GameOverField(&bg_img);
    57. quit = true;
    58. }
    59. }
    60. } while (quit == false);
    61. //游戏结束
    62. system("pause");
    63. closegraph();
    64. return 0;
    65. }

    function.cpp:

    1. #include<iostream>
    2. #include<iostream>
    3. #include<Windows.h>
    4. #include<graphics.h>
    5. #include<stdlib.h>
    6. #include<string>
    7. #include<conio.h> //获取键盘的热键
    8. #include<mmsystem.h>//音乐头文件
    9. #pragma comment(lib,"winmm.lib")//支持音乐播放的库文件
    10. using namespace std;
    11. #define WEIGHT 1190 //游戏窗口的宽度
    12. #define HEIGHT 800 //游戏窗口的高度
    13. #define LINE 9 //地图的行数
    14. #define COLUMN 12 //地图的列数
    15. #define KEY_UP 'w' //上键
    16. #define KEY_LEFT 'a' //左键
    17. #define KEY_DOWN 's' //下键
    18. #define KEY_RIGHT 'd' //右键
    19. #define KEY_QUIT 'q' //退出
    20. #define WEIGHT_BIAS 300 //地图离整个画布的横向偏移
    21. #define HEIGHT_BIAS 150 //地图离整个画布的纵向偏移
    22. #define RATIO 61 //小人物、强、箱子等元素的长和宽
    23. //把判断条件用宏来替换
    24. #define isValid(pos) pos.x >= 0 && pos.x < LINE && pos.y >= 0 && pos.y < COLUMN && map1[pos.x][pos.y]
    25. //小人所在二维数组的位置,行数和列数
    26. struct _POS {//很多开源项目结构体名用下划线开头
    27. int x;
    28. int y;
    29. };
    30. struct _POS man;
    31. typedef enum _DIRECTION {
    32. UP,
    33. DOWN,
    34. LEFT,
    35. RIGHT
    36. }DIRECTION;
    37. //用枚举定义道具
    38. enum _PROPS {
    39. WALL, //
    40. FLOOR, //地板
    41. BOX_DES, //目的地
    42. MAN, //小人
    43. BOX, //箱子
    44. HIT, //命中目标
    45. ALL //为什么最后还要定义一个ALL,为了定义下面images数组的长度,这是顶尖项目的经验
    46. };
    47. IMAGE images[ALL]; //存放各类道具图片变量
    48. //地图1,假设后面还要做几个关卡,那么map1表示第一关
    49. int map1[LINE][COLUMN] = {
    50. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    51. { 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
    52. { 0, 1, 4, 1, 0, 2, 1, 0, 2, 1, 0, 0 },
    53. { 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0 },
    54. { 0, 1, 0, 2, 0, 1, 1, 4, 1, 1, 1, 0 },
    55. { 0, 1, 1, 1, 0, 3, 1, 1, 1, 4, 1, 0 },
    56. { 0, 1, 2, 1, 1, 4, 1, 1, 1, 1, 1, 0 },
    57. { 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0 },
    58. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    59. };
    60. /*
    61. 小人发生移动时,改变地图中游戏的道具
    62. line 地图的行下标
    63. colum 地图的列下标
    64. pros 道具的类型
    65. */
    66. void changeMap(struct _POS* pos, enum _PROPS pros) {
    67. map1[pos->x][pos->y] = pros; //更改地图的状态
    68. putimage(WEIGHT_BIAS + pos->y * RATIO, HEIGHT_BIAS + pos->x * RATIO, &images[pros]);
    69. }
    70. /*
    71. 实现游戏四个方向的移动
    72. 这里有很大的优化空间
    73. */
    74. //版本1
    75. //void gameControl(DIRECTION direct) {
    76. // if (direct == UP) {//先处理前进方向是地板的情况
    77. // if (man.x - 1 >= 0 && map1[man.x - 1][man.y] == FLOOR) {//防御式编程,判断坐标的合法性
    78. // changeMap(man.x-1, man.y, MAN); //小人的往上走一格
    79. // changeMap(man.x, man.y, FLOOR);//小人原来的位置设置为地板
    80. // man.x = man.x - 1;//调整小人的坐标
    81. // }
    82. // }
    83. // else if (direct == DOWN) {
    84. // if (man.x + 1 < LINE && map1[man.x + 1][man.y] == FLOOR) {
    85. // changeMap(man.x + 1, man.y, MAN); //小人的往下走一格
    86. // changeMap(man.x, man.y, FLOOR);//小人原来的位置设置为地板
    87. // man.x = man.x + 1;//调整小人的坐标
    88. // }
    89. // }
    90. // else if (direct == LEFT) {
    91. // if (man.y - 1 >= 0 && map1[man.x][man.y - 1] == FLOOR) {
    92. // changeMap(man.x, man.y-1, MAN); //小人的往左走一格
    93. // changeMap(man.x, man.y, FLOOR);//小人原来的位置设置为地板
    94. // man.y = man.y - 1;//调整小人的坐标
    95. // }
    96. // }
    97. // else if (direct == RIGHT) {
    98. // if (man.y + 1 < COLUMN && map1[man.x][man.y + 1] == FLOOR) {
    99. // changeMap(man.x, man.y + 1, MAN); //小人的往右走一格
    100. // changeMap(man.x, man.y, FLOOR);//小人原来的位置设置为地板
    101. // man.y = man.y + 1;//调整小人的坐标
    102. // }
    103. // }
    104. //}
    105. //版本2,简化代码!!提高代码重用率
    106. void gameControl(DIRECTION direct) {
    107. struct _POS next_man = man;
    108. struct _POS next_box = man;
    109. switch (direct) {
    110. case UP:
    111. next_man.x--;
    112. next_box.x -= 2;
    113. break;
    114. case DOWN:
    115. next_man.x++;
    116. next_box.x += 2;
    117. break;
    118. case LEFT:
    119. next_man.y--;
    120. next_box.y -= 2;
    121. break;
    122. case RIGHT:
    123. next_man.y++;
    124. next_box.y += 2;
    125. break;
    126. }
    127. /*判断条件太长,用宏定义替换来优化*/
    128. //if (next_man.x >= 0 && next_man.x < LINE &&
    129. // next_man.y >= 0 && next_man.y < COLUMN &&
    130. // map1[next_man.x][next_man.y] == FLOOR) {//防御式编程,判断坐标的合法性
    131. // changeMap(next_man.x, next_man.y, MAN); //小人的往上走一格
    132. // changeMap(man.x, man.y, FLOOR);//小人原来的位置设置为地板
    133. // man = next_man;//调整小人的坐标
    134. //}
    135. if (isValid(next_man) && map1[next_man.x][next_man.y] == FLOOR) {//防御式编程,判断坐标的合法性
    136. //changeMap(next_man.x, next_man.y, MAN); //小人的往上走一格
    137. //changeMap(man.x, man.y, FLOOR);//小人原来的位置设置为地板
    138. //结构体传递用指针传递,提高效率,进一步优化
    139. changeMap(&next_man, MAN);
    140. changeMap(&man, FLOOR);
    141. man = next_man;//调整小人的坐标
    142. }
    143. else if (isValid(next_man) && map1[next_man.x][next_man.y] == BOX) {
    144. //第一种情况:箱子前面是地板
    145. if (isValid(next_box) && map1[next_box.x][next_box.y] == FLOOR) {
    146. changeMap(&next_box, BOX);
    147. changeMap(&next_man, MAN);
    148. changeMap(&man, FLOOR);
    149. man = next_man;
    150. }
    151. //第二种情况,前面是箱子
    152. else if (isValid(next_box) && map1[next_box.x][next_box.y] == BOX_DES) {
    153. changeMap(&next_box, HIT);
    154. changeMap(&next_man, MAN);
    155. changeMap(&man, FLOOR);
    156. man = next_man;
    157. }
    158. //第二种情况,箱子前面是目的地
    159. }
    160. }
    161. /*
    162. 判断游戏是否结束,如果地图中没有箱子目的地,说明说有箱子已经全部移动到指定位置
    163. 游戏结束,反之没有结束
    164. */
    165. bool isGameOver() {
    166. for (int i = 0; i < LINE; i++) {
    167. for (int j = 0; j < COLUMN; j++) {
    168. if (map1[i][j] == BOX_DES) {
    169. return false;
    170. }
    171. }
    172. }
    173. return true;
    174. }
    175. /*
    176. 游戏结束场景
    177. */
    178. void GameOverField(IMAGE* bg) {
    179. putimage(0, 0, bg);
    180. settextcolor(WHITE);
    181. RECT rec = { 0,0,WEIGHT,HEIGHT };
    182. settextstyle(100, 0, _T("宋体"));
    183. loadimage(NULL, _T("游戏结束背景.jpg"));//加载胜利图片
    184. //音乐播放
    185. mciSendString(_T("play 游戏结束.mp3"), 0, 0, 0);
    186. outtextxy(WEIGHT_BIAS, HEIGHT / 2, _T("恭喜你通关啦"));
    187. }

    box_man.h:

    1. #pragma once //头文件文件只包含一次
    2. #ifndef TEST_H
    3. #define TEST_H
    4. #include
    5. #include //获取键盘的热键
    6. #define WEIGHT 1190 //游戏窗口的宽度
    7. #define HEIGHT 800 //游戏窗口的高度
    8. #define LINE 9 //地图的行数
    9. #define COLUMN 12 //地图的列数
    10. #define RATIO 61 //小人物、强、箱子等元素的长和宽
    11. #define WEIGHT_BIAS 300 //地图离整个画布的横向偏移
    12. #define HEIGHT_BIAS 150 //地图离整个画布的纵向偏移
    13. enum _PROPS {
    14. WALL, //墙
    15. FLOOR, //地板
    16. BOX_DES, //目的地
    17. MAN, //小人
    18. BOX, //箱子
    19. HIT, //命中目标
    20. ALL //为什么最后还要定义一个ALL,为了定义下面images数组的长度,这是顶尖项目的经验
    21. };
    22. extern IMAGE images[ALL]; //存放各类道具图片变量
    23. typedef enum _DIRECTION {
    24. UP,
    25. DOWN,
    26. LEFT,
    27. RIGHT
    28. }DIRECTION;
    29. struct _POS {//很多开源项目结构体名用下划线开头
    30. int x;
    31. int y;
    32. };
    33. extern struct _POS man;
    34. extern int map1[LINE][COLUMN];
    35. //函数声明一定要放在最后!!
    36. void changeMap(struct _POS* pos, enum _PROPS pros);
    37. void gameControl(DIRECTION direct);
    38. bool isGameOver();
    39. void GameOverField(IMAGE* bg);
    40. #endif

    如果图形库正常安装,项目属性字符集改为多字节字符集就可以运行了。

    运行:(我这里用的其他图片元素做的)

    胜利后:

     可能大家代码写好但是没有图像资源文件:

    大家可以私聊我,一件三连免费送。没有图像资源光有代码运行不起来。

  • 相关阅读:
    SQL审核 | 如何利用 OpenAPI 实现自己的扫描任务
    LeetCode50天刷题计划第二季(Day 17 — 克隆图 (10.00-11.20)
    Could not read from boot medium. System halted.
    知识图谱从入门到应用——知识图谱的应用
    【语义分割】2022-HRViT CVPR
    qt-C++笔记之两个窗口ui的交互
    基于Java的农资采购销售管理系统设计与实现(源码+lw+部署文档+讲解等)
    【无标题】
    QT之QChart绘制动态曲线
    革新突破!智能指标平台引领时代,国产大模型与企业级部署的完美结合
  • 原文地址:https://blog.csdn.net/qq_51956388/article/details/134096018