c/c++语言开发共享C++实现飞机大战

本文实例为大家分享了c++实现飞机大战的具体代码,供大家参考,具体内容如下开发工具vs2019(vs系列都可以),easyx图形库效果展示源代码一些头文件myhelp.h文件#pragma once#

c/c++开发分享C++实现飞机大战实例为大家分享了c++实现飞机大战的具体代码,供大家参考,具体内容如下

开发工具

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

效果展示

C++实现飞机大战

C++实现飞机大战

源代码

一些头文件

myhelp.h文件

  #pragma once  #include<easyx.h>  #include<conio.h>  #include<list>  #include<vector>  #include<iostream>  using namespace std;  struct node  {   int x, y;   node(int x, int y) :x(x), y(y) {}   node() { x = 0; y = 0; }  };

airplan.h文件

  #pragma once  #include"myhelp.h"  class airplan  {   node plan; //自己的飞机   int px;  //飞机大小   list<node> enemyplan; //敌机  public:   airplan();   airplan(int x, int y);   list<node>& getenemyplan()   {   return enemyplan;   }   node& getplan()   {   return plan;   }   bool planecolide(node& v1, node& v2, int px); //飞机碰撞   void destoryenemy(int height);  };

airplan.cpp文件

  #include "airplan.h"  airplan::airplan(int x, int y) :plan(x, y)  {   px = 40;  }  airplan::airplan(){}  bool airplan::planecolide(node& v1, node& v2, int px)  {   int x = abs(v1.x - v2.x);   int y = abs(v1.y - v2.y);   return y < px&& x < px;  }  void airplan::destoryenemy(int height)  {   for (auto it = enemyplan.begin(); it != enemyplan.end(); it++)   {   if (it->y > height)   {   enemyplan.erase(it);//删除it当前位置的数据   break;   }   }  }

bullet.h文件

  #pragma once  #include"myhelp.h"  #include"airplan.h"  class bullet  {   list<node> bullet; //子弹节点  public:   list<node>& getbullet()   {   return bullet;   }   void spwnbullet(node& plan); //发射子弹   void dextorybullet(); //销毁子弹  };

bullet.cpp文件

  #include "bullet.h"    void bullet::spwnbullet(node& plan) //发射子弹  {   bullet.push_back(node(plan.x, plan.y + 10));  }  void bullet::dextorybullet() //销毁子弹  {   for (auto it = bullet.begin(); it != bullet.end(); it++)   {   if (it->y < 0)   {    bullet.erase(it);    break;   }   }  }

game.h文件

  #pragma once  #include"airplan.h"  #include"bullet.h"  class game  {   int wid;   int height;   int grade;   int px;  //图片大小   tchar tc[30]; //输入文字提示   vector<image> ving; //图片   airplan plan;   bullet bullet;  public:   game(int wid, int height);   ~game();   //初始化   void init();   //生成敌机   void spawnenemy();   //移动   void control();   //飞机撞子弹   bool bulletcollide(airplan& plan, int px);   //游戏是否结束   bool isgameover();   void draw();   //刷新   void updata(airplan& plan, bullet& bullet);   //开始游戏   void start();  };

game.cpp文件

  #include "game.h"  game::game(int wid, int height) :wid(wid), height(height)  {   initgraph(wid, height);   px = 40;   ving.resize(3);   loadimage(&ving[0], _t("res/1.jpg"), px, px);   loadimage(&ving[1], _t("res/2.jpg"), px, px);   loadimage(&ving[2], _t("res/3.jpg"), px, px);   grade = 0;   plan = { wid / 2, height - px * 2 };  //飞机在中间  }  game::~game()  {   closegraph();  }  void game::init()  {   grade = 0;   plan = { wid / 2, height - px * 2 };  //飞机在中间   bullet.getbullet().clear();   plan.getenemyplan().clear();  }  //生成敌机  void game::spawnenemy()  {   static int x = 0; // 敌机的数量   if (x >= 20)   {   if (plan.getenemyplan().size() < 5)   {    plan.getenemyplan().push_back(node(rand() % (wid - px) + px / 2, 0));   }   x = 0;   }   x++;  }  //移动  void game::control()  {   int speed = 4;   if (getasynckeystate(vk_up) || getasynckeystate('w'))   {   if (plan.getplan().y > px / 2)    plan.getplan().y -= speed;   }   if (getasynckeystate(vk_right) || getasynckeystate('d'))   {   if (plan.getplan().x < wid - px)    plan.getplan().x += speed;   }   if (getasynckeystate(vk_left) || getasynckeystate('a'))   {   if (plan.getplan().x > 0)    plan.getplan().x -= speed;   }   if (getasynckeystate(vk_down) || getasynckeystate('s'))   {   if (plan.getplan().y < height - px)    plan.getplan().y += speed;   }   if (_kbhit())   {   if (_getch() == (vk_space))   {    bullet.spwnbullet(plan.getplan());   }   }  }  //飞机撞子弹  bool game::bulletcollide(airplan& plan, int px)  {   for (auto p = bullet.getbullet().begin(); p != bullet.getbullet().end(); p++)   {   for (auto en = plan.getenemyplan().begin(); en != plan.getenemyplan().end(); en++)   {    if (plan.planecolide(*p, *en, px))    {    bullet.getbullet().erase(p);    plan.getenemyplan().erase(en);    return true;    }   }   }   return false;  }  //游戏是否结束  bool game::isgameover()  {   for (auto p : plan.getenemyplan())   {   if (plan.planecolide(plan.getplan(), p, px))   {    return true;   }   }   return false;  }  void game::draw()  {   beginbatchdraw();   cleardevice();   for (auto p : plan.getenemyplan())   {   putimage(p.x, p.y, &ving[0]);   }   for (auto p : bullet.getbullet())   {   putimage(p.x, p.y, &ving[2]);   }   putimage(plan.getplan().x, plan.getplan().y, &ving[1]);   wsprintf(tc, _t("score:%d"), grade);   outtextxy(wid / 2, px, tc);   endbatchdraw();  }  //刷新  void game::updata(airplan& plan, bullet& bullet)  {   for (auto& p : plan.getenemyplan())   {   p.y += 2;   }   for (auto& p : bullet.getbullet())   {   p.y -= 8;   }   if (bulletcollide(plan, px))   grade++;   plan.destoryenemy(height);   bullet.dextorybullet();   spawnenemy();  }  //开始游戏  void game::start()  {   while (true)   {   updata(plan, bullet);   control();   draw();   if (isgameover())   {    if (messagebox(getforegroundwindow(), _t("是否开始新的游戏"), _t("游戏结束"), mb_yesno) == idyes)    {    init();    }    else    break;   }   sleep(20);   }  }

main.cpp文件

  #include<iostream>  #include"game.h"  using namespace std;  int main()  {   game play(360, 630);   play.start();   system("pause");   return 0;  }

素材

C++实现飞机大战

C++实现飞机大战

C++实现飞机大战

以上就是c/c++开发分享C++实现飞机大战的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/596536.html

(0)
上一篇 2021年5月8日
下一篇 2021年5月8日

精彩推荐