服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现坦克大战小游戏EGE图形界面

C++实现坦克大战小游戏EGE图形界面

2021-08-20 12:17白家名 C/C++

这篇文章主要为大家详细介绍了C++实现坦克大战小游戏EGE图形界面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

C++ EGE 实现坦克大战小游戏,供大家参考,具体内容如下

因为有过一次用EGE写小游戏的经验,所以这一次写坦克大战快了很多。并且使用对象编程也简化了很多编程时繁琐的步骤。
写出坦克大战使我在学习编程的道路上又迈出了一大步。

如果您需要图片素材的,我可以单独发给您。

技术环节:

编译环境:Windows VS2019

需求:

控制坦克移动发射炮弹,炮弹可以消灭敌军坦克,且可以消灭砖块。坦克遇到方块会被挡住。敌军消灭我军三次或基地被毁则游戏失败,共摧毁十次敌方坦克游戏胜利。

思路:

先写出坦克的父类,我方坦克类和敌方坦克类继承坦克父类,实例化我方坦克和敌方坦克。地图使用list容器存储。

在代码注释中标注了每一步是怎么实现的。

注意:

因为我在程序中用了一些不规范的写法,所以要在VS中正常编译运行,需要右键源文件->属性->C/C+±>符合模式,改为否。

包含<graphics.h>图形库需要提前配置EGE图形库。
如要在其他graphics图形库下编译,可能需要修改某些地方。

运行效果:

C++实现坦克大战小游戏EGE图形界面

C++实现坦克大战小游戏EGE图形界面

C++实现坦克大战小游戏EGE图形界面

代码:

  1. #include <graphics.h> //图形库
  2. #include <ctime> //time();
  3. #include <list> //list容器
  4.  
  5. using namespace std; //标准命名空间 list等
  6.  
  7. //设置图片对象中图片的宽高 全局函数
  8. //参数:宽、高、对象名
  9. void setimage(int pwidth, int pheight, PIMAGE img_1);
  10.  
  11. //地图全局结构
  12. struct mapstr
  13. {
  14. int m_x; //xy坐标
  15. int m_y;
  16. int prop; //属性
  17. };
  18.  
  19. //地图类
  20. class Mymap
  21. {
  22. private:
  23. list<mapstr> listmap; //地图容器,存储全地图信息,全地图1350个20*20的格子
  24.  
  25. public:
  26. //设置地图
  27. Mymap()
  28. {
  29. mapstr temp;
  30.  
  31. //构造函数为链表容器中的地图赋值
  32.  
  33. //全地图横向45个格子 竖向30个格子
  34.  
  35. //基地部分
  36. {
  37. temp.prop = 0;
  38. for (int i = 0; i < 4; i++) { temp.m_x = 380 + i * 20; temp.m_y = 540; listmap.push_back(temp); }
  39. for (int i = 0; i < 4; i++) { temp.m_x = 380 + i * 20; temp.m_y = 520; listmap.push_back(temp); }
  40. for (int i = 0; i < 2; i++) { temp.m_x = 380; temp.m_y = 560 + i * 20; listmap.push_back(temp); }
  41. for (int i = 0; i < 4; i++) { temp.m_x = 360; temp.m_y = 520 + i * 20; listmap.push_back(temp); }
  42. for (int i = 0; i < 2; i++) { temp.m_x = 440; temp.m_y = 560 + i * 20; listmap.push_back(temp); }
  43. for (int i = 0; i < 4; i++) { temp.m_x = 460; temp.m_y = 520 + i * 20; listmap.push_back(temp); }
  44. temp.prop = 4, temp.m_x = 400, temp.m_y = 560; listmap.push_back(temp);
  45. }
  46.  
  47. //左上角部分
  48. {
  49. temp.prop = 0;
  50. //左上角单独砖块
  51. for (int i = 0; i < 2; i++) { temp.m_x = 40 + i * 20; temp.m_y = 80; listmap.push_back(temp); }
  52. for (int i = 0; i < 2; i++) { temp.m_x = 40 + i * 20; temp.m_y = 100; listmap.push_back(temp); }
  53. //竖铁块
  54. for (int i = 0; i < 4; i++) { temp.m_x = 160; temp.m_y = i * 20; temp.prop = 1; listmap.push_back(temp); }
  55. for (int i = 0; i < 4; i++) { temp.m_x = 180; temp.m_y = i * 20; listmap.push_back(temp); }
  56. //砖块
  57. for (int i = 0; i < 4; i++) { temp.m_x = 160; temp.m_y = 160 + i * 20; temp.prop = 0; listmap.push_back(temp); }
  58. for (int i = 0; i < 4; i++) { temp.m_x = 180; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
  59. //草块
  60. for (int i = 0; i < 4; i++) { temp.m_x = 0; temp.m_y = 200 + i * 20; temp.prop = 2; listmap.push_back(temp); }
  61. for (int i = 0; i < 4; i++) { temp.m_x = 20; temp.m_y = 200 + i * 20; listmap.push_back(temp); }
  62. for (int i = 0; i < 2; i++) { temp.m_x = 40 + i * 20; temp.m_y = 240; listmap.push_back(temp); }
  63. for (int i = 0; i < 2; i++) { temp.m_x = 40 + i * 20; temp.m_y = 260; listmap.push_back(temp); }
  64. }
  65.  
  66. //中上部分
  67. {
  68. //铁块
  69. for (int i = 0; i < 2; i++) { temp.m_x = 320; temp.m_y = i * 20; temp.prop = 1; listmap.push_back(temp); }
  70. for (int i = 0; i < 2; i++) { temp.m_x = 340; temp.m_y = i * 20; listmap.push_back(temp); }
  71. for (int i = 0; i < 2; i++) { temp.m_x = 280 + i * 20; temp.m_y = 160; listmap.push_back(temp); }
  72. for (int i = 0; i < 2; i++) { temp.m_x = 280 + i * 20; temp.m_y = 180; listmap.push_back(temp); }
  73. for (int i = 0; i < 4; i++) { temp.m_x = 400 + i * 20; temp.m_y = 200; listmap.push_back(temp); }
  74. for (int i = 0; i < 4; i++) { temp.m_x = 400 + i * 20; temp.m_y = 220; listmap.push_back(temp); }
  75. //砖块
  76. for (int i = 0; i < 4; i++) { temp.m_x = 320; temp.m_y = 40 + i * 20; temp.prop = 0; listmap.push_back(temp); }
  77. for (int i = 0; i < 4; i++) { temp.m_x = 340; temp.m_y = 40 + i * 20; listmap.push_back(temp); }
  78. for (int i = 0; i < 2; i++) { temp.m_x = 240; temp.m_y = 200 + i * 20; listmap.push_back(temp); }
  79. for (int i = 0; i < 2; i++) { temp.m_x = 260; temp.m_y = 200 + i * 20; listmap.push_back(temp); }
  80. }
  81.  
  82. //右上部分
  83. {
  84. //砖块
  85. for (int i = 0; i < 4; i++) { temp.m_x = 480; temp.m_y = 40 + i * 20; listmap.push_back(temp); }
  86. for (int i = 0; i < 4; i++) { temp.m_x = 500; temp.m_y = 40 + i * 20; listmap.push_back(temp); }
  87. for (int i = 0; i < 2; i++) { temp.m_x = 480; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
  88. for (int i = 0; i < 2; i++) { temp.m_x = 500; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
  89. for (int i = 0; i < 4; i++) { temp.m_x = 600; temp.m_y = 40 + i * 20; listmap.push_back(temp); }
  90. for (int i = 0; i < 4; i++) { temp.m_x = 620; temp.m_y = 40 + i * 20; listmap.push_back(temp); }
  91. for (int i = 0; i < 2; i++) { temp.m_x = 600; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
  92. for (int i = 0; i < 2; i++) { temp.m_x = 620; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
  93. for (int i = 0; i < 6; i++) { temp.m_x = 680 + i * 20; temp.m_y = 200; listmap.push_back(temp); }
  94. for (int i = 0; i < 6; i++) { temp.m_x = 680 + i * 20; temp.m_y = 220; listmap.push_back(temp); }
  95. for (int i = 0; i < 6; i++) { temp.m_x = 760; temp.m_y = 0 + i * 20; listmap.push_back(temp); }
  96. for (int i = 0; i < 6; i++) { temp.m_x = 780; temp.m_y = 0 + i * 20; listmap.push_back(temp); }
  97. //草块
  98. for (int i = 0; i < 6; i++) { temp.m_x = 560; temp.m_y = 160 + i * 20; temp.prop = 2; listmap.push_back(temp); }
  99. for (int i = 0; i < 6; i++) { temp.m_x = 580; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
  100. for (int i = 0; i < 4; i++) { temp.m_x = 520; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
  101. for (int i = 0; i < 4; i++) { temp.m_x = 540; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
  102. for (int i = 0; i < 4; i++) { temp.m_x = 860; temp.m_y = 80 + i * 20; listmap.push_back(temp); }
  103. for (int i = 0; i < 4; i++) { temp.m_x = 880; temp.m_y = 80 + i * 20; listmap.push_back(temp); }
  104. //铁块
  105. for (int i = 0; i < 4; i++) { temp.m_x = 520 + i * 20; temp.m_y = 80; temp.prop = 1; listmap.push_back(temp); }
  106. for (int i = 0; i < 4; i++) { temp.m_x = 520 + i * 20; temp.m_y = 100; listmap.push_back(temp); }
  107. for (int i = 0; i < 4; i++) { temp.m_x = 640 + i * 20; temp.m_y = 160; listmap.push_back(temp); }
  108. for (int i = 0; i < 4; i++) { temp.m_x = 640 + i * 20; temp.m_y = 180; listmap.push_back(temp); }
  109. for (int i = 0; i < 6; i++) { temp.m_x = 800 + i * 20; temp.m_y = 200; listmap.push_back(temp); }
  110. for (int i = 0; i < 6; i++) { temp.m_x = 800 + i * 20; temp.m_y = 220; listmap.push_back(temp); }
  111. }
  112.  
  113. //左下部分
  114. {
  115. //铁块
  116. for (int i = 0; i < 2; i++) { temp.m_x = i * 20; temp.m_y = 360; temp.prop = 1; listmap.push_back(temp); }
  117. for (int i = 0; i < 2; i++) { temp.m_x = i * 20; temp.m_y = 380; listmap.push_back(temp); }
  118. for (int i = 0; i < 4; i++) { temp.m_x = 160; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  119. for (int i = 0; i < 4; i++) { temp.m_x = 180; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  120. //砖块
  121. for (int i = 0; i < 12; i++) { temp.m_x = 40; temp.m_y = 360 + i * 20; temp.prop = 0; listmap.push_back(temp); }
  122. for (int i = 0; i < 12; i++) { temp.m_x = 60; temp.m_y = 360 + i * 20; listmap.push_back(temp); }
  123. for (int i = 0; i < 8; i++) { temp.m_x = 40 + i * 20; temp.m_y = 280; listmap.push_back(temp); }
  124. for (int i = 0; i < 8; i++) { temp.m_x = 40 + i * 20; temp.m_y = 300; listmap.push_back(temp); }
  125. for (int i = 0; i < 4; i++) { temp.m_x = 160; temp.m_y = 400 + i * 20; listmap.push_back(temp); }
  126. for (int i = 0; i < 4; i++) { temp.m_x = 180; temp.m_y = 400 + i * 20; listmap.push_back(temp); }
  127. for (int i = 0; i < 2; i++) { temp.m_x = 160; temp.m_y = 560 + i * 20; listmap.push_back(temp); }
  128. for (int i = 0; i < 2; i++) { temp.m_x = 180; temp.m_y = 560 + i * 20; listmap.push_back(temp); }
  129. for (int i = 0; i < 2; i++) { temp.m_x = 240 + i * 20; temp.m_y = 240; listmap.push_back(temp); }
  130. for (int i = 0; i < 2; i++) { temp.m_x = 240 + i * 20; temp.m_y = 260; listmap.push_back(temp); }
  131. for (int i = 0; i < 8; i++) { temp.m_x = 280; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  132. for (int i = 0; i < 8; i++) { temp.m_x = 300; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  133. for (int i = 0; i < 8; i++) { temp.m_x = 320; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  134. for (int i = 0; i < 8; i++) { temp.m_x = 340; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  135. for (int i = 0; i < 4; i++) { temp.m_x = 360; temp.m_y = 400 + i * 20; listmap.push_back(temp); }
  136. for (int i = 0; i < 4; i++) { temp.m_x = 380; temp.m_y = 400 + i * 20; listmap.push_back(temp); }
  137. for (int i = 0; i < 4; i++) { temp.m_x = 400; temp.m_y = 400 + i * 20; listmap.push_back(temp); }
  138. for (int i = 0; i < 4; i++) { temp.m_x = 420; temp.m_y = 400 + i * 20; listmap.push_back(temp); }
  139. for (int i = 0; i < 8; i++) { temp.m_x = 440; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  140. for (int i = 0; i < 8; i++) { temp.m_x = 460; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  141. for (int i = 0; i < 8; i++) { temp.m_x = 480; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  142. for (int i = 0; i < 8; i++) { temp.m_x = 500; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  143. for (int i = 0; i < 8; i++) { temp.m_x = 520; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  144. for (int i = 0; i < 8; i++) { temp.m_x = 540; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  145. //草块
  146. for (int i = 0; i < 10; i++) { temp.m_x = 200 + i * 20; temp.m_y = 280; temp.prop = 2; listmap.push_back(temp); }
  147. for (int i = 0; i < 10; i++) { temp.m_x = 200 + i * 20; temp.m_y = 300; listmap.push_back(temp); }
  148. for (int i = 0; i < 4; i++) { temp.m_x = 200 + i * 20; temp.m_y = 320; listmap.push_back(temp); }
  149. for (int i = 0; i < 4; i++) { temp.m_x = 200 + i * 20; temp.m_y = 340; listmap.push_back(temp); }
  150. }
  151.  
  152. //右下部分
  153. {
  154. //砖块
  155. for (int i = 0; i < 8; i++) { temp.m_x = 600; temp.m_y = 320 + i * 20; temp.prop = 0; listmap.push_back(temp); }
  156. for (int i = 0; i < 8; i++) { temp.m_x = 620; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  157. for (int i = 0; i < 4; i++) { temp.m_x = 560; temp.m_y = 520 + i * 20; listmap.push_back(temp); }
  158. for (int i = 0; i < 4; i++) { temp.m_x = 580; temp.m_y = 520 + i * 20; listmap.push_back(temp); }
  159. for (int i = 0; i < 8; i++) { temp.m_x = 600 + i * 20; temp.m_y = 560; listmap.push_back(temp); }
  160. for (int i = 0; i < 8; i++) { temp.m_x = 600 + i * 20; temp.m_y = 580; listmap.push_back(temp); }
  161. for (int i = 0; i < 4; i++) { temp.m_x = 680 + i * 20; temp.m_y = 520; listmap.push_back(temp); }
  162. for (int i = 0; i < 4; i++) { temp.m_x = 680 + i * 20; temp.m_y = 540; listmap.push_back(temp); }
  163. for (int i = 0; i < 7; i++) { temp.m_x = 760 + i * 20; temp.m_y = 320; listmap.push_back(temp); }
  164. for (int i = 0; i < 7; i++) { temp.m_x = 760 + i * 20; temp.m_y = 340; listmap.push_back(temp); }
  165. for (int i = 0; i < 8; i++) { temp.m_x = 800; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  166. for (int i = 0; i < 8; i++) { temp.m_x = 820; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  167. //铁块
  168. for (int i = 0; i < 4; i++) { temp.m_x = 640; temp.m_y = 320 + i * 20; temp.prop = 1; listmap.push_back(temp); }
  169. for (int i = 0; i < 4; i++) { temp.m_x = 660; temp.m_y = 320 + i * 20; listmap.push_back(temp); }
  170. for (int i = 0; i < 2; i++) { temp.m_x = 680; temp.m_y = 320 + i * 20; temp.prop = 1; listmap.push_back(temp); }
  171. for (int i = 0; i < 2; i++) { temp.m_x = 700; temp.m_y = 320 + i * 20; temp.prop = 1; listmap.push_back(temp); }
  172. for (int i = 0; i < 4; i++) { temp.m_x = 820 + i * 20; temp.m_y = 480; listmap.push_back(temp); }
  173. for (int i = 0; i < 4; i++) { temp.m_x = 820 + i * 20; temp.m_y = 500; listmap.push_back(temp); }
  174. //草块
  175. for (int i = 0; i < 4; i++) { temp.m_x = 560; temp.m_y = 360 + i * 20; temp.prop = 2; listmap.push_back(temp); }
  176. for (int i = 0; i < 4; i++) { temp.m_x = 580; temp.m_y = 360 + i * 20; listmap.push_back(temp); }
  177. }
  178. }
  179.  
  180. //显示地图
  181. void printmap(list<mapstr>& listmap)
  182. {
  183. PIMAGE mapprop_0 = newimage();
  184. getimage(mapprop_0, "坦克大战完整素材\\砖块.png");
  185. setimage(20, 20, mapprop_0);
  186. PIMAGE mapprop_1 = newimage();
  187. getimage(mapprop_1, "坦克大战完整素材\\铁块.png");
  188. setimage(20, 20, mapprop_1);
  189. PIMAGE mapprop_2 = newimage();
  190. getimage(mapprop_2, "坦克大战完整素材\\草块.png");
  191. setimage(20, 20, mapprop_2);
  192. PIMAGE mapprop_4 = newimage();
  193. getimage(mapprop_4, "坦克大战完整素材\\老鹰_1.png");
  194. setimage(40, 40, mapprop_4);
  195. PIMAGE mapprop_5 = newimage();
  196. getimage(mapprop_5, "坦克大战完整素材\\老鹰_2.png");
  197. setimage(40, 40, mapprop_5);
  198.  
  199. for (list<mapstr>::iterator it = listmap.begin(); it != listmap.end(); it++)
  200. {
  201. switch (it->prop)
  202. {
  203. case 0:
  204. putimage(it->m_x, it->m_y, mapprop_0);
  205. break;
  206. case 1:
  207. putimage(it->m_x, it->m_y, mapprop_1);
  208. break;
  209. case 2:
  210. putimage(it->m_x, it->m_y, mapprop_2);
  211. break;
  212. case 4:
  213. putimage(it->m_x, it->m_y, mapprop_4);
  214. break;
  215. case 5:
  216. putimage(it->m_x, it->m_y, mapprop_5);
  217. break;
  218. }
  219. }
  220.  
  221. delimage(mapprop_0);
  222. delimage(mapprop_1);
  223. delimage(mapprop_2);
  224. delimage(mapprop_4);
  225. delimage(mapprop_5);
  226.  
  227. }
  228.  
  229. //获取地图容器
  230. list<mapstr> getmapves()
  231. {
  232. return listmap;
  233. }
  234. };
  235.  
  236. //坦克父类
  237. class Tank
  238. {
  239. public:
  240. virtual void move(const list<mapstr>& tempves) = 0; //坦克移动函数
  241.  
  242. int getlauch_x() //获取子弹x坐标
  243. {
  244. return b_m_x;
  245. }
  246. int getlauch_y() //获取子弹y坐标
  247. {
  248. return b_m_y;
  249. }
  250. void setlauch_xy() //设置子弹坐标
  251. {
  252. b_m_x = m_x + 18; //重置位置为坦克中间
  253. b_m_y = m_y + 18;
  254. key1 = key2; //key1 = key2避免炮弹一直发射
  255. }
  256. int getTank_x() //获取坦克x坐标
  257. {
  258. return m_x;
  259. }
  260. int getTank_y() //获取坦克y坐标
  261. {
  262. return m_y;
  263. }
  264. char getkey2() //返回发射时的坦克方向信息
  265. {
  266. return key2;
  267. }
  268.  
  269. //坦克攻击子弹位置爆炸
  270. void exploed()
  271. {
  272. PIMAGE explimg_1 = newimage();
  273. getimage(explimg_1, "坦克大战完整素材\\爆炸效果1.png");
  274. setimage(10, 10, explimg_1);
  275. PIMAGE explimg_2 = newimage();
  276. getimage(explimg_2, "坦克大战完整素材\\爆炸效果2.png");
  277. setimage(20, 20, explimg_2);
  278. PIMAGE explimg_3 = newimage();
  279. getimage(explimg_3, "坦克大战完整素材\\爆炸效果3.png");
  280. setimage(40, 40, explimg_3);
  281.  
  282. PIMAGE explimgarr[3] = { explimg_1, explimg_2, explimg_3 };
  283.  
  284. for (int i = 0; i < 3; i++)
  285. {
  286. if (key2 == 'd' || key2 == 'a')//根据坦克的攻击朝向确定爆炸的位置
  287. putimage(b_m_x, b_m_y - 6 * i, explimgarr[i]);
  288. else
  289. putimage(b_m_x - 6 * i, b_m_y, explimgarr[i]);
  290.  
  291. delay_fps(42);
  292. }
  293.  
  294. delimage(explimg_1);
  295. delimage(explimg_2);
  296. delimage(explimg_3);
  297. }
  298.  
  299. //构造函数
  300. Tank()
  301. {
  302. m_x = 0; m_y = 0;
  303. m_health = 0; m_damage = 0;
  304. path_1 = NULL; path_2 = NULL;
  305. path_3 = NULL; path_4 = NULL;
  306. b_m_x = 0; b_m_y = 0;
  307. key1 = '0'; key2 = '0';
  308. }
  309.  
  310. protected:
  311. //从文件中获取坦克图片
  312. void gettank(const char *path)
  313. {
  314. PIMAGE tankimg = newimage(); //创建图片对象
  315. getimage(tankimg, path); //在文件中获取图片到图片对象
  316. setimage(40, 40, tankimg); //设置图片对象大小
  317. putimage(this->m_x, this->m_y, tankimg);//在坐标处输出图片
  318. delimage(tankimg); //释放图片对象
  319. }
  320. //输出显示坦克
  321. void printtank(const char key2)
  322. {
  323. //根据当前的键值,输出坦克
  324. switch (key2)
  325. {
  326. case 'w':
  327. gettank(path_1); break; //输出坦克
  328. case 's':
  329. gettank(path_2); break;
  330. case 'a':
  331. gettank(path_3); break;
  332. case 'd':
  333. gettank(path_4); break;
  334. }
  335. }
  336.  
  337. //发射子弹
  338. void launch()
  339. {
  340. printtank(key2);
  341. setfillcolor(WHITE);
  342.  
  343. switch (key2)
  344. {
  345. case 'w':
  346. b_m_y -= 10;
  347. bar(b_m_x, b_m_y, b_m_x + 4, b_m_y + 8);
  348. break;
  349. case 'a':
  350. b_m_x -= 10;
  351. bar(b_m_x, b_m_y, b_m_x + 8, b_m_y + 4);
  352. break;
  353. case 's':
  354. b_m_y += 10;
  355. bar(b_m_x, b_m_y, b_m_x + 4, b_m_y + 8);
  356. break;
  357. case 'd':
  358. b_m_x += 10;
  359. bar(b_m_x, b_m_y, b_m_x + 8, b_m_y + 4);
  360. break;
  361. }
  362.  
  363. //子弹越界目标则子弹坐标刷新
  364. if (b_m_x >= 900 || b_m_x <= 0 || b_m_y >= 600 || b_m_y <= 0)
  365. setlauch_xy(); //重置子弹位置
  366. }
  367.  
  368. int m_x; //坦克xy坐标
  369. int m_y;
  370. int m_health; //坦克血量
  371. int m_damage; //子弹伤害量
  372. char* path_1; //四张不同方向的坦克图片,由派生类决定图片路径
  373. char* path_2;
  374. char* path_3;
  375. char* path_4;
  376. int b_m_x; //子弹坐标xy
  377. int b_m_y;
  378. char key1; //用于接收键盘信息
  379. char key2; //用于存储上一条键值,也是发射时的坦克的朝向
  380.  
  381. };
  382.  
  383. //游戏失败结束全局函数 在生命为0 和 基地被攻击时调用
  384. void gameoverfalse()
  385. {
  386. cleardevice();
  387.  
  388. PIMAGE gameoverbackimg = newimage();
  389. getimage(gameoverbackimg, "坦克大战完整素材\\游戏结束背景.jpg");
  390. setimage(900,600,gameoverbackimg);
  391. putimage(0, 0, gameoverbackimg); //输出背景图片
  392.  
  393. PIMAGE gameoverimg = newimage();
  394. getimage(gameoverimg, "坦克大战完整素材\\游戏结束.png");
  395. setimage(200, 200, gameoverimg);
  396. putimage_withalpha(NULL, gameoverimg, 350, 200); //透明格式输出游戏结束图片
  397.  
  398. delimage(gameoverbackimg);
  399. delimage(gameoverimg); //释放内存
  400.  
  401. getch();
  402. getch();
  403. }
  404.  
  405. //我方坦克,可被操控移动
  406. class TankFriend :public Tank
  407. {
  408. private:
  409. int Fridienum = 0;
  410. public:
  411. //构造函数初始化坦克坐标
  412. TankFriend()
  413. {
  414. m_x = 300; //我方坦克的初始坐标为屏幕中下方
  415. m_y = 560;
  416. m_health = 100; //坦克血量
  417. m_damage = 90; //坦克伤害
  418. b_m_x = m_x + 18;
  419. b_m_x = m_x + 18;
  420. path_1 = "坦克大战完整素材\\己方坦克上.png"; //赋值需要将源文件属性语言中符合模式改为否
  421. path_2 = "坦克大战完整素材\\己方坦克下.png";
  422. path_3 = "坦克大战完整素材\\己方坦克左.png";
  423. path_4 = "坦克大战完整素材\\己方坦克右.png";
  424. key1 = 'w'; //初始化key1用于输出初始的坦克
  425. }
  426.  
  427. int getTankdamage() //获取坦克伤害
  428. {
  429. return m_damage;
  430. }
  431. int getTankhealth() //获取坦克血量
  432. {
  433. return m_health;
  434. }
  435. void setTankhealth(const int health) //设置坦克血量
  436. {
  437. m_health = health;
  438. }
  439. int getFridienumfun() //获取我方坦克被消灭次数
  440. {
  441. return Fridienum;
  442. }
  443.  
  444. //坦克被操控移动
  445. virtual void move(const list<mapstr>& tempves)
  446. {
  447. if (key1 != 'j')
  448. key2 = key1; //key2记录上一个key1的值
  449.  
  450. if (kbhit())
  451. {
  452. //用临时变量接收键值,如果键值为wasdj中的一个则赋给使用的变量
  453. char tempkey = getch();
  454.  
  455. switch (tempkey)
  456. {
  457. case 'w': case 'a':
  458. case 's': case 'd':
  459. case 'j':
  460. key1 = tempkey;
  461. break;
  462. //接收的键盘值不是wasdj直接return
  463. default:
  464. return;
  465. }
  466.  
  467. //判断坦克撞墙情况,坦克撞墙可以改变方向,但不会移动坐标
  468. for (list<mapstr>::const_iterator it = tempves.begin(); it != tempves.end(); it++)
  469. {
  470. switch (tempkey)
  471. {
  472. //这里的大于小于号一个都不能错
  473. case 'w':
  474. if (
  475. (
  476. //判断坦克原xy点
  477. (m_x >= it->m_x && m_x < it->m_x + 20) && (m_y - 20 == it->m_y) ||
  478.  
  479. //判断坦克右xy点
  480. (m_x + 40 > it->m_x&& m_x + 40 <= it->m_x + 20) && (m_y - 20 == it->m_y)
  481. )
  482. //判断方块属性
  483. && (it->prop == 0 || it->prop == 1)
  484.  
  485. //判断地图边界范围
  486. || (m_y - 20 < 0)
  487. )
  488. return;
  489. break;
  490. case 'a':
  491. if (
  492. (
  493. //判断坦克原xy点
  494. (m_y >= it->m_y && m_y < it->m_y + 20) && (m_x - 20 == it->m_x) ||
  495.  
  496. //判断坦克右xy点
  497. (m_y + 40 > it->m_y && m_y + 40 <= it->m_y + 20) && (m_x - 20 == it->m_x)
  498. )
  499. //方块属性
  500. && (it->prop == 0 || it->prop == 1)
  501.  
  502. //判断地图边界范围
  503. || (m_x - 20 < 0)
  504. )
  505. return;
  506. break;
  507. case 's':
  508. if (
  509. (
  510. //判断坦克原xy点
  511. (m_x >= it->m_x && m_x < it->m_x + 20) && (m_y + 40 == it->m_y) ||
  512.  
  513. //判断坦克右xy点
  514. (m_x + 40 > it->m_x&& m_x + 40 <= it->m_x + 20) && (m_y + 40 == it->m_y)
  515. )
  516. //判断方块属性
  517. && (it->prop == 0 || it->prop == 1)
  518.  
  519. //判断地图边界范围
  520. || (m_y + 60 > 600)
  521. )
  522. return;
  523. break;
  524. case 'd':
  525. if (
  526. (
  527. //判断坦克原xy点
  528. (m_y >= it->m_y && m_y < it->m_y + 20) && (m_x + 40 == it->m_x) ||
  529.  
  530. //判断坦克右xy点
  531. (m_y + 40 > it->m_y&& m_y + 40 <= it->m_y + 20) && (m_x + 40 == it->m_x)
  532. )
  533. //方块属性
  534. && (it->prop == 0 || it->prop == 1)
  535.  
  536. //判断地图边界范围
  537. || (m_x + 60 > 900)
  538. )
  539. return;
  540. break;
  541. }
  542. }
  543.  
  544. //根据key1值修改坦克坐标
  545. switch (key1)
  546. {
  547. case 'w':
  548. m_y -= 20; break;
  549. case 'a':
  550. m_x -= 20; break;
  551. case 's':
  552. m_y += 20; break;
  553. case 'd':
  554. m_x += 20; break;
  555. }
  556. }
  557.  
  558. //如果键值为j则发射炮弹,如果键值为wasd则移动坦克
  559. if (key1 == 'j')
  560. launch();
  561. else
  562. {
  563. printtank(key1); //根据键值输出显示坦克
  564. b_m_x = m_x + 18; //移动时也重置子弹坐标
  565. b_m_y = m_y + 18; //这里key1值不能重置为key2值
  566. }
  567.  
  568. //如果我军坦克被消灭,则被消灭次数+1,并重置坦克
  569. if (m_health <= 0)
  570. {
  571. Fridienum++;
  572. m_x = 300; //坐标
  573. m_y = 560;
  574. m_health = 100; //血量
  575. }
  576. }
  577. };
  578.  
  579. //统计所有敌军坦克次数全局变量
  580. int Endienum;
  581.  
  582. //敌军坦克
  583. class TankEnemy :public Tank
  584. {
  585. private:
  586. int pathsch; //路线方案
  587. int contdir;
  588. bool Entankdie = true; //标记敌军坦克的死亡状态
  589.  
  590. public:
  591. //构造函数接收初始坦克坐标
  592. TankEnemy(const int x ,const int y, const int pathsch) //构造函数初始化坦克坐标
  593. {
  594. m_x = x; //敌方坦克初始坐标
  595. m_y = y;
  596. b_m_x = m_x + 18; //坦克子弹坐标
  597. b_m_x = m_x + 18;
  598. m_health = 300; //坦克血量
  599. m_damage = 90; //坦克伤害
  600. contdir = 0;
  601. this->pathsch = pathsch; //路线
  602. path_1 = "坦克大战完整素材\\敌方坦克上.png"; //赋值需要将源文件属性语言中符合模式改为否
  603. path_2 = "坦克大战完整素材\\敌方坦克下.png";
  604. path_3 = "坦克大战完整素材\\敌方坦克左.png";
  605. path_4 = "坦克大战完整素材\\敌方坦克右.png";
  606.  
  607. gettank(path_1);//输出一个坦克图片
  608. }
  609.  
  610. int getTankdamage() //获取坦克伤害
  611. {
  612. return m_damage;
  613. }
  614. int getTankhealth() //获取坦克血量
  615. {
  616. return m_health;
  617. }
  618. void setTankhealth(const int health) //设置坦克血量
  619. {
  620. m_health = health;
  621. }
  622. bool getEntadist() //获取坦克的死亡状态
  623. {
  624. return Entankdie;
  625. }
  626. void setpathsch(const int tanknum) //修改坦克行动路线
  627. {
  628. switch (tanknum) //判断坦克编号
  629. {
  630. case 1:
  631. pathsch = 5; //坦克1路线切换为路线2
  632. break;
  633. case 2:
  634. pathsch = 6;
  635. break;
  636. case 3:
  637. pathsch = 7;
  638. break;
  639. case 4:
  640. pathsch = 8;
  641. break;
  642. }
  643. }
  644.  
  645. //重置坦克
  646. void setEntank(const int tanknum)
  647. {
  648. if (Entankdie == false && (pathsch == 5 || pathsch == 6 || pathsch == 7 || pathsch == 8))
  649. return; //如果这个坦克的路线已经被修改过,且再次死亡,则不再重置
  650.  
  651. switch (tanknum)
  652. {
  653. case 1:
  654. m_x = 200, m_y = 40;
  655. contdir = 2;
  656. break;
  657. case 2:
  658. m_x = 720, m_y = 120;
  659. contdir = 3;
  660. break;
  661. case 3:
  662. m_x = 560, m_y = 120;
  663. contdir = 2;
  664. break;
  665. case 4:
  666. m_x = 80, m_y = 360;
  667. contdir = 2;
  668. }
  669. m_health = 300;
  670. Entankdie = true;
  671. }
  672.  
  673. //坦克1第二路线
  674. //直接攻击基地
  675. void pathschfun5_1_2()
  676. {
  677. static bool temp = true; //临时变量用作标记
  678.  
  679. if(temp == true)
  680. contdir = 2;
  681.  
  682. if (m_y == 560 && temp == true) //往右
  683. contdir = 4;
  684.  
  685. if (m_y == 560 && m_x == 240)
  686. {
  687. m_y = 560, m_x = 240;
  688. contdir = 0;
  689. temp = false;
  690. }
  691. }
  692.  
  693. //坦克2路线2
  694. void pathschfun6_2_2()
  695. {
  696. //720, 120
  697. //需要改变两次方向
  698. static bool temp = true;
  699. static bool temp2 = true;
  700.  
  701. if (temp == true && temp2 == true)
  702. contdir = 3; //往左
  703.  
  704. if (m_x == 200 && temp == true && temp2 == true) //往下
  705. contdir = 2;
  706.  
  707. if (m_x == 200 && m_y == 560 && temp == true)
  708. {
  709. contdir = 4; //往右
  710. temp2 = false;
  711. }
  712.  
  713. if (m_y == 560 && m_x == 280)
  714. {
  715. m_y = 560, m_x = 280;
  716. contdir = 0;
  717. temp = false;
  718. }
  719. }
  720.  
  721. //坦克3路线2
  722. void pathschfun7_3_2()
  723. {
  724. static bool temp = true;
  725. if (temp == true)
  726. contdir = 2; //往下
  727.  
  728. if (m_y == 560 && temp == true) //往左
  729. contdir = 3;
  730.  
  731. if (m_y == 560 && m_x == 480)
  732. {
  733. m_y = 560, m_x = 480;
  734. contdir = 0;
  735. temp = false;
  736. }
  737. }
  738.  
  739. //坦克4路线2
  740. void pathschfun8_4_2()
  741. {
  742. static bool temp = true;
  743. if(temp == true)
  744. contdir = 2;
  745.  
  746. if (m_y == 560 && temp == true)
  747. contdir = 4;
  748.  
  749. if (m_x == 200 && m_y == 560)
  750. {
  751. m_x = 200, m_y = 560;
  752. contdir = 0;
  753. temp = false;
  754. }
  755. }
  756.  
  757. //正常路线1
  758. void pathschfun_1()
  759. {
  760. static bool temp = false; //临时变量辅助控制坦克行走路线
  761.  
  762. if (m_y == 480 && m_x == 200 && temp == true) //上
  763. {
  764. b_m_x = m_x + 18; //重置子弹位置为坦克中间
  765. b_m_y = m_y + 18;
  766. contdir = 1;
  767. return;
  768. }
  769. else if (m_y == 40 && m_x == 200) //下
  770. {
  771. b_m_x = m_x + 18;
  772. b_m_y = m_y + 18;
  773. contdir = 2;
  774. temp = false;
  775. return;
  776. }
  777. else if (m_y == 480 && m_x == 200 && temp == false) //右
  778. {
  779. b_m_x = m_x + 18;
  780. b_m_y = m_y + 18;
  781. contdir = 4;
  782. temp = true;
  783. return;
  784. }
  785. else if (m_x == 700 && m_y == 480) //左
  786. {
  787. b_m_x = m_x + 18;
  788. b_m_y = m_y + 18;
  789. contdir = 3;
  790. return;
  791. }
  792. }
  793.  
  794. //正常路线2
  795. void pathschfun_2()
  796. {
  797. if (m_x == 720)
  798. {
  799. b_m_x = m_x + 18;
  800. b_m_y = m_y + 18;
  801. contdir = 3;
  802. return;
  803. }
  804. if (m_x <= 140)
  805. {
  806. b_m_x = m_x + 18;
  807. b_m_y = m_y + 18;
  808. contdir = 4;
  809. return;
  810. }
  811. }
  812.  
  813. //正常路线3
  814. void pathschfun_3()
  815. {
  816. if (m_y == 120)
  817. {
  818. b_m_x = m_x + 18;
  819. b_m_y = m_y + 18;
  820. contdir = 2;
  821. return;
  822. }
  823. if (m_y >= 480)
  824. {
  825. b_m_x = m_x + 18;
  826. b_m_y = m_y + 18;
  827. contdir = 1;
  828. return;
  829. }
  830. }
  831.  
  832. //正常路线4
  833. void pathschfun_4()
  834. {
  835. if (m_y == 360)
  836. {
  837. b_m_x = m_x + 18;
  838. b_m_y = m_y + 18;
  839. contdir = 2;
  840. return;
  841. }
  842. if (m_y >= 560)
  843. {
  844. b_m_x = m_x + 18;
  845. b_m_y = m_y + 18;
  846. contdir = 1;
  847. return;
  848. }
  849. }
  850.  
  851. //敌军坦克自动移动
  852. virtual void move(const list<mapstr>& tempves)
  853. {
  854. //根据contdir值,向不同方向移动
  855. switch (contdir)
  856. {
  857. case 1:
  858. key2 = 'w'; //方向
  859. gettank(path_1); //坦克图片
  860. m_y -= 4;
  861. break;
  862. case 2:
  863. key2 = 's';
  864. gettank(path_2);
  865. m_y += 4;
  866. break;
  867. case 3:
  868. key2 = 'a';
  869. gettank(path_3);
  870. m_x -= 4;
  871. break;
  872. case 4:
  873. key2 = 'd';
  874. gettank(path_4);
  875. m_x += 4;
  876. break;
  877. }
  878.  
  879. launch(); //发射子弹
  880.  
  881. //不同pathsch不同路线 一个坦克两条路线
  882. switch (pathsch)
  883. {
  884. case 1: pathschfun_1(); break;
  885. case 2: pathschfun_2(); break;
  886. case 3: pathschfun_3(); break;
  887. case 4: pathschfun_4(); break;
  888. case 5: pathschfun5_1_2(); break;
  889. case 6: pathschfun6_2_2(); break;
  890. case 7: pathschfun7_3_2(); break;
  891. case 8: pathschfun8_4_2();
  892. }
  893.  
  894. //敌军坦克被消灭
  895. //如果坦克血量小于等于0,则将坦克从界面内移除
  896. if (m_health <= 0)
  897. {
  898. Endienum++; //敌军坦克被消灭次数自增
  899. Entankdie = false; //死亡状态为false
  900. m_x = -40,m_y = -40;//坦克被移除至页面外
  901. b_m_x = m_x, b_m_y = m_y;//子弹跟随坦克
  902. }
  903.  
  904. }
  905. };
  906.  
  907. //敌军坦克数量
  908. const int N = 4;
  909.  
  910. //子弹命中检测
  911. bool hittest(TankFriend& tank_f, TankEnemy* Etankarr, list<mapstr>& listmap) //参数:我方坦克对象,敌军坦克对象数组,地图list容器
  912. {
  913. const int tanfirlau_x = tank_f.getlauch_x(); //友军坦克子弹x坐标
  914. const int tanfirlau_y = tank_f.getlauch_y(); //友军坦克子弹y坐标
  915. const int firtank_x = tank_f.getTank_x(); //友军坦克x坐标
  916. const int firtank_y = tank_f.getTank_y(); //友军坦克y坐标
  917. const int tankfirdam = tank_f.getTankdamage(); //友军坦克伤害
  918. const int tankfirhea = tank_f.getTankhealth(); //友军坦克血量
  919.  
  920. for (int i = 0; i < N; i++)
  921. {
  922. //发射子弹需要判断两个点
  923. //如果友军子弹和敌军坦克重合,敌军坦克血量减少,且友军坦克子弹坐标重置
  924. if ((tanfirlau_x >= Etankarr[i].getTank_x() && tanfirlau_x <= Etankarr[i].getTank_x() + 40 &&
  925. tanfirlau_y >= Etankarr[i].getTank_y() && tanfirlau_y <= Etankarr[i].getTank_y() + 40) ||
  926. (tanfirlau_x + 4 >= Etankarr[i].getTank_x() && tanfirlau_x <= Etankarr[i].getTank_x() + 4 + 40 &&
  927. tanfirlau_y >= Etankarr[i].getTank_y() + 4 && tanfirlau_y + 4 <= Etankarr[i].getTank_y() + 40))
  928. {
  929. Etankarr[i].setTankhealth(Etankarr[i].getTankhealth() - tankfirdam); //血量减少
  930. tank_f.exploed(); //友方坦克攻击导致子弹遇到对方的位置爆炸
  931. tank_f.setlauch_xy(); //友军的炮弹坐标重置
  932. }
  933. //如果敌军子弹和友军坦克重合,友军坦克血量减少,且敌军坦克子弹坐标重置
  934. if ((Etankarr[i].getlauch_x() >= firtank_x && Etankarr[i].getlauch_x() <= firtank_x + 40 &&
  935. Etankarr[i].getlauch_y() >= firtank_y && Etankarr[i].getlauch_y() <= firtank_y + 40) ||
  936. (Etankarr[i].getlauch_x() + 4 >= firtank_x && Etankarr[i].getlauch_x() + 4 <= firtank_x + 40 &&
  937. Etankarr[i].getlauch_y() + 4 >= firtank_y && Etankarr[i].getlauch_y() + 4 <= firtank_y + 40))
  938. {
  939. tank_f.setTankhealth(tankfirhea - Etankarr[i].getTankdamage()); //友军坦克本身血量 - 敌军坦克伤害
  940. Etankarr[i].exploed();
  941. Etankarr[i].setlauch_xy(); //敌军的炮弹坐标重置
  942. }
  943.  
  944. //判断墙的状态
  945. //包括我军坦克和敌军坦克子弹和墙的状态
  946. for (list<mapstr>::iterator it = listmap.begin(); it != listmap.end(); it++)
  947. {
  948. //子弹碰到墙壁需要判断两个点
  949. if ((tank_f.getlauch_x() >= it->m_x && tank_f.getlauch_x() <= it->m_x + 20 &&
  950. tank_f.getlauch_y() >= it->m_y && tank_f.getlauch_y() <= it->m_y + 20) ||
  951. (tank_f.getlauch_x() + 4 >= it->m_x && tank_f.getlauch_x() + 4 <= it->m_x + 20 &&
  952. tank_f.getlauch_y() + 4 >= it->m_y && tank_f.getlauch_y() + 4 <= it->m_y + 20))
  953. {
  954. switch (it->prop)
  955. {
  956. case 0: //砖块可以被删除
  957. tank_f.exploed(); //子弹处发生爆炸
  958. tank_f.setlauch_xy(); //子弹重置
  959. listmap.erase(it); //删除被子弹击中的墙壁数据
  960. break;
  961. case 1: //铁块会爆炸不会被删除
  962. tank_f.exploed(); //子弹处发生爆炸
  963. tank_f.setlauch_xy(); //子弹重置
  964. break;
  965. case 2: //草块不会有作用
  966. break;
  967. case 4:
  968. mapstr temp;
  969. temp.m_x = it->m_x;
  970. temp.m_y = it->m_y;
  971. temp.prop = 5;
  972. listmap.insert(it, temp);
  973. listmap.erase(it); //老鹰被攻击游戏结束
  974. return true;
  975. }
  976. //最后break
  977. break;
  978. }
  979.  
  980. if ((Etankarr[i].getlauch_x() >= it->m_x && Etankarr[i].getlauch_x() <= it->m_x + 20 &&
  981. Etankarr[i].getlauch_y() >= it->m_y && Etankarr[i].getlauch_y() <= it->m_y + 20) ||
  982. (Etankarr[i].getlauch_x() + 4 >= it->m_x && Etankarr[i].getlauch_x() + 4 <= it->m_x + 20 &&
  983. Etankarr[i].getlauch_y() + 4 >= it->m_y && Etankarr[i].getlauch_y() + 4 <= it->m_y + 20))
  984. {
  985. switch (it->prop)
  986. {
  987. case 0: //砖块可以被删除
  988. Etankarr[i].exploed(); //子弹处发生爆炸
  989. Etankarr[i].setlauch_xy(); //子弹重置
  990. listmap.erase(it); //删除被子弹击中的墙壁数据
  991. break;
  992. case 1: //铁块会爆炸不会被删除
  993. Etankarr[i].exploed(); //子弹处发生爆炸
  994. Etankarr[i].setlauch_xy(); //子弹重置
  995. break;
  996. case 2: //草块不会有作用
  997. break;
  998. case 4:
  999. mapstr temp;
  1000. temp.m_x = it->m_x;
  1001. temp.m_y = it->m_y;
  1002. temp.prop = 5;
  1003. listmap.insert(it, temp);
  1004. listmap.erase(it);
  1005. return true;
  1006. }
  1007. //最后break
  1008. break;
  1009. }
  1010. }
  1011. }
  1012. return false;
  1013. }
  1014.  
  1015. //游戏开始界面
  1016. void initgamebegin()
  1017. {
  1018. //开始背景
  1019. PIMAGE gabegbaimg = newimage();
  1020. getimage(gabegbaimg,"坦克大战完整素材\\开始游戏背景.jpg");
  1021. setimage(1000,600,gabegbaimg);
  1022. putimage(0, 0, gabegbaimg);
  1023. //开始按钮
  1024. PIMAGE gabegimg = newimage();
  1025. getimage(gabegimg, "坦克大战完整素材\\开始游戏.png");
  1026. setimage(290, 210, gabegimg);
  1027. putimage_withalpha (NULL, gabegimg, 305, 440);
  1028.  
  1029. setfont(120,0,"楷体"); //设置字号,字体
  1030. setcolor(YELLOW); //设置文字颜色
  1031. setbkmode(1); //文字背景色透明
  1032. outtextxy(210, 100, "坦克大战"); //输出文字
  1033.  
  1034. mouse_msg msg; //接收开始游戏的鼠标信息
  1035.  
  1036. while (true)
  1037. {
  1038. msg = getmouse();
  1039.  
  1040. if (msg.is_left())
  1041. if (msg.x >= 305 && msg.y >= 440 && msg.x <= 305 + 290 && msg.y <= 440 + 210)
  1042. break;
  1043.  
  1044. delay_fps(50);
  1045. }
  1046.  
  1047. }
  1048.  
  1049. //游戏胜利结束,全局函数
  1050. void gameovertrue()
  1051. {
  1052. cleardevice();
  1053.  
  1054. PIMAGE gameoverbackimg = newimage();
  1055. getimage(gameoverbackimg, "坦克大战完整素材\\游戏胜利结束背景.jpg");
  1056. setimage(1000, 600, gameoverbackimg);
  1057. putimage(0, 0, gameoverbackimg); //输出背景图片
  1058.  
  1059. PIMAGE gameoverimg = newimage();
  1060. getimage(gameoverimg, "坦克大战完整素材\\胜利文字.png");
  1061. setimage(206, 107, gameoverimg);
  1062. putimage_withalpha(NULL, gameoverimg, 347, 220); //透明格式输出游戏结束图片
  1063.  
  1064. delimage(gameoverbackimg);
  1065. delimage(gameoverimg); //释放内存
  1066.  
  1067. getch();
  1068. getch();
  1069. }
  1070.  
  1071. //主函数
  1072. int main()
  1073. {
  1074. initgraph(900, 600, INIT_RENDERMANUAL); //初始化图形界面
  1075. setcaption("C++ EGE坦克大战"); //设置窗口标题
  1076.  
  1077. initgamebegin(); //游戏开始界面
  1078.  
  1079. TankFriend tank_f; //我方坦克
  1080. TankEnemy tank_e_1(200, 40, 1); //敌方坦克 1
  1081. TankEnemy tank_e_2(720, 120, 2); //敌方坦克 2
  1082. TankEnemy tank_e_3(560, 120, 3); //敌方坦克 3
  1083. TankEnemy tank_e_4(80, 360, 4); //敌方坦克 4
  1084.  
  1085. //敌军坦克数组
  1086. TankEnemy Etankarr[N] = { tank_e_1, tank_e_2,tank_e_3,tank_e_4 };
  1087.  
  1088. Mymap map_1; //地图
  1089.  
  1090. //获取地图信息
  1091. list<mapstr> listmap = map_1.getmapves();
  1092.  
  1093. bool gameoverstat = false; //判断游戏结束状态变量,默认失败
  1094. bool basestat = false; //判断基地老鹰的状态,为真则游戏失败
  1095.  
  1096. //游戏循环
  1097. while (true)
  1098. {
  1099. cleardevice(); //清屏
  1100.  
  1101. //三条生命 被消灭三次时结束游戏
  1102. if (tank_f.getFridienumfun() < 3)
  1103. tank_f.move(listmap);
  1104. else break;
  1105.  
  1106. //敌军坦克数组循环
  1107. for (int i = 0; i < N; i++)
  1108. {
  1109. if(Etankarr[i].getEntadist() == true) //坦克必须存活时才会调用移动函数
  1110. Etankarr[i].move(listmap); //敌方坦克
  1111. else
  1112. {
  1113. switch (Endienum)
  1114. {
  1115. case 3: //坦克死亡时,且敌军总被消灭次数为3时,已被消灭坦克复活
  1116. Etankarr[i].setEntank(i + 1); //重置已坦克被消灭
  1117. break;
  1118. case 4:
  1119. case 6:
  1120. case 8:
  1121. case 10:
  1122. Etankarr[i].setEntank(i + 1); //重置坦克
  1123. Etankarr[i].setpathsch(i + 1); //修改坦克路线
  1124. }
  1125. }
  1126. }
  1127.  
  1128. map_1.printmap(listmap); //输出地图
  1129.  
  1130. basestat = hittest(tank_f, Etankarr ,listmap); //子弹命中坦克检测
  1131.  
  1132. if (basestat) break; //如果basestat为true则游戏按失败break
  1133.  
  1134. if (Endienum > 10) //消灭敌军达10则游戏胜利
  1135. {
  1136. gameoverstat = true;
  1137. break;
  1138. }
  1139.  
  1140. delay_fps(62);
  1141. }
  1142.  
  1143. //根据游戏结束状态,调用结束函数
  1144. gameoverstat ? gameovertrue(): gameoverfalse(); //游戏结束
  1145.  
  1146. return 0;
  1147. }
  1148.  
  1149. //设置图片宽高全局函数
  1150. void setimage(int pwidth, int pheight, PIMAGE img_1)
  1151. {
  1152. int whidth = getwidth(img_1), height = getheight(img_1);//获取当前img图像对象的宽高
  1153.  
  1154. PIMAGE img_2 = newimage(pwidth, pheight); //创建一个新的图像对象,这个新的图像对象的宽高为
  1155. //要重新设置的图像的宽高
  1156.  
  1157. putimage(img_2, 0, 0, pwidth, pheight, img_1, 0, 0, whidth, height); //将原本img中的图像拉伸绘制到img_2中
  1158.  
  1159. getimage(img_1, img_2, 0, 0, pwidth, pheight); //img再获取img_2中的图像
  1160.  
  1161. delimage(img_2); //使用完毕将释放掉
  1162. }

不足之处:

参照标准的坦克大战,子弹在攻击砖块时,如果子弹正好打中两个砖块,则两个砖块同时被消灭,这个效果我在程序中做不出来。只能是用一个一个地消灭代替了。
在按下键键值不是wasdj时和坦克碰到墙壁时坦克会闪的问题,也暂时没有找到解决办法。
因为初学C++,所以我在类和对象的使用上面还有着很多很多的缺陷和不规范的地方。
还有关于地图的问题,因为我没有经验,所以不知道该怎么简便地创造一个游戏地图,在刚开始的时候还在为用什么实现地图而犹豫过,最后还是选择了list容器逐个将地图元素塞进去,因为考虑到list容器插入和删除数据很快。
我知道在程序中很多地方我都写的极为地不合理,以至于程序运行效率非常低(从我的CPU占用看出来的),但是我会继续努力,继续学习,争取早日解决这些问题。

欢迎大家提出批评和建议。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/qq_46239972/article/details/104566928

延伸 · 阅读

精彩推荐