Qt之飞机大战

由于一个小比赛,写了一个c++的飞机大战,不过个人感觉不太满意

第一是对于Qt的学习还很初步,第二个对于c++的继承的思想没有很好的

理解,第三点便是命名规范不太好。

所以这次的程序仅供参考。

代码如下

工程文件

#-------------------------------------------------
#
# Project created by QtCreator 2019-06-02T11:17:26
#
#-------------------------------------------------

QT       += core gui
QT +=multimedia

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = projectplany01
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        background.cpp \
        bossplany.cpp \
        eneumplany.cpp \
        gameover.cpp \
        main.cpp \
        myplany.cpp \
        name.cpp \
        playbuttle.cpp \
        playwindow.cpp \
        srengine.cpp \
        widget.cpp

HEADERS += \
        background.h \
        bossplany.h \
        eneumplany.h \
        gameover.h \
        myplany.h \
        name.h \
        playbuttle.h \
        playwindow.h \
        srengine.h \
        widget.h

FORMS += \
        widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    image.qrc
RC_ICONS=a.ico

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

初始界面widget

#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QPaintEvent>
#include<QPainter>
#include<QGraphicsView>
#include<playwindow.h>
#include<QLabel>
#include"name.h"
#include<QVBoxLayout>
#include"srengine.h"
namespace Ui {
class Widget;
}
//开始界面
class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    void paintEvent(QPaintEvent *e);
    ~Widget();
private slots:
    void change();
    void help();
    void leardboardslots();
private:
    Ui::Widget *ui;
    playwindow *play_window;
    QLabel *label;
    name *play_name;
    QWidget *leard_widget;
    SREngine *srengine;
};

#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
//初始界面cha
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
     play_window=new playwindow;
    play_name= new name;
    play_name->setWindowFlag(Qt::WindowStaysOnTopHint);
    play_name->show();
    ui->setupUi(this);
    this->resize(800,600);
    this->setWindowFlag(Qt::FramelessWindowHint);//去掉边框加置顶
    ui->pushButton->resize(140,50);
    ui->pushButton->setFlat(true);
    ui->pushButton_2->setFlat(true);
    ui->pushButton_2->resize(140,50);
    ui->pushButton_3->setFlat(true);
    ui->pushButton_3->resize(140,50);
    ui->pushButton_4->resize(140,50);
    ui->pushButton_4->setFlat(true);
    connect(ui->pushButton_2,&QPushButton::clicked,this,&QWidget::close);
    connect(ui->pushButton,&QPushButton::clicked,this,&Widget::change);
    connect(ui->pushButton_3,&QPushButton::clicked,this,&Widget::help);
    connect(ui->pushButton_4,&QPushButton::clicked,this,&Widget::leardboardslots);
    //srengine = new SREngine;
}
//背景图
void Widget::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    p.drawPixmap(0,0,width(),height(),QPixmap(":/new/prefix1/Image/68396.jpg"));
}
void Widget::help()
{
    label= new QLabel;
    label->resize(450,400);
    label->setWindowFlag(Qt::WindowStaysOnTopHint,true);
    label->setWindowTitle(QStringLiteral("帮助"));
    label->setText(QStringLiteral("      欢迎来到飞机大战1.0版本\n      进入游戏后,鼠标点击界面解锁你的飞机\n       w a s d对应飞机上下左右"
                         "空格发射子弹 z可以消除boss子弹\n     由于空间原因 您的子弹可以陷入空间乱流请注意"));
    label->show();
}
//排行榜
void Widget::leardboardslots()
{
 leard_widget= new QWidget;
 leard_widget->setWindowTitle(QStringLiteral("排行榜"));
 leard_widget->resize(400,400);
 QFile *f= new QFile("D:/1.txt");
 int x=140;
 int y=100;
 QString s;
 if(!f->open(QIODevice::ReadWrite|QIODevice::Text))
 {
        qDebug()<<"not open file";
 }
            while(!f->atEnd())
            {
             QByteArray line = f->read(1);
             if(QString(line)!='\n')
             s.push_back(line);
            }
qDebug()<<s<<endl;
        f->close();
        delete f;
        leard_widget->show();
}
Widget::~Widget()
{
    delete ui;
}
//显示游戏界面
void Widget::change()
{
    this->hide();
    play_window->show();
}

游戏界面代码:

#include "playwindow.h"
#include<QGraphicsPixmapItem>
#include<QDebug>
playwindow::playwindow()
{
    game_over= new class gameover;
    this->resize(WINDOWWIDTH,WINDOWLONG);//界面大小
    back_ground=new background;//析构
    this->setScene(back_ground);
    setWindowTitle(QStringLiteral("飞机大战"));
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//去除滑动轮
    setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    connect(back_ground,&background::plant_shape,this,&playwindow::gameover);
}
//游戏结束
void playwindow::gameover()
{
    back_ground->addItem(game_over);
    this->setEnabled(false);
}

playwindow::~playwindow()
{
   delete back_ground;
}
#ifndef PLAYWINDOW_H
#define PLAYWINDOW_H
#include<QGraphicsView>
#include"background.h"
#include"gameover.h"
//游戏界面
const int WINDOWWIDTH=1800;//场景的宽
const int WINDOWLONG=1000;//场景的长
class playwindow:public QGraphicsView
{
    Q_OBJECT
public:
    ~playwindow();
    playwindow();
private:
background *back_ground;//背景照片
class gameover *game_over;
public slots:
void gameover();
};

#endif // PLAYWINDOW_H

游戏背景及一些逻辑

#ifndef BACKGROUND_H
#define BACKGROUND_H
#include<QGraphicsScene>
#include<QPainter>
#include"myplany.h"
#include"eneumplany.h"
#include<QVector>
#include<QTime>
#include<QMediaPlayer>
#include"bossplany.h"
//背景类
struct grade{
    int left;
    int right;
    int leve;
};
class background:public QGraphicsScene
{
    Q_OBJECT
public:
    background();
    ~background();
    myplany *my_plany;//玩家飞机
    QVector<eneumplany*> eneum_plany;//敌机
    grade leave;
private:
    void drawBackground(QPainter *painter, const QRectF &rect);
    void timerEvent(QTimerEvent *event);
     void produce_eneumplany();
     bool plany_impact();
     void eneum_shape();
     void myplany_shape(eneumplany &eneum_plany);
     void boss_initia();
signals:
     void plant_shape();
private:
     bool judge_eneumplany();
     bool judge_eneumplany_init;
     void delete_eneum_plany();
     void delete_shape_buttle();
     int n;
     int boss_n;
     QMediaPlayer *music;
     bossplany *boss_plany;
     bool boss_judge;

};

#endif // BACKGROUND_H
#include "background.h"
#include<QDebug>
#include<QFile>
background::background()
{
    leave.left=1;
    leave.right=10;
    leave.leve=1;
    my_plany=new myplany;//析构
    judge_eneumplany_init=true;
    addItem(my_plany);
    startTimer(50);
    produce_eneumplany();
    n=startTimer(10);
    music= new QMediaPlayer(this);
    connect(music,SIGNAL(positionChanged(qint64)),this,SLOT(postitionChanged(qint64)));
    music->setMedia(QUrl::fromLocalFile("D:\\music\\1111.mp3"));
    music->setVolume(60);
    music->play();
    boss_n=0;
    boss_judge=false;
}
background::~background()
{
    delete music;
    delete my_plany;
    delete boss_plany;
}
//画背景图
void background::drawBackground(QPainter *painter, const QRectF &rect)
{
    painter->drawPixmap(0,0,1800,1000,QPixmap(":/new/prefix1/Image/background.png"));

}
//判断碰撞事件
void background::timerEvent(QTimerEvent *event)
{
    //判断我方飞机是否撞到敌机上
       plany_impact();
       //判断是否生成敌机
    if(judge_eneumplany()&&judge_eneumplany_init&&boss_judge==false)
    {
        produce_eneumplany();
    }
    boss_initia();
    //判断玩家飞机是否死亡
    if(my_plany->blood==0&&my_plany->life)
    {
        my_plany->image=":/new/prefix1/Image/bigairplane3.png";//爆炸图片
        judge_eneumplany_init=false;
        //死后保存分数
        QFile *f=new QFile("D:/1.txt");
        if(!f->open(QIODevice::ReadWrite | QIODevice::Text|QIODevice::Append)) {
        qDebug()<<"Can't open the file!"<<endl;
    }
      QTextStream time(f);
      time<<my_plany->mark<<endl;
      f->close();
      delete f;
      my_plany->life=false;
      plant_shape();
    }
    //判断是否打中敌机
    eneum_shape();
     //判断敌机子弹是否击中玩家的飞机
    for(auto be=eneum_plany.begin();be!=eneum_plany.end();be++)
    {
    myplany_shape(**be);
    }
    //判断子弹是否击中子弹
    if(event->timerId()==n)
    delete_shape_buttle();
}
//生成战机
void background::produce_eneumplany()
{
    srand((unsigned)time(NULL));
    int value=0;//随机生成敌机
    value=rand()%leave.right+leave.left;
    for(int i=0;i<value;i++)
    {

            int x1=rand()%1750+0;
            int y1=-(rand()%100+0);
            eneumplany *plany=new eneumplany(x1,y1,rand());//析构
            eneum_plany.push_back(plany);
            addItem(eneum_plany.back());
    }
    my_plany->mark+=10*value;//分数
}
//判断碰撞
bool background::plany_impact()
{
    for(auto a:eneum_plany)
    {
        if(my_plany->plany_x>=a->plany_x+10&&my_plany->plany_x<=a->plany_x+50&&my_plany->plany_y<=a->plany_y+70
                &&my_plany->plany_y>=a->plany_y+10)
        {
            my_plany->blood--;
            a->blood--;
        }
    }
    return false;
}
//判断子弹是否击中敌机
void background::eneum_shape()
{
    bool break_judge=false;
    for(auto a=my_plany->play_buttle.begin();a!=my_plany->play_buttle.end();a++)
    {
        for(auto b=eneum_plany.begin();b!=eneum_plany.end();b++)
        {
            if(a->buttle_x>=(*b)->plany_x&&a->buttle_x<=(*b)->plany_x+60&&
                    a->buttle_y<=(*b)->plany_y+80&&a->buttle_y>=(*b)->plany_y)
            {
                if((*b)->blood==0)
                {
                    (*b)->life=false;
                    (*b)->image=":/new/prefix1/Image/bigairplane3.png";
                    (*b)->eneum_buttle.clear();
                    eneum_plany.erase(b);//删除敌机
                    break_judge=true;
                    my_plany->mark+=20;//分数加20
                    break;
                }
                  (*b)->blood--;
                  my_plany->play_buttle.erase(a);//删除敌机子弹
                  break_judge=true;
                  break;
            }
        }
        if(break_judge)
        {
            break;
        }
    }
}
//判断是否需要生成敌机
bool background::judge_eneumplany()
{
 qDebug()<<eneum_plany.size()<<endl;
     for(auto a:eneum_plany)
     {
         if(!(a->life))
          continue;
         else
         {
            return false;
         }
     }
     boss_n++;
     eneum_plany.clear();
     return true;
}
//判断子弹是否碰撞
void background::delete_shape_buttle()
{
    for(auto a:eneum_plany)
    {
        for(auto be=a->eneum_buttle.begin();be!=a->eneum_buttle.end();be++)
        {
            for(auto be1=my_plany->play_buttle.begin();be1!=my_plany->play_buttle.end();be1++)
            {
                if(be->buttle_x>=be1->buttle_x-40&&be->buttle_x<=be1->buttle_x+40&&
                        be->buttle_y>=be1->buttle_y+20)
                {
                    my_plany->play_buttle.erase(be1);
                    a->eneum_buttle.erase(be);
                    break;
                }
            }
            break;
        }
    }
}
//判断子弹是否打中玩家飞机
void background::myplany_shape(eneumplany &eneum_plany)
{
         for(auto be1=(eneum_plany).eneum_buttle.begin();be1!=(eneum_plany).eneum_buttle.end();be1++)
         {
             if(be1->buttle_x>=my_plany->plany_x&&be1->buttle_x<=my_plany->plany_x+60&&
                     be1->buttle_y>=my_plany->plany_y&&be1->buttle_y<=my_plany->plany_y+80)
             {
                my_plany->blood--;
                (eneum_plany).eneum_buttle.erase(be1);
                break;
             }
         }
}
//boss初始化
void background::boss_initia()
{
    if(boss_n==10)
    {
        my_plany->blood=10;
        boss_plany =new bossplany(*(my_plany));//需要析构
        this->addItem(boss_plany);
        boss_n++;
        boss_plany->life=true;
        boss_judge=true;
     }
    else {
        if(boss_n>=1&&boss_judge==true)
        {
            if(boss_plany->blood==0)
            {
                delete boss_plany;
                boss_judge=false;
                my_plany->mark+=5000;
            }
        }
    }
}











玩家飞机

#ifndef MYPLANY_H
#define MYPLANY_H
#include<QGraphicsObject>//继承为Qobject
#include<QKeyEvent>
#include<QTimerEvent>
#include<QTimer>
#include<QList>
#include"playbuttle.h"
#include<QString>
//玩家飞机类
class myplany:public QGraphicsObject
{
    Q_OBJECT
public:
    myplany();
    //飞机的当前坐标
 //血量
     int blood;
     QString image;
     int plany_x;
     int plany_y;
     QList<PlayButtle> play_buttle;//析构
     int mark;//分数
      bool life;
      int skill;
private:
     QRectF
      boundingRect() const;
     void keyPressEvent(QKeyEvent *event);
     void  keyReleaseEvent(QKeyEvent* event);
     void paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr);
     void timerEvent(QTimerEvent *event);
     //判断飞机的飞行方向
      bool w_judge;
      bool s_judge;
      bool a_judge;
      bool d_judge;
      bool space_judge;
      bool z_judge;
      void produce_eneumplany();
private:
      int time1;
      int time2;
      static const int buttle_speed=35;
      int speed;
signals:
      void skill_z();
};

#endif // MYPLANY_H
#include "myplany.h"
#include<QPainter>
#include<QDebug>
const int PLANYBOUNDARY_X=1750;
const int PLANYBOUNDARY_Y=930;
myplany::myplany()
{
  plany_x=900;
  plany_y=900;
  w_judge=false;
  s_judge=false;
  a_judge=false;
  d_judge=false;
  z_judge=false;
  space_judge=false;
  speed=29;
  blood=10;
  skill=10;
  time1=startTimer(20);//启动计时器
  setFlag(QGraphicsItem::ItemIsFocusable);//注意获得焦点
  image=":/new/prefix1/Image/myplane.png";
  mark=0;
  life =true;
}
QRectF myplany::boundingRect() const
{
    qreal penwidth=1;
    return QRectF(0,0,1800,1000);
}
//每次重新绘制飞机
void myplany::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawPixmap(plany_x,plany_y,60,80,QPixmap(image));
    //子弹
    for(auto be=play_buttle.begin();be!=play_buttle.end();be++)
    {
        painter->drawPixmap(be->buttle_x,be->buttle_y,20,20,QPixmap(":/new/prefix1/Image/Bomb4.png"));
    }
    //分数
    painter->setPen(Qt::blue);
    QFont font;
    font.setPixelSize(50);
    painter->setFont(font);
    painter->drawText(1530,800,QStringLiteral("分数: "));
    painter->drawText(1650,800,QString::number(mark));
    update();
    //蓝条
    painter->setPen(Qt::white);
    painter->drawRect(1500,900,10*20,20);
    painter->setBrush(QBrush(Qt::blue));
    if(skill>0)
    painter->drawRect(1500,950,this->skill*20,20);
    //血条
    painter->setBrush(Qt::white);
    painter->setPen(Qt::white);
    painter->drawRect(1500,900,10*20,20);
    painter->setBrush(QBrush(Qt::red));
    painter->drawRect(1500,900,this->blood*20,20);
    update();
}
//控制飞机方向
void myplany::keyPressEvent(QKeyEvent *event)
{
  switch(event->key())
  {case Qt::Key_W:
      w_judge=true;
 break;
  case Qt::Key_S:
      s_judge=true;
     break;
  case Qt::Key_A:
       a_judge=true;
      break;
  case Qt::Key_D:
        d_judge=true;
      break;
  case Qt::Key_Space:
      space_judge=true;
      break;
  case Qt::Key_Z:
      z_judge=true;
      break;
  }
}
void myplany::keyReleaseEvent(QKeyEvent *event)
{
    switch(event->key())
    {case Qt::Key_W:
        w_judge=false;
   break;
    case Qt::Key_S:
        s_judge=false;
         break;
    case Qt::Key_A:
         a_judge=false;
        break;
    case Qt::Key_D:
          d_judge=false;
        break;
    case Qt::Key_Space:
        space_judge=false;
        break;
    }
}
//定时器事件
void myplany::timerEvent(QTimerEvent *event)
{

//玩家子弹的移动
for(auto be=play_buttle.begin();be!=play_buttle.end();be++)
{
    be->buttle_y-=buttle_speed;//移动
    if(be->buttle_y<0)
    {
        play_buttle.erase(be);
        break;
    }
}
//判断是否发射子弹
if(space_judge)
{
  PlayButtle playbuttle(plany_x+25,plany_y);
  play_buttle.push_back(playbuttle);
}
mark+=10;//存活0.1秒加10分
if(w_judge)
{
    plany_y-=speed;
    if(plany_y<0)
        plany_y=0;
}
if(s_judge)
{
    plany_y+=speed;
    if(plany_y>PLANYBOUNDARY_Y)
    {
        plany_y=PLANYBOUNDARY_Y;
    }
}
if(a_judge)
{
      plany_x-=speed;
      if(plany_x<0)
          plany_x=0;
}
if(d_judge)
{
      plany_x+=speed;
      if(plany_x>PLANYBOUNDARY_X)
          plany_x=PLANYBOUNDARY_X;
}
if(z_judge)
{
    skill-=2;
    emit skill_z();
    z_judge=false;
}
}

玩家子弹

#ifndef PLAYBUTTLE_H
#define PLAYBUTTLE_H
#include<QGraphicsRectItem>
#include<QPainter>
class PlayButtle:public QGraphicsRectItem
{
public:
    PlayButtle(int a,int b);
    PlayButtle(const PlayButtle&a);
    ~PlayButtle();
    PlayButtle& operator=(PlayButtle &a);
    int buttle_x;
    int buttle_y;
    QPainterPath shape()const;
    static const int speed =10;
};

#endif // PLAYBUTTLE_H
#include "playbuttle.h"
#include<QDebug>
PlayButtle::PlayButtle(int a,int b)
{
buttle_x=a;
buttle_y=b;
}

PlayButtle::PlayButtle(const PlayButtle &a)
{
    buttle_x=a.buttle_x;
    buttle_y=a.buttle_y;
}

PlayButtle::~PlayButtle()
{

}

PlayButtle &PlayButtle::operator=(PlayButtle &a)
{
    buttle_x=a.buttle_x;
    buttle_y=a.buttle_y;
    return *this;
}
QPainterPath PlayButtle::shape() const
{
    QPainterPath p;
    p.addRect(buttle_x,buttle_y,20,20);
    return p;
}


敌机

#ifndef ENEUMPLANY_H
#define ENEUMPLANY_H
#include<QGraphicsObject>
#include<QtGlobal>
#include"playbuttle.h"
#include<QList>
#include<QtGlobal>
#include<QString>
#include<QGraphicsObject>
class eneumplany:public QGraphicsObject
{
public:
    eneumplany(int x=0,int y=0,int c=0);
    eneumplany(const eneumplany&);//拷贝构造函数
    ~eneumplany();
   void operator=(const eneumplany &);//=重载函数
public:
    QRectF boundingRect()const;
    void paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr);
    void timerEvent(QTimerEvent *event);
     QPainterPath shape() const;
     int plany_x;
     int plany_y;
     QString image;
    int blood;//血量
     bool life;//飞机的状态
     QList<PlayButtle> eneum_buttle;//子弹析构
      int speed;
private:
   int x_speed;
    int val_random;//使每一架飞机的随机数种子不一样
   int explode_judge;//用来使爆炸照片停留的时间
};

#endif // ENEUMPLANY_H
#include "eneumplany.h"
#include<QPainter>
#include<QDebug>
eneumplany::eneumplany(int x,int y,int c)
{
 plany_x=x;
 plany_y=y;
 val_random=c;
 startTimer(300);
 srand(val_random);
 this->speed=rand()%10+30;
 blood=5;
 life=true;
 explode_judge=0;
 image=":/new/prefix1/Image/enemyplane.png";
}
//拷贝构造函数
eneumplany::eneumplany(const eneumplany &a)
{
    this->plany_x=a.plany_x;
    this->plany_y=a.plany_y;
}

eneumplany::~eneumplany()
{

}
//重载=运算符
void eneumplany::operator=(const eneumplany &a)
{
    this->plany_x=a.plany_x;
    this->plany_y=a.plany_y;
}
//飞机的飞行范围
QRectF eneumplany::boundingRect() const
{
    return QRectF(0,0,1800,1000);
}
//画敌机和子弹
void eneumplany::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    //飞机
    if(life)
    {painter->drawPixmap(plany_x,plany_y,60,80,QPixmap(image));
    }
    //敌机死后爆炸照片
    if(!life&&explode_judge!=50)
    {
        painter->drawPixmap(plany_x,plany_y,60,80,QPixmap(image));
        explode_judge++;
    }
    //子弹
    if(life)
    for(auto be=eneum_buttle.begin();be!=eneum_buttle.end();be++)
    {
        painter->drawPixmap(be->buttle_x,be->buttle_y,20,30,QPixmap(":/new/prefix1/Image/mybullet.png"));
         be->buttle_y+=be->speed;//子弹往下移动
    }
    update();
   }
//发射子弹
void eneumplany::timerEvent(QTimerEvent *event)
{
   //飞机本身
   if(life)
   {srand(val_random);
   x_speed=rand()%60-40;
   plany_x+=x_speed;
   plany_y+=speed;}
   if(plany_x<=0)
   {
       plany_x=0;
   }
   if(plany_x>=1740)
   {
       plany_x=1740;
   }
   //如果超出边界,则认为飞机以死
   if(plany_y>=1000)
   {
       life=false;
       blood=0;
   }
   // 子弹的移动
      PlayButtle buttle(plany_x+20,plany_y+80);
      for(auto be=eneum_buttle.begin();be!=eneum_buttle.end();be++)
      {
           be->buttle_y+=be->speed;
      }

      eneum_buttle.push_back(buttle);
      if(eneum_buttle.front().buttle_y>1800)//判断子弹是否超出边界
      {
          eneum_buttle.pop_front();
      }
}
QPainterPath eneumplany::shape() const
{
    QPainterPath p;
    p.addRect(plany_x,plany_y,60,80);
    return p;
}

死亡画面

#ifndef GAMEOVER_H
#define GAMEOVER_H
#include<QGraphicsObject>
#include<QPainter>
#include<QPushButton>
#include<QTimerEvent>
class gameover:public QGraphicsObject
{
    Q_OBJECT
public:
    gameover();
    QRectF boundingRect()const;
    void paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr);
    void timerEvent(QTimerEvent *event);
private:

signals:
    void show_mainwindow();

};

#endif // GAMEOVER_H
#include "gameover.h"

gameover::gameover()
{
    startTimer(5000);
}

QRectF gameover::boundingRect() const
{
    return QRectF(0,0,1800,1000);
}

void gameover::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->drawPixmap(0,0,1800,1000,QPixmap(":/new/prefix1/Image/425d4c0c0f16c401db1f1[1].jpg"));

}

void gameover::timerEvent(QTimerEvent *event)
{

    emit show_mainwindow();
}

boss飞机

#ifndef BOSSPLANY_H
#define BOSSPLANY_H
#include"eneumplany.h"
#include"myplany.h"
class bossplany:public eneumplany
{
    Q_OBJECT
public:
    bossplany(myplany &c);
    QRectF boundingRect()const;
    void paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr);
     void timerEvent(QTimerEvent *event);
     bool life;
     QList<PlayButtle> boss_buttle_1;
     QList<PlayButtle> boss_buttle_2;
     QList<PlayButtle> boss_buttle_3;
private:
     int buttly_y_1;//第一种子弹的y
     myplany &my_plany;
     int time1;
     int time2;
     int time3;
     void boss_myplany(QList<PlayButtle> &boss_buttle);
     void mybuttle_boss();
     void buttle_shape(QList<PlayButtle> &boss_buttle);
     int x_speed;
     int speed;
public slots:
     void buttle_skill();

};

#endif // BOSSPLANY_H
#include "bossplany.h"

bossplany::bossplany(myplany &c):my_plany(c)
{
    image=":/new/prefix1/Image/boss9.png";
    blood=250;
    plany_x=500;
    plany_y=0;
    life=false;
    time1=startTimer(200);
    time2=startTimer(1000);
    time3=startTimer(5000);
    x_speed=100;
    speed=50;
}
QRectF bossplany::boundingRect() const
{
    return QRectF(0,0,1800,1000);
}
void bossplany::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(blood==0)//如果boss死亡
    {
        life=false;
        image=":/new/prefix1/Image/bigairplane3.png";
    }
     if(life)
     {painter->drawPixmap(plany_x,plany_y,400,400,QPixmap(image));
     for(auto b:boss_buttle_1)
     {
         painter->drawPixmap(b.buttle_x-60,b.buttle_y+100,40,40,QPixmap(":/new/prefix1/Image/Bomb4.png"));
     }
     for(auto b:boss_buttle_2)
     {
         painter->drawPixmap(b.buttle_x+50,b.buttle_y+100,40,40,QPixmap(":/new/prefix1/Image/Bomb4.png"));
     }
     for(auto b:boss_buttle_3)
     {
         painter->drawPixmap(b.buttle_x,b.buttle_y+100,40,80,QPixmap(":/new/prefix1/Image/bossbullet (2).png"));
     }
     //画boss的血条
     QFont font;
     font.setPixelSize(25);
     painter->setFont(font);
     painter->setPen(Qt::blue);
     painter->drawText(0,40,QStringLiteral("Great old one"));
     painter->drawRect(170,20,700,20);
     painter->setBrush(Qt::red);
     painter->drawRect(170,20,7*blood/5,20);
     update();}
}

void bossplany::timerEvent(QTimerEvent *event)
{
    if(time1==event->timerId()&&life)
    { //boss的变化
    plany_x+=speed;
    if(plany_x<=0)
    {
        plany_x=0;
        speed=-speed;
    }
    if(plany_x>=1400)
    {
        plany_x=1400;
        speed=-speed;
    }
//子弹的生成
    PlayButtle buttle(plany_x+200,plany_y);
    boss_buttle_1.push_back(buttle);
    boss_buttle_2.push_back(buttle);
    for(auto be=boss_buttle_1.begin();be!=boss_buttle_1.end();be++)
    {
        be->buttle_y+=70;
        srand(my_plany.plany_x);
        be->buttle_x+=x_speed;
        if(be->buttle_y>=1000||be->buttle_x<=0||be->buttle_x>=1800)
        {
            boss_buttle_1.erase(be);
            break;
        }
    }
    for(auto be=boss_buttle_2.begin();be!=boss_buttle_2.end();be++)
    {
        be->buttle_y+=70;
        srand(my_plany.plany_x);
        be->buttle_x-=x_speed;
        if(be->buttle_y>=1000||be->buttle_x<=0||be->buttle_x>=1800)
        {
            boss_buttle_2.erase(be);
            break;
        }
        x_speed+=20;
        if(x_speed>=150)
        {
            x_speed=0;
        }
    }
    boss_myplany(boss_buttle_1);
    boss_myplany(boss_buttle_2);
    boss_myplany(boss_buttle_3);
    buttle_shape(boss_buttle_1);
    buttle_shape(boss_buttle_2);
    buttle_shape(boss_buttle_3);
    mybuttle_boss();
    connect(&my_plany,&myplany::skill_z,this,&bossplany::buttle_skill);
    }
    //生成第二种子弹
    for(auto be=boss_buttle_3.begin();be!=boss_buttle_3.end();be++)
    {
        be->buttle_y+=80;
        srand(my_plany.plany_x);
        if(be->buttle_y>=1000||be->buttle_x<=0||be->buttle_x>=1800)
        {
            boss_buttle_3.erase(be);
            break;
        }
    }
    if(time2==event->timerId()&&life)
  { PlayButtle buttle(plany_x+200,plany_y);
    boss_buttle_3.push_back(buttle);
    }
    if(time3==event->timerId())
    {
        if(my_plany.skill<10)
        my_plany.skill++;
    }
}
//boss飞机击中玩家
void bossplany::boss_myplany(QList<PlayButtle> &boss_buttle)
{
   for(auto be=boss_buttle.begin();be!=boss_buttle.end();be++)
   {
       if(be->buttle_x>=my_plany.plany_x&&be->buttle_x<=my_plany.plany_x+60&&
               be->buttle_y>=my_plany.plany_y&&be->buttle_y<=my_plany.plany_y+80)
       {
          my_plany.blood--;
         boss_buttle.erase(be);
          break;
       }
   }
}
//玩家子弹击中飞机
void bossplany::mybuttle_boss()
{
    for(auto be=my_plany.play_buttle.begin();be!=my_plany.play_buttle.end();be++)
    {
        if(be->buttle_x>plany_x&&be->buttle_x<plany_x+400
                &&be->buttle_y<=400)
        {
            this->blood--;
            my_plany.play_buttle.erase(be);
            break;
        }
    }
}
//子弹碰撞
void bossplany::buttle_shape(QList<PlayButtle> &boss_buttle)
{
    for(auto be=boss_buttle.begin();be!=boss_buttle.end();be++)
    {
        for(auto be1=my_plany.play_buttle.begin();be1!=my_plany.play_buttle.end();be1++)
        {
            if(be->buttle_x>=be1->buttle_x&&be->buttle_x<=be1->buttle_x+40&&
                    be->buttle_y>=be1->buttle_y+20)
            {
                my_plany.play_buttle.erase(be1);
                boss_buttle.erase(be);
                break;
            }
        }
        break;
    }
}
void bossplany::buttle_skill()
{
    boss_buttle_1.clear();
    boss_buttle_2.clear();
    boss_buttle_3.clear();
}

注意最好是参考一下,然后自己写,里面有很多东西可以去做。


版权声明:本文为qq_43702629原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。