C++实现飞机大战

时间:2022-09-04 11:16:56

本文实例为大家分享了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++实现飞机大战

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

原文链接:https://blog.csdn.net/qq_45893999/article/details/110298551