C++實現飛機大戰
阿新 • • 發佈:2020-12-01
本文例項為大家分享了C++實現飛機大戰的具體程式碼,供大家參考,具體內容如下
開發工具
vs2019(vs系列都可以),easyx圖形庫
效果展示
原始碼
一些標頭檔案
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,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); loadimage(&ving[2],_T("res/3.jpg"),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,&ving[2]); } putimage(plan.getPlan().x,plan.getPlan().y,&ving[1]); wsprintf(tc,_T("score:%d"),grade); outtextxy(wid / 2,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; }
素材
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。