飞机大战初步功能实现:
1.开始菜单界面(可更改地图)
2.游戏界面(移动飞机、发射子弹、击毁敌机、撞毁)
3.结束界面(可返回开始菜单)
功能截图如下:
程序代码如下:
项目工程组成
mainscene.h:
#ifndef MAINSCENE_H
#define MAINSCENE_H
#include
#include"config.h"
#include
#include
#include
#include"map.h"
#include
#include"heroplane.h"
#include
#include"bullet.h"
#include"enemyplane.h"
#include"bomb.h"
#include
#include
#include
#include"mythread.h"
#include"dickmenu.h"
#include
class mainScene : public QWidget
{
Q_OBJECT
public:
mainScene(QWidget *parent = nullptr);
~mainScene();
void InitScene();
QMutex m_mutex;
QTimer m_timer;
bool StartPaintBullet=true;
bool StartCheckEnemy=true;
void playGame();
void updatePosition();
void paintEvent(QPaintEvent *);
map M_map;
HeroPlane hero_plane;
void mouseMoveEvent(QMouseEvent *);
void enemyToscreen();
EnemyPlane m_enemyplanes[7];
EnemyPlane m_enemies[ENEMY_NUM];
int enemy_recorder;
void collisionDetection();
Bomb m_bombs[Bomb_NUM];
MyThread *thread;
signals:
void ReturnMenu();
};
#endif
heroplane.h:
#ifndef HEROPLANE_H
#define HEROPLANE_H
#include
#include
#include"bullet.h"
class HeroPlane
{
public:
HeroPlane();
void shoot();
void setPlanePosition(int x,int y);
bool hero_free;
QPixmap m_Plane;
int plane_x;
int plane_y;
QRect plane_rect;
bullet m_bullets[bullet_num];
int m_recorder;
};
#endif
enemyplane.h:
#ifndef ENEMYPLANE_H
#define ENEMYPLANE_H
#include
#include"config.h"
#include
class EnemyPlane
{
public:
EnemyPlane();
void updateEnemyPosition();
QPixmap m_enemy;
int enemy_x;
int enemy_y;
QRect enemy_rect;
bool enemy_free;
int enemy_speed;
};
#endif
startgame.h:
#ifndef STARTGAME_H
#define STARTGAME_H
#include
#include
#include"mainscene.h"
namespace Ui {
class StartGame;
}
class StartGame : public QDialog
{
Q_OBJECT
public:
explicit StartGame(QWidget *parent = nullptr);
~StartGame();
QString MapStyle=":/new/prefix1/res/img_bg_level_1.jpg";
QString EnemyPlaneStyle=QString(":/new/prefix1/res/img-plane_%1.png").arg(rand()%10);
void changeMap();
public slots:
void on_pushButtonStart_clicked();
void on_pushButtonStart_Design_clicked();
void on_pushButtonStart_Exit_clicked();
void on_pushButtonSureDeSign_clicked();
private slots:
private:
Ui::StartGame *ui;
};
#endif
bomb.h:
#ifndef BOMB_H
#define BOMB_H
#include
#include
#include"config.h"
class Bomb
{
public:
Bomb();
void updateInfo();
QVector<QPixmap>m_pixArr;
int Bomb_x;
int Bomb_y;
bool Bomb_free;
int Bomb_recorder;
int bombing_index;
};
#endif
bullet.h:
#ifndef BULLET_H
#define BULLET_H
#include"config.h"
#include
class bullet
{
public:
bullet();
void updateBulletPosition();
QPixmap m_bullet;
int bullet_x;
int bullet_y;
int bullet_speed;
bool bullet_free;
QRect bullet_rect;
};
#endif
dickmenu.h:
#ifndef DICKMENU_H
#define DICKMENU_H
#include"startgame.h"
#include
namespace Ui {
class DickMenu;
}
class DickMenu : public QDialog
{
Q_OBJECT
public:
explicit DickMenu(QWidget *parent = nullptr);
~DickMenu();
public slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::DickMenu *ui;
signals:
void returnWindow();
};
#endif
map:
#ifndef MAP_H
#define MAP_H
#include
#include
class map
{
public:
map();
void mapPosition();
QPixmap m_map1;
QPixmap m_map2;
int m_map1_posY;
int m_map2_posY;
int m_scroll_speed;
};
#endif
mythread.h:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = nullptr);
void dicked();
protected:
void run();
signals:
void isDone();
};
#endif
config.h:
#ifndef CONFIG_H
#define CONFIG_H
#define GAME_Width 512
#define GAME_Heigth 768
#define GAME_Title "飞机大战"
#define GAME_ICON ":/new/prefix1/res/app.ico"
#define GAME_Rate 10
#define Map_Path ":/new/prefix1/res/img_bg_level_2.jpg"
#define Map_Scroll_Speed 2
#define Plane_Model ":/new/prefix1/res/hero.png"
#define bullet_model ":/new/prefix1/res/bullet_11.png"
#define BULLET_SPEED 5
#define bullet_num 30
#define bullet_interval 20
#define enemyplane_model ":/new/prefix1/res/img-plane_7.png"
#define ENEMY_SPEED 3
#define ENEMY_NUM 50
#define ENEMY_INTERVAL 30
#define Bomb_model ":/new/prefix1/res/bomb-%1.png"
#define Bomb_NUM 20
#define Bomb_max 7
#define Bomb_Interval 10
#define Sound_backgroud "D:/qt/PlaneWar/res/bg.wav"
#define Sound_bomb "D:/qt/PlaneWar/res/bomb.wav"
#endif
mainscene.cpp:
#include "mainscene.h"
#include
#include
#include
mainScene::mainScene(QWidget *parent)
: QWidget(parent)
{
InitScene();
playGame();
thread=new MyThread();
connect(thread,&MyThread::isDone,[=]()
{
this->close();
});
}
mainScene::~mainScene()
{
}
void mainScene::InitScene()
{
setFixedSize(GAME_Width,GAME_Heigth);
setWindowTitle(GAME_Title);
setWindowIcon(QIcon(GAME_ICON));
m_timer.setInterval(GAME_Rate);
enemy_recorder=0;
srand((unsigned int)time(NULL));
}
void mainScene::playGame()
{
m_timer.start();
QSound::play("D:/qt/PlaneWar/res/bg.wav");
connect(&m_timer,&QTimer::timeout,[=](){
enemyToscreen();
updatePosition();
update();
collisionDetection();
});
}
void mainScene::updatePosition()
{
M_map.mapPosition();
hero_plane.shoot();
for(int i=0;i<bullet_num;i++)
{
if(hero_plane.m_bullets[i].bullet_free==false)
{
hero_plane.m_bullets[i].updateBulletPosition();
}
}
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_enemies[i].enemy_free==false)
{
m_enemies[i].updateEnemyPosition();
}
}
for(int i=0;i<Bomb_NUM;i++)
{
if(m_bombs[i].Bomb_free==false)
{
m_bombs[i].updateInfo();
}
}
}
void mainScene::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.drawPixmap(0,M_map.m_map1_posY,M_map.m_map1);
painter.drawPixmap(0,M_map.m_map2_posY,M_map.m_map2);
if(hero_plane.hero_free==false)
{
painter.drawPixmap(hero_plane.plane_x,hero_plane.plane_y,hero_plane.m_Plane);
}
if(StartPaintBullet==true)
{
for(int i=0;i<bullet_num;i++)
{
if(hero_plane.m_bullets[i].bullet_free==false)
{
painter.drawPixmap(hero_plane.m_bullets[i].bullet_x,hero_plane.m_bullets[i].bullet_y,
hero_plane.m_bullets[i].m_bullet);
}
}
}
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_enemies[i].enemy_free==false)
{
painter.drawPixmap(m_enemies[i].enemy_x,
m_enemies[i].enemy_y,
m_enemies[i].m_enemy);
}
}
for(int i=0;i<Bomb_NUM;i++)
{
if(m_bombs[i].Bomb_free==false)
{
painter.drawPixmap(m_bombs[i].Bomb_x,m_bombs[i].Bomb_y,
m_bombs[i].m_pixArr[m_bombs[i].bombing_index]);
}
}
}
void mainScene::mouseMoveEvent(QMouseEvent *event)
{
int x=event->x()-hero_plane.plane_rect.width()*0.5;
int y=event->y()-hero_plane.plane_rect.height()*0.5;
if(x<=0)
{
x=0;
}
if(x>GAME_Width-hero_plane.plane_rect.width())
{
x=GAME_Width-hero_plane.plane_rect.width();
}
if(y<=0)
{
y=0;
}
if(y>=GAME_Heigth-hero_plane.plane_rect.height())
{
y=GAME_Heigth-hero_plane.plane_rect.height();
}
hero_plane.setPlanePosition(x,y);
}
void mainScene::enemyToscreen()
{
enemy_recorder++;
if(enemy_recorder<ENEMY_INTERVAL)
{
return;
}
enemy_recorder=0;
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_enemies[i].enemy_free)
{
m_enemies[i].enemy_free=false;
m_enemies[i].enemy_x=rand()%(GAME_Width-m_enemies[i].enemy_rect.width());
m_enemies[i].enemy_y=-m_enemies[i].enemy_rect.height();
break;
}
}
}
void mainScene::collisionDetection()
{
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_enemies[i].enemy_free)
{
continue;
}
if(hero_plane.plane_rect.intersects(m_enemies[i].enemy_rect))
{
hero_plane.hero_free=true;
StartPaintBullet=false;
StartCheckEnemy=false;
hero_plane.plane_rect.setHeight(0);
hero_plane.plane_rect.setWidth(0);
for(int k=0;k<Bomb_NUM;k++)
{
if(m_bombs[k].Bomb_free)
{
m_bombs[k].Bomb_free=false;
m_bombs[k].Bomb_x=hero_plane.plane_x;
m_bombs[k].Bomb_y=hero_plane.plane_y;
break;
}
}
thread->start();
}
if(StartCheckEnemy==true)
{
for(int j=0;j<bullet_num;j++)
{
if(hero_plane.m_bullets[j].bullet_free)
{
continue;
}
if(m_enemies[i].enemy_rect.intersects(hero_plane.m_bullets[j].bullet_rect))
{
m_enemies[i].enemy_free=true;
hero_plane.m_bullets[j].bullet_free=true;
for(int k=0;k<Bomb_NUM;k++)
{
if(m_bombs[k].Bomb_free)
{
m_bombs[k].Bomb_free=false;
m_bombs[k].Bomb_x=m_enemies[i].enemy_x;
m_bombs[k].Bomb_y=m_enemies[i].enemy_y;
break;
}
}
}
}
}
}
}
heroplane.cpp:
#include "heroplane.h"
#include"config.h"
#include"map.h"
HeroPlane::HeroPlane()
{
m_Plane.load(Plane_Model);
plane_x=(GAME_Width-m_Plane.width())*0.5;
plane_y=GAME_Heigth-m_Plane.height();
plane_rect.setWidth(m_Plane.width());
plane_rect.setHeight(m_Plane.height());
plane_rect.moveTo(plane_x,plane_y);
hero_free=false;
m_recorder=0;
}
void HeroPlane::shoot()
{
m_recorder++;
if(m_recorder<bullet_interval)
{
return;
}
m_recorder=0;
for(int i=0;i<bullet_num;i++)
{
if(m_bullets[i].bullet_free)
{
m_bullets[i].bullet_free=false;
m_bullets[i].bullet_x=plane_x+plane_rect.width()*0.5-10;
m_bullets[i].bullet_y=plane_y-25;
break;
}
}
}
void HeroPlane::setPlanePosition(int x, int y)
{
plane_x=x;
plane_y=y;
plane_rect.moveTo(plane_x,plane_y);
}
enemyplane.cpp:
#include "enemyplane.h"
#include"startgame.h"
#include
EnemyPlane::EnemyPlane()
{
StartGame *str;
str=new StartGame();
m_enemy.load(str->EnemyPlaneStyle);
enemy_x=0;
enemy_y=0;
enemy_free=true;
enemy_speed=ENEMY_SPEED;
enemy_rect.setWidth(m_enemy.width());
enemy_rect.setHeight(m_enemy.height());
enemy_rect.moveTo(enemy_x,enemy_y);
}
void EnemyPlane::updateEnemyPosition()
{
if(enemy_free)
{
return;
}
enemy_y+=enemy_speed;
enemy_rect.moveTo(enemy_x,enemy_y);
if(enemy_y>GAME_Heigth)
{
enemy_free=true;
}
}
startgame.cpp:
#include "startgame.h"
#include "ui_startgame.h"
#include"QDebug"
static int Map=1;
StartGame::StartGame(QWidget *parent) :
QDialog(parent),
ui(new Ui::StartGame)
{
ui->setupUi(this);
this->setWindowTitle("飞机大战");
ui->stackedWidget->setCurrentWidget(ui->pageStart);
ui->spinBox->setMaximum(5);
ui->spinBox->setValue(5);
}
StartGame::~StartGame()
{
delete ui;
}
void StartGame::on_pushButtonStart_clicked()
{
this->close();
mainScene *m;
m=new mainScene();
m->show();
}
void StartGame::on_pushButtonStart_Design_clicked()
{
ui->stackedWidget->setCurrentWidget(ui->page_2);
}
void StartGame::on_pushButtonStart_Exit_clicked()
{
exit(0);
}
void StartGame::on_pushButtonSureDeSign_clicked()
{
ui->stackedWidget->setCurrentWidget(ui->pageStart);
Map=ui->spinBox->text().toInt();
}
void StartGame::changeMap()
{
MapStyle=QString(":/new/prefix1/res/img_bg_level_%1.jpg").arg(Map);
}
bomb.cpp:
#include "bomb.h"
#include
Bomb::Bomb()
{
for(int i=1;i<=Bomb_max;i++)
{
QString str=QString(Bomb_model).arg(i);
m_pixArr.push_back(QPixmap(str));
}
Bomb_x=0;
Bomb_y=0;
Bomb_free=true;
bombing_index=0;
Bomb_recorder=0;
}
void Bomb::updateInfo()
{
if(Bomb_free)
{
return;
}
Bomb_recorder++;
if(Bomb_recorder<Bomb_Interval)
{
return;
}
Bomb_recorder=0;
bombing_index++;
if(bombing_index>Bomb_max-1)
{
bombing_index=0;
Bomb_free=true;
}
}
bullet.cpp:
#include "bullet.h"
#include
bullet::bullet()
{
m_bullet.load(bullet_model);
bullet_x=(GAME_Width-m_bullet.width())*0.5;
bullet_y=GAME_Heigth;
bullet_free=true;
bullet_speed=BULLET_SPEED;
bullet_rect.setWidth(m_bullet.width());
bullet_rect.setHeight(m_bullet.height());
bullet_rect.moveTo(bullet_x,bullet_y);
}
void bullet::updateBulletPosition()
{
if(bullet_free)
{
return;
}
bullet_y-=bullet_speed;
bullet_rect.moveTo(bullet_x,bullet_y);
if(bullet_y<=0)
{
bullet_free=true;
}
}
dickmenu.cpp:
#include "dickmenu.h"
#include "ui_dickmenu.h"
#include
DickMenu::DickMenu(QWidget *parent) :
QDialog(parent),
ui(new Ui::DickMenu)
{
ui->setupUi(this);
this->setWindowTitle("游戏失败");
}
DickMenu::~DickMenu()
{
delete ui;
}
void DickMenu::on_pushButton_clicked()
{
StartGame *st;
st=new StartGame();
st->show();
this->close();
emit returnWindow();
}
void DickMenu::on_pushButton_2_clicked()
{
exit(0);
}
map.cpp:
#include "map.h"
#include"config.h"
#include"startgame.h"
#include
map::map()
{
StartGame *str;
str=new StartGame();
str->changeMap();
m_map1.load(str->MapStyle);
m_map2.load(str->MapStyle);
m_map1_posY=-GAME_Heigth;
m_map2_posY=0;
m_scroll_speed=Map_Scroll_Speed;
}
void map::mapPosition()
{
m_map1_posY+=m_scroll_speed;
if(m_map1_posY>=0)
{
m_map1_posY=-GAME_Heigth;
}
m_map2_posY+=m_scroll_speed;
if(m_map2_posY>=GAME_Heigth)
{
m_map2_posY=0;
}
}
mythread.cpp:
#include "mythread.h"
#include"QMessageBox"
#include"dickmenu.h"
MyThread::MyThread(QObject *parent) : QThread(parent)
{
connect(this,&MyThread::isDone,this,&MyThread::dicked);
}
void MyThread::run()
{
sleep(2);
emit isDone();
}
void MyThread::dicked()
{
DickMenu *dick;
dick=new DickMenu;
dick->show();
}
main.cpp:
#include "mainscene.h"
#include
#include"startgame.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StartGame st;
st.show();
return a.exec();
}