欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)飛機(jī)大戰(zhàn)

 更新時(shí)間:2020年11月30日 14:30:47   作者:塵客-追夢(mèng)  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C++實(shí)現(xiàn)飛機(jī)大戰(zhàn)的具體代碼,供大家參考,具體內(nèi)容如下

開(kāi)發(fā)工具

vs2019(vs系列都可以),easyx圖形庫(kù)

效果展示

源代碼

一些頭文件

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; //自己的飛機(jī)
 int px;  //飛機(jī)大小
 list<node> enemyPlan; //敵機(jī)
public:
 airplan();
 airplan(int x, int y);
 list<node>& getEnemyPlan()
 {
 return enemyPlan;
 }
 node& getPlan()
 {
 return Plan;
 }
 bool planeColide(node& v1, node& v2, int px); //飛機(jī)碰撞
 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當(dāng)前位置的數(shù)據(jù)
 break;
 }
 }
}

bullet.h文件

#pragma once
#include"myhelp.h"
#include"airplan.h"
class Bullet
{
 list<node> bullet; //子彈節(jié)點(diǎn)
public:
 list<node>& getBullet()
 {
 return bullet;
 }
 void spwnBullet(node& plan); //發(fā)射子彈
 void dextoryBullet(); //銷毀子彈
};

bullet.cpp文件

#include "bullet.h"

void Bullet::spwnBullet(node& plan) //發(fā)射子彈
{
 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();
 //生成敵機(jī)
 void spawnEnemy();
 //移動(dòng)
 void control();
 //飛機(jī)撞子彈
 bool bulletCollide(airplan& plan, int px);
 //游戲是否結(jié)束
 bool isGameOver();
 void draw();
 //刷新
 void updata(airplan& plan, Bullet& bullet);
 //開(kāi)始游戲
 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 };  //飛機(jī)在中間
}
game::~game()
{
 closegraph();
}
void game::init()
{
 grade = 0;
 plan = { wid / 2, height - px * 2 };  //飛機(jī)在中間
 bullet.getBullet().clear();
 plan.getEnemyPlan().clear();
}
//生成敵機(jī)
void game::spawnEnemy()
{
 static int x = 0; // 敵機(jī)的數(shù)量
 if (x >= 20)
 {
 if (plan.getEnemyPlan().size() < 5)
 {
  plan.getEnemyPlan().push_back(node(rand() % (wid - px) + px / 2, 0));
 }
 x = 0;
 }
 x++;
}
//移動(dòng)
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());
 }
 }
}
//飛機(jī)撞子彈
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;
}
//游戲是否結(jié)束
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();
}
//開(kāi)始游戲
void game::start()
{
 while (true)
 {
 updata(plan, bullet);
 control();
 draw();
 if (isGameOver())
 {
  if (MessageBox(GetForegroundWindow(), _T("是否開(kāi)始新的游戲"), _T("游戲結(jié)束"), 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;
}

素材

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論