C++编程----简单项目实战飞机大战

野性酷女 2022-12-24 14:58 305阅读 0赞

文章目录

  • 开发工具
  • 效果展示
  • 源代码
  • 素材

开发工具

vs2019(vs系列都可以),easyx图形库

效果展示

在这里插入图片描述
在这里插入图片描述

源代码

一些头文件
myhelp.h文件

  1. #pragma once
  2. #include<easyx.h>
  3. #include<conio.h>
  4. #include<list>
  5. #include<vector>
  6. #include<iostream>
  7. using namespace std;
  8. struct node
  9. {
  10. int x, y;
  11. node(int x, int y) :x(x), y(y) {
  12. }
  13. node() {
  14. x = 0; y = 0; }
  15. };

airplan.h文件

  1. #pragma once
  2. #include"myhelp.h"
  3. class airplan
  4. {
  5. node Plan; //自己的飞机
  6. int px; //飞机大小
  7. list<node> enemyPlan; //敌机
  8. public:
  9. airplan();
  10. airplan(int x, int y);
  11. list<node>& getEnemyPlan()
  12. {
  13. return enemyPlan;
  14. }
  15. node& getPlan()
  16. {
  17. return Plan;
  18. }
  19. bool planeColide(node& v1, node& v2, int px); //飞机碰撞
  20. void destoryEnemy(int height);
  21. };

airplan.cpp文件

  1. #include "airplan.h"
  2. airplan::airplan(int x, int y) :Plan(x, y)
  3. {
  4. px = 40;
  5. }
  6. airplan::airplan(){
  7. }
  8. bool airplan::planeColide(node& v1, node& v2, int px)
  9. {
  10. int x = abs(v1.x - v2.x);
  11. int y = abs(v1.y - v2.y);
  12. return y < px&& x < px;
  13. }
  14. void airplan::destoryEnemy(int height)
  15. {
  16. for (auto it = enemyPlan.begin(); it != enemyPlan.end(); it++)
  17. {
  18. if (it->y > height)
  19. {
  20. enemyPlan.erase(it);//删除it当前位置的数据
  21. break;
  22. }
  23. }
  24. }

bullet.h文件

  1. #pragma once
  2. #include"myhelp.h"
  3. #include"airplan.h"
  4. class Bullet
  5. {
  6. list<node> bullet; //子弹节点
  7. public:
  8. list<node>& getBullet()
  9. {
  10. return bullet;
  11. }
  12. void spwnBullet(node& plan); //发射子弹
  13. void dextoryBullet(); //销毁子弹
  14. };

bullet.cpp文件

  1. #include "bullet.h"
  2. void Bullet::spwnBullet(node& plan) //发射子弹
  3. {
  4. bullet.push_back(node(plan.x, plan.y + 10));
  5. }
  6. void Bullet::dextoryBullet() //销毁子弹
  7. {
  8. for (auto it = bullet.begin(); it != bullet.end(); it++)
  9. {
  10. if (it->y < 0)
  11. {
  12. bullet.erase(it);
  13. break;
  14. }
  15. }
  16. }

game.h文件

  1. #pragma once
  2. #include"airplan.h"
  3. #include"bullet.h"
  4. class game
  5. {
  6. int wid;
  7. int height;
  8. int grade;
  9. int px; //图片大小
  10. TCHAR tc[30]; //输入文字提示
  11. vector<IMAGE> ving; //图片
  12. airplan plan;
  13. Bullet bullet;
  14. public:
  15. game(int wid, int height);
  16. ~game();
  17. //初始化
  18. void init();
  19. //生成敌机
  20. void spawnEnemy();
  21. //移动
  22. void control();
  23. //飞机撞子弹
  24. bool bulletCollide(airplan& plan, int px);
  25. //游戏是否结束
  26. bool isGameOver();
  27. void draw();
  28. //刷新
  29. void updata(airplan& plan, Bullet& bullet);
  30. //开始游戏
  31. void start();
  32. };

game.cpp文件

  1. #include "game.h"
  2. game::game(int wid, int height) :wid(wid), height(height)
  3. {
  4. initgraph(wid, height);
  5. px = 40;
  6. ving.resize(3);
  7. loadimage(&ving[0], _T("res/1.jpg"), px, px);
  8. loadimage(&ving[1], _T("res/2.jpg"), px, px);
  9. loadimage(&ving[2], _T("res/3.jpg"), px, px);
  10. grade = 0;
  11. plan = {
  12. wid / 2, height - px * 2 }; //飞机在中间
  13. }
  14. game::~game()
  15. {
  16. closegraph();
  17. }
  18. void game::init()
  19. {
  20. grade = 0;
  21. plan = {
  22. wid / 2, height - px * 2 }; //飞机在中间
  23. bullet.getBullet().clear();
  24. plan.getEnemyPlan().clear();
  25. }
  26. //生成敌机
  27. void game::spawnEnemy()
  28. {
  29. static int x = 0; // 敌机的数量
  30. if (x >= 20)
  31. {
  32. if (plan.getEnemyPlan().size() < 5)
  33. {
  34. plan.getEnemyPlan().push_back(node(rand() % (wid - px) + px / 2, 0));
  35. }
  36. x = 0;
  37. }
  38. x++;
  39. }
  40. //移动
  41. void game::control()
  42. {
  43. int speed = 4;
  44. if (GetAsyncKeyState(VK_UP) || GetAsyncKeyState('w'))
  45. {
  46. if (plan.getPlan().y > px / 2)
  47. plan.getPlan().y -= speed;
  48. }
  49. if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('d'))
  50. {
  51. if (plan.getPlan().x < wid - px)
  52. plan.getPlan().x += speed;
  53. }
  54. if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('a'))
  55. {
  56. if (plan.getPlan().x > 0)
  57. plan.getPlan().x -= speed;
  58. }
  59. if (GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState('s'))
  60. {
  61. if (plan.getPlan().y < height - px)
  62. plan.getPlan().y += speed;
  63. }
  64. if (_kbhit())
  65. {
  66. if (_getch() == (VK_SPACE))
  67. {
  68. bullet.spwnBullet(plan.getPlan());
  69. }
  70. }
  71. }
  72. //飞机撞子弹
  73. bool game::bulletCollide(airplan& plan, int px)
  74. {
  75. for (auto p = bullet.getBullet().begin(); p != bullet.getBullet().end(); p++)
  76. {
  77. for (auto en = plan.getEnemyPlan().begin(); en != plan.getEnemyPlan().end(); en++)
  78. {
  79. if (plan.planeColide(*p, *en, px))
  80. {
  81. bullet.getBullet().erase(p);
  82. plan.getEnemyPlan().erase(en);
  83. return true;
  84. }
  85. }
  86. }
  87. return false;
  88. }
  89. //游戏是否结束
  90. bool game::isGameOver()
  91. {
  92. for (auto p : plan.getEnemyPlan())
  93. {
  94. if (plan.planeColide(plan.getPlan(), p, px))
  95. {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. void game::draw()
  102. {
  103. BeginBatchDraw();
  104. cleardevice();
  105. for (auto p : plan.getEnemyPlan())
  106. {
  107. putimage(p.x, p.y, &ving[0]);
  108. }
  109. for (auto p : bullet.getBullet())
  110. {
  111. putimage(p.x, p.y, &ving[2]);
  112. }
  113. putimage(plan.getPlan().x, plan.getPlan().y, &ving[1]);
  114. wsprintf(tc, _T("score:%d"), grade);
  115. outtextxy(wid / 2, px, tc);
  116. EndBatchDraw();
  117. }
  118. //刷新
  119. void game::updata(airplan& plan, Bullet& bullet)
  120. {
  121. for (auto& p : plan.getEnemyPlan())
  122. {
  123. p.y += 2;
  124. }
  125. for (auto& p : bullet.getBullet())
  126. {
  127. p.y -= 8;
  128. }
  129. if (bulletCollide(plan, px))
  130. grade++;
  131. plan.destoryEnemy(height);
  132. bullet.dextoryBullet();
  133. spawnEnemy();
  134. }
  135. //开始游戏
  136. void game::start()
  137. {
  138. while (true)
  139. {
  140. updata(plan, bullet);
  141. control();
  142. draw();
  143. if (isGameOver())
  144. {
  145. if (MessageBox(GetForegroundWindow(), _T("是否开始新的游戏"), _T("游戏结束"), MB_YESNO) == IDYES)
  146. {
  147. init();
  148. }
  149. else
  150. break;
  151. }
  152. Sleep(20);
  153. }
  154. }

main.cpp文件

  1. #include<iostream>
  2. #include"game.h"
  3. using namespace std;
  4. int main()
  5. {
  6. game play(360, 630);
  7. play.start();
  8. system("pause");
  9. return 0;
  10. }

素材

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
我这里是把图片放在res文件面的。

发表评论

表情:
评论列表 (有 0 条评论,305人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java飞机

    一、项目分析 1、设计步骤 在一个Java Swing的小游戏中,首先需要考虑的是组件的绘制,其次是如何让需要运动的组件运动起来,最后是如何检测物体的碰撞等。 (1)