QT(飞机大战)课设

pro初始化

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses 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

SOURCES += \
    Ends.cpp \
    Enemyplane.cpp \
    Pause.cpp \
    Start.cpp \
    bomb_1.cpp \
    bomb_2.cpp \
    bomb_3.cpp \
    bomb_4.cpp \
    bullet.cpp \
    main.cpp \
    mainscene.cpp \
    map.cpp \
    menu.cpp \
    my_plane.cpp \
    rank.cpp \
    stop.cpp

HEADERS += \
    Ends.h \
    Enemyplane.h \
    Pause.h \
    Start.h \
    bomb_1.h \
    bomb_2.h \
    bomb_3.h \
    bomb_4.h \
    bullet.h \
    game.h \
    mainscene.h \
    map.h \
    menu.h \
    my_plane.h \
    rank.h \
    stop.h

FORMS += \
    Start.ui \
    ends.ui \
    mainscene.ui \
    menu.ui \
    rank.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 += \
    resure.qrc

QT        += core gui multimedia

QT       += core gui sql

头文件依次
爆炸效果1头文件

#ifndef BOMB_1_H
#define BOMB_1_H

#include "game.h"
#include <QPixmap>
#include <QVector>

class Bomb_1
{
public:
    Bomb_1();
    void updateIfo();
public:
    QVector<QPixmap> m_pixAir;

    int m_x;
    int m_y;

    bool m_free;

    int m_Recoreder;
    int m_index;
};

#endif // BOMB_1_H

爆炸效果2头文件

#ifndef BOMB_2_H
#define BOMB_2_H

#include "game.h"
#include <QPixmap>
#include <QVector>

class Bomb_2
{
public:
    Bomb_2();
    void updateIfo();
public:
    QVector<QPixmap> m_pixAir;

    int m_x;
    int m_y;

    bool m_free;

    int m_Recoreder;
    int m_index;
};

#endif // BOMB_2_H

爆炸效果3头文件

#ifndef BOMB_3_H
#define BOMB_3_H


#include "game.h"
#include <QPixmap>
#include <QVector>

class Bomb_3
{
public:
    Bomb_3();
    void updateIfo();
public:
    QVector<QPixmap> m_pixAir;

    int m_x;
    int m_y;

    bool m_free;

    int m_Recoreder;
    int m_index;
};

#endif // BOMB_3_H

飞机爆炸效果头文件

#ifndef BOMB_4_H
#define BOMB_4_H

#include "game.h"
#include <QPixmap>
#include <QVector>

class Bomb_4
{
public:
    Bomb_4();
    void updateIfo();
public:
    QVector<QPixmap> m_pixAir;

    int m_x;
    int m_y;

    bool m_free;

    int m_Recoreder;
    int m_index;
};

#endif // BOMB_4_H

子弹头文件`

#ifndef BULLET_H
#define BULLET_H
#include "game.h"

#include <QPixmap>
#include <QRect>

class bullet
{
public:
    bullet();
    void updatePosition();
public:
    QPixmap m_bullet;
    int m_x;
    int m_y;
    int m_speed;
    bool m_free;
    QRect m_rect;
   // bool flag;
};

#endif // BULLET_H

结束界面头文件

#ifndef ENDS_H
#define ENDS_H

#include <QWidget>
#include "mainscene.h"

namespace Ui {
class Ends;
}

class Ends : public QWidget
{
    Q_OBJECT

public:
    explicit Ends(QWidget *parent = nullptr);
    ~Ends();
    Ui::Ends *ui;
    int score;
    void get_score(int x);

private slots:
    void on_pushButton_clicked();



    void on_groupBox_clicked();


private:

    //Mainscene a = new Mainscene();
};

#endif // ENDS_H

敌机头文件

#ifndef ENEMYPLANE_H
#define ENEMYPLANE_H

#include <QPixmap>

class Enemyplane
{
public:
    Enemyplane();
    void updatePoition();

public:
    QPixmap m_enemy;
    QPixmap m_enemy2;
    QPixmap m_enemy3;
    QPixmap m_enemy4;
    QPixmap m_enemy5;
    int m_x;
    int m_y;

    QRect m_Rect;

    bool m_free;

    int m_speed;
    int type;
};

#endif // ENEMYPLANE_H

定义变量头文件

#ifndef GAME_H
#define GAME_H

/* 界面初始值*/
#define Game_width 480
#define Game_height 700
#define Game_title "大战✈"
#define Game_res_path "./plane.rcc"  //转换二进制文件


/*刷新时间*/
#define Game_rate 10

/*地图路径*/
#define Map_path ":/resource/background.png"
#define Map_scroll_speed 2    //地图滚动速度

/*飞机参数*/
#define Plane_path ":/resource/me1.png"
#define Plane_speed 8

/*子弹参数*/
#define Bullet_path ":/resource/bullet1.png"
#define Bullet_speed 2

#define Bullet_num 30
#define Bullet_interval 20

/*敌人参数*/
#define Enemy_path ":/resource/enemy2.png"
#define Enemy2_path ":/resource/enemy1.png"
#define Enemy3_path ":/resource/enemy3_n2.png"
#define Enemy4_path ":/resource/bullet_supply.png"
#define Enemy5_path ":/resource/bomb_supply.png"
#define Enemy_speed 4
#define Enemy_num 30
#define Enemy_interval 60   //敌人出现间隔

/*爆炸参数*/
#define Bomb1_path ":/resource/enemy1_down%1.png"
#define Bomb1_num 20
#define Bomb1_max 4  //爆炸时有的图像数量
#define Bomb1_interval 20

#define Bomb2_path ":/resource/enemy2_down%1.png"
#define Bomb2_num 20
#define Bomb2_max 4
#define Bomb2_interval 20

#define Bomb3_path ":/resource/enemy3_down%1.png"
#define Bomb3_num 20
#define Bomb3_max 6
#define Bomb3_interval 20

#define Bomb4_path ":/resource/enemy3_down%1.png"
#define Bomb4_num 20
#define Bomb4_max 4
#define Bomb4_interval 20

/*暂停参数*/
#define Stop_path ":/resource/resume_nor.png"

#define Sound_path ":/bgm1.wav"

#define Pause1_path ":/resource/pause_nor.png"
#define Pause2_path ":/resource/pause_pressed.png"
#define Pause3_path ":/resource/resume_nor.png"
#define Pause4_path ":/resource/resume_pressed.png"
#define Pause_max 4
#define Pause_interval 20
#define Game_ends ":/resource/gameover.png"

#endif // GAME_H


/*按键操作:
P:单倍子弹
L:双倍子弹
O:炸弹
I:暂停
K:游戏直接结束
Space:左上角的游戏暂停
*/

主场景头文件

#ifndef MAINSCENE_H
#define MAINSCENE_H

#include <QWidget>
#include <QTimer>
#include "map.h"
#include "my_plane.h"
#include "bullet.h"
#include "Ends.h"
#include "Enemyplane.h"
#include "bomb_1.h"
#include "bomb_2.h"
#include "bomb_3.h"
#include "bomb_4.h"
#include "stop.h"
#include "Pause.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Mainscene; }
QT_END_NAMESPACE

class Mainscene : public QWidget
{
    Q_OBJECT

public:
    Mainscene(QWidget *parent = nullptr);
    ~Mainscene();

    void init();

    void playGame();

    void updatePosition();
    //绘制到屏幕
    void paintEvent(QPaintEvent *);

    void keyPressEvent(QKeyEvent *);
    void keyReleaseEvent(QKeyEvent *);

    void mouseMoveEvent(QMouseEvent *);

    void Enemyshouw();

    void conflict1();
    void conflict2();

    int cnt = 0;
    void updateback();
    int get_score();

    void mouseReleaseEvent(QMouseEvent *);

    void get_info(QString str);


    Enemyplane m_enemy[Enemy_num];

    int enemy_recoder;
    int score;

    map m_map;

   // Ends *ss;

    My_plane hero_plane;
    QTimer m_Timer;

    Bomb_1 m_bomb1[Bomb1_num];
    Bomb_2 m_bomb2[Bomb2_num];
    Bomb_3 m_bomb3[Bomb3_num];
    Bomb_4 m_bomb4[Bomb4_num];

    bool W_free;
    bool S_free;
    bool A_free;
    bool D_free;

    Pause pause_state;
    int game_state;

    bool op;

    stop STOP;

    QString username;
    //bullet temp_bullet;
private:
    Ui::Mainscene *ui;
};
#endif // MAINSCENE_H

地图头文件

#ifndef MAP_H
#define MAP_H
#include <QPixmap>

class map
{
public:
    map();
    void mapPosition();

public:
    QPixmap m_map1;
    QPixmap m_map2;

    int m_map1_posY;
    int m_map2_posY;

    int m_scroll_speed;
};

#endif // MAP_H

菜单头文件

#ifndef MENU_H
#define MENU_H

#include "mainscene.h"
#include <QDialog>

namespace Ui {
class menu;
}

class menu : public QDialog
{
    Q_OBJECT

public:
    explicit menu(QWidget *parent = nullptr);
    ~menu();

private slots:
    //void on_buttonBox_clicked(QAbstractButton *button);

    void on_buttonBox_accepted();

    void on_pushButton_clicked();

    void on_pushButton_clicked(bool checked);

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void save();

    bool check();

private:
    Ui::menu *ui;
};

#endif // MENU_H

我方飞机头文件

#ifndef MY_PLANE_H
#define MY_PLANE_H

#include "game.h"
#include "bullet.h"


#include <QPixmap>
#include <QRect>


class My_plane
{
public:
    My_plane();
    void shoot_bullet();
    void shoot_bomb();
    void setPosition(int x, int y);

public:
    int heights;
    int widths;
    QPixmap m_plane;
    int m_x;
    int m_y;
    int Num_bomb;
    int blood;
    int Num_bullet;
    QRect m_Rect;
    bool flag;
    bool flag2;
    bool check_bomb;
    bool m_free;
    int cnt = 0;
 // int score;
    QString username;
    bullet m_bullets[Bullet_num + 10];

    int m_recorder;
};

#endif // MY_PLANE_H

暂停按钮头文件

#ifndef PAUSE_H
#define PAUSE_H

#include <game.h>
#include <QPixmap>

class Pause
{
public:
    Pause();

public:
    bool pause_pressed;
    QPixmap pause_background;
    QPixmap pause_pic1;
    QPixmap pause_pic2;
    QPixmap start_pic1;
    QPixmap start_pic2;
    QPixmap game_ends;
    int pause_pic_posX;
    int pause_pic_posY;
    bool game_paused;
};

#endif // PAUSE_H

等级排行头文件

#ifndef RANK_H
#define RANK_H

#include <QWidget>

namespace Ui {
class rank;
}

class rank : public QWidget
{
    Q_OBJECT

public:
    explicit rank(QWidget *parent = nullptr);
    ~rank();

private slots:
    void on_pushButton_clicked();

private:
    Ui::rank *ui;
};

#endif // RANK_H

开始界面头文件

#ifndef START_H
#define START_H

#include <QWidget>

namespace Ui {
class Start;
}

class Start : public QWidget
{
    Q_OBJECT

public:
    explicit Start(QWidget *parent = nullptr);
    ~Start();
    void get_str(QString username);
    QString str;

private slots:
    void on_pushButton_clicked();

private:
    Ui::Start *ui;
};

#endif // START_H

另外一个暂停界面头文件

#ifndef STOP_H
#define STOP_H

#include "game.h"
#include <QPixmap>

class stop
{
public:
    stop();
public:
    QPixmap Stop;

    int m_x;
    int m_y;

    bool m_free;
};

#endif // STOP_H

CPP文件依次:
爆炸效果1函数

#include "bomb_1.h"
#include "game.h"

#include <QDebug>

Bomb_1::Bomb_1()
{

    m_pixAir.push_back(QPixmap(":/resource/enemy1_down1.png"));
    m_pixAir.push_back(QPixmap(":/resource/enemy1_down2.png"));
    m_pixAir.push_back(QPixmap(":/resource/enemy1_down3.png"));
    m_pixAir.push_back(QPixmap(":/resource/enemy1_down4.png"));

    m_x = 0;
    m_y = 0;

    m_free = true;
    
    /*当前是否需要爆炸,当前到了第几个图片,当前是否需要换图片*/
    m_index = 0;

    m_Recoreder = 0;
}

void Bomb_1::updateIfo()
{
    /*判断是否应该有爆炸效果*/
    if (m_free)    return;

    m_Recoreder ++;
    /*如果间隔不够改变图片就结束*/
    if (m_Recoreder < Bomb1_interval)
    {
        return;
    }

    /*实时修改信息*/
    m_Recoreder = 0;
    m_index ++;

    /*如果最后一张图片也更新完,修改信息*/
    if (m_index >= Bomb1_max)
    {
        m_index = 0;
        m_free = true;
    }


}

爆炸效果2函数:

#include "bomb_2.h"

Bomb_2::Bomb_2()
{
    m_pixAir.push_back(QPixmap(":/resource/enemy2_down1.png"));
    m_pixAir.push_back(QPixmap(":/resource/enemy2_down2.png"));
    m_pixAir.push_back(QPixmap(":/resource/enemy2_down3.png"));
    m_pixAir.push_back(QPixmap(":/resource/enemy2_down4.png"));

    m_x = 0;
    m_y = 0;

    /*当前是否在爆炸,当前枚举到了第几张图片,当前是否需要换图片*/
    m_free = true;

    m_index = 0;

    m_Recoreder = 0;
}

void Bomb_2::updateIfo()
{
    if (m_free)    return;

    m_Recoreder ++;
    if (m_Recoreder < Bomb2_interval)
    {
        return;
    }

    m_Recoreder = 0;

    m_index ++;
    if (m_index >= Bomb2_max)
    {
        m_index = 0;
        m_free = true;
    }
}

爆炸效果3函数

#include "bomb_3.h"

Bomb_3::Bomb_3()
{
    m_pixAir.push_back(QPixmap(":/resource/enemy3_down1.png"));
    m_pixAir.push_back(QPixmap(":/resource/enemy3_down2.png"));
    m_pixAir.push_back(QPixmap(":/resource/enemy3_down3.png"));
    m_pixAir.push_back(QPixmap(":/resource/enemy3_down4.png"));
    m_pixAir.push_back(QPixmap(":/resource/enemy3_down4.png"));
    m_pixAir.push_back(QPixmap(":/resource/enemy3_down4.png"));

    m_x = 0;
    m_y = 0;
    
    /*当前是否在爆炸,当前枚举到了第几张图片,当前是否需要换图片*/
    m_free = true;

    m_index = 0;

    m_Recoreder = 0;
}

void Bomb_3::updateIfo()
{
    if (m_free)    return;

    m_Recoreder ++;
    if (m_Recoreder < Bomb3_interval)
    {
        return;
    }

    m_Recoreder = 0;

    m_index ++;
    if (m_index >= Bomb3_max)
    {
        m_index = 0;
        m_free = true;
    }
}

自身爆炸效果函数

#include "bomb_4.h"

Bomb_4::Bomb_4()
{
    m_pixAir.push_back(QPixmap(":/resource/me_destroy_1.png"));
    m_pixAir.push_back(QPixmap(":/resource/me_destroy_2.png"));
    m_pixAir.push_back(QPixmap(":/resource/me_destroy_3.png"));
    m_pixAir.push_back(QPixmap(":/resource/me_destroy_4.png"));

    m_x = 0;
    m_y = 0;
    
    /*当前是否在爆炸,当前枚举到了第几张图片,当前是否需要换图片*/
    m_free = true;

    m_index = 0;

    m_Recoreder = 0;
}

void Bomb_4::updateIfo()
{
    if (m_free)    return;

    m_Recoreder ++;
    if (m_Recoreder < Bomb4_interval)
    {
        return;
    }

    m_Recoreder = 0;

    m_index ++;
    if (m_index >= Bomb4_max)
    {
        m_index = 0;
        m_free = true;
    }
}

子弹函数:

#include "bullet.h"
#include "game.h"

#include <QRect>

bullet::bullet()
{
    m_bullet.load(Bullet_path);

    m_x = (Game_width - m_bullet.width()) * 0.5;
    m_y = Game_height;

    m_free = true;

    m_speed = Bullet_speed;

    m_rect.setWidth(m_bullet.width());
    m_rect.setHeight(m_bullet.height());
    m_rect.moveTo(m_x, m_y);

   // flag = true;
}

void bullet::updatePosition()
{
    if (m_free)   return;
    
    /*更新子弹的位置,如果射出去的话更新位置*/
    m_y -= m_speed;
    m_rect.moveTo(m_x, m_y);

    if (m_y <= m_rect.height())
    {
        m_free = true;
    }
}

结束界面函数:

#include "Ends.h"
#include "ui_ends.h"
#include "mainscene.h"
#include <QLabel>

Ends::Ends(QWidget *parent) :
    QWidget(parent),
   ui(new Ui::Ends)
{
  ui->setupUi(this);
    //Mainscene a;
    /*设置UI界面的标签*/
    QString str = "Your score is " + QString::number(score);
    ui->label_2 -> setText(str);
}

Ends::~Ends()
{
    delete ui;
}

void Ends::on_pushButton_clicked()
{
    Mainscene *w = new Mainscene();
    w -> setWindowTitle(Game_title);
    w -> setWindowIcon(QIcon(":/resource/me1.png"));
    this -> close();
    w -> show();
}




void Ends::on_groupBox_clicked()
{
    Mainscene *w = new Mainscene();
    w -> setWindowTitle(Game_title);
    w -> setWindowIcon(QIcon(":/resource/me1.png"));
    this -> close();
    w -> show();
}

void Ends::get_score(int x)
{
    /*得到参数*/
    score = x;
    QString str = "Your score is " + QString::number(score);
    ui->label_2 -> setText(str);
}

敌机函数:

#include "Enemyplane.h"
#include "game.h"

#include <QPixmap>
#include <QRect>

Enemyplane::Enemyplane()
{
     m_enemy.load(Enemy_path);
     m_enemy2.load(Enemy2_path);
     m_enemy3.load(Enemy3_path);
     m_enemy4.load(Enemy4_path);
     m_enemy5.load(Enemy5_path);

     m_x = 0;
     m_y = 0;

     m_free = true;

     m_speed = Enemy_speed;

     m_Rect.setWidth(m_enemy.width());
     m_Rect.setHeight(m_enemy.height());
     m_Rect.moveTo(m_x, m_y);
     type = 10;
}

void Enemyplane::updatePoition()
{
    if (m_free)    return;

    /*跟新敌机的位置,射出去的话改变状态*/
    m_y += Enemy_speed;
    m_Rect.moveTo(m_x, m_y);

    if (m_y >= Game_height + m_Rect.height())
    {
        m_y = 0;
        m_free = true;
    }
}

主场景函数:

#include "mainscene.h"
#include "ui_mainscene.h"
#include "game.h"
#include "Ends.h"
#include "my_plane.h"
#include "my_plane.h"
#include <QIcon>
#include <QPainter>
#include <QKeyEvent>
#include <QMouseEvent>
#include <ctime>
#include <QMessageBox>
#include <QLabel>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QCoreApplication>
#include <QSound>

Mainscene::Mainscene(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Mainscene)
{
    score = 0;
    op = false;
    init();
    playGame();
    /*标记移动方向的状态*/
    W_free = false;
    S_free = false;
    A_free = false;
    D_free = false;

    ui->setupUi(this);

    ;
    game_state = 1;
}

Mainscene::~Mainscene()
{
    delete ui;
}

void Mainscene::init()
{
    /*设置游戏页面长度和宽度*/
    setFixedSize(Game_width, Game_height);
    // setWindowIcon(QIcon(":/resource/again.png"));
    /*设置计时器间隔*/
    m_Timer.setInterval(Game_rate);

     enemy_recoder = 0;
     /*生成随机数种子*/
     srand((unsigned int)time(NULL));
 }

 void Mainscene::playGame()
 {
     QSound::play(Sound_path);


     m_Timer.start();
     /*计时器开始工作,每当感应到计时器工作的信号便发动槽函数*/
     connect(&m_Timer, &QTimer::timeout, [=]()
     {
         /*如果是非暂停状态才更新各种位置*/
         if (game_state == 1 && !pause_state.game_paused)
         {
             Enemyshouw();
             updatePosition();
             updateback();
             conflict1();
             if (op == false)   conflict2();
         }
        /*画出页面中的图形*/
         update();
     });
     if (op)
     {

     }
 }

void Mainscene::updatePosition()
{
    /*更新地图的位置*/
    m_map.mapPosition();

   // temp_bullet.m_free = false;
    //temp_bullet.updatePosition();
    /*飞机射击子弹*/
    hero_plane.shoot_bullet();

    /*根据状态移动飞机位置*/
    if (W_free)
    {
        hero_plane.setPosition(hero_plane.m_x, hero_plane.m_y - Plane_speed);
    }
    if (S_free)
    {
        hero_plane.setPosition(hero_plane.m_x, hero_plane.m_y + Plane_speed);
    }
    if (A_free)
    {
        hero_plane.setPosition(hero_plane.m_x - Plane_speed, hero_plane.m_y);
    }
    if (D_free)
    {
         hero_plane.setPosition(hero_plane.m_x + Plane_speed, hero_plane.m_y);
    }

    /*如果子弹状态为被射出,更改子弹状态*/
    for (int i = 0; i < Bullet_num; i ++)
    {
        if (hero_plane.m_bullets[i].m_free == false)
        {
            hero_plane.m_bullets[i].updatePosition();
        }
    }

    /*如果敌军状态为发动,更改敌军位置*/
    for (int i = 0; i < Enemy_num; i ++)
    {
        if (m_enemy[i].m_free == false)
        {
            m_enemy[i].updatePoition();
        }
    }

    /*如果某个爆炸发生了,实时改变爆炸效果的图片*/
    for (int i = 0; i < Bomb1_num; i ++)
    {
        if (m_bomb1[i].m_free == false)
        {
            m_bomb1[i].updateIfo();
        }

        if (m_bomb2[i].m_free == false)
        {
            m_bomb2[i].updateIfo();
        }

        if (m_bomb3[i].m_free == false)
        {
            m_bomb3[i].updateIfo();
        }

        if (m_bomb4[i].m_free == false)
        {
            m_bomb4[i].updateIfo();
        }
    }
}

void Mainscene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    if (pause_state.game_paused)
    {

    }

    /*如果当前暂停,只画暂停按钮*/
    if (STOP.m_free == false)
    {
         painter.drawPixmap(STOP.m_x,STOP.m_y, STOP.Stop);
        // painter.drawPixmap(Game_height * 0.8, Game_width * 0.3, Pause.game_ends);
         return;
    }

    /*画出两张地图的位置*/
    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.m_free)  painter.drawPixmap(hero_plane.m_x, hero_plane.m_y, hero_plane.m_plane);
   else{
        /*如果飞机被炸毁,计时器间隔80次再重新画出飞机*/
       hero_plane.cnt ++;
       if (hero_plane.cnt >= 80)
       {
           hero_plane.cnt = 0;
           hero_plane.setPosition((Game_width - hero_plane.m_Rect.width()) * 0.5, Game_height-hero_plane.m_Rect.height());
         // hero_plane.blood --;
           painter.drawPixmap(hero_plane.m_x, hero_plane.m_y, hero_plane.m_plane);
           hero_plane.m_free = true;
       }
   }

    // painter.drawPixmap(0, m_map.m_map1_posY, m_map.m_map1);
     //painter.drawPixmap(0, m_map.m_map2_posY, m_map.m_map2);

   /*画出当前飞行的子弹*/
    for (int i = 0; i < Bullet_num - 1; i ++)
    {
        if (hero_plane.m_bullets[i].m_free == false)
        {
            painter.drawPixmap(hero_plane.m_bullets[i].m_x, hero_plane.m_bullets[i].m_y, hero_plane.m_bullets[i].m_bullet);
        }
    }

    /*画出敌军,根据敌军的随机函数生成的随机数判断生成哪架飞机并且修改敌机的方框*/
    for (int i = 0; i < Enemy_num; i ++)
    {
        if (m_enemy[i].m_free == false)
        {
          if (m_enemy[i].type == 0 || m_enemy[i].type == 1 || m_enemy[i].type == 2) painter.drawPixmap(m_enemy[i].m_x, m_enemy[i].m_y, m_enemy[i].m_enemy);
          else if (m_enemy[i].type == 3 || m_enemy[i].type == 4)
          {
              m_enemy[i].m_Rect.setWidth(m_enemy[i].m_enemy2.width());
              painter.drawPixmap(m_enemy[i].m_x, m_enemy[i].m_y, m_enemy[i].m_enemy2);
          }
          else if (m_enemy[i].type == 5)
          {
              m_enemy[i].m_Rect.setWidth(m_enemy[i].m_enemy3.width());
              painter.drawPixmap(m_enemy[i].m_x, m_enemy[i].m_y, m_enemy[i].m_enemy3);
          }
          else if (m_enemy[i].type == 6)
          {
              m_enemy[i].m_Rect.setWidth(m_enemy[i].m_enemy4.width());
              painter.drawPixmap(m_enemy[i].m_x, m_enemy[i].m_y, m_enemy[i].m_enemy4);
          }
          else if (m_enemy[i].type == 7)
          {
              m_enemy[i].m_Rect.setWidth(m_enemy[i].m_enemy5.width());
              painter.drawPixmap(m_enemy[i].m_x, m_enemy[i].m_y, m_enemy[i].m_enemy5);
          }
        }
    }

    /*画出爆炸效果所在的图*/
    for (int i = 0; i < Bomb1_num; i ++)
    {
        if (m_bomb1[i].m_free == false)
        {
            painter.drawPixmap(m_bomb1[i].m_x, m_bomb1[i].m_y, m_bomb1[i].m_pixAir[m_bomb1[i].m_index]);
        }

        if (m_bomb2[i].m_free == false)
        {
            painter.drawPixmap(m_bomb2[i].m_x, m_bomb2[i].m_y, m_bomb2[i].m_pixAir[m_bomb2[i].m_index]);
        }

        if (m_bomb3[i].m_free == false)
        {
            painter.drawPixmap(m_bomb3[i].m_x, m_bomb3[i].m_y, m_bomb3[i].m_pixAir[m_bomb3[i].m_index]);
        }

        if (m_bomb4[i].m_free == false)
        {
            painter.drawPixmap(m_bomb4[i].m_x, m_bomb4[i].m_y, m_bomb4[i].m_pixAir[m_bomb4[i].m_index]);
        }
    }


    //暂停功能绘制
        if(pause_state.game_paused)
        {
            /*暂停首先画出大背景*/
            painter.drawPixmap(0,0,pause_state.pause_background);
            /*画出游戏结束界面*/
             painter.drawPixmap(Game_width * 0.17, Game_height * 0.8, pause_state.game_ends);
             /*根据是否按压判断是哪张图*/
            if(pause_state.pause_pressed)
                painter.drawPixmap(pause_state.pause_pic_posX,pause_state.pause_pic_posY,pause_state.start_pic2);
            else
                painter.drawPixmap(pause_state.pause_pic_posX,pause_state.pause_pic_posY,pause_state.start_pic1);

        }
        else
        {
            /*根据是否按压判断是哪张图*/
            if(pause_state.pause_pressed)
                painter.drawPixmap(pause_state.pause_pic_posX,pause_state.pause_pic_posY,pause_state.pause_pic2);
            else
                painter.drawPixmap(pause_state.pause_pic_posX,pause_state.pause_pic_posY,pause_state.pause_pic1);
        }

   // painter.drawPixmap(temp_bullet.m_x, temp_bullet.m_y, temp_bullet.m_bullet);
}

void Mainscene::keyPressEvent(QKeyEvent *k)
{
    /*方向键只更新状态*/
    switch(k -> key())
    {
     case Qt::Key_W :
    {
        // hero_plane.setPosition(hero_plane.m_x, hero_plane.m_y - Plane_speed);
         W_free = true;
    };break;
    case Qt::Key_S:
    {
        //hero_plane.setPosition(hero_plane.m_x, hero_plane.m_y + Plane_speed);
        S_free = true;
    };  break;
    case Qt::Key_A :
    {
        //hero_plane.setPosition(hero_plane.m_x - Plane_speed, hero_plane.m_y);
        A_free = true;
    };break;
    case Qt::Key_D :
    {
        //hero_plane.setPosition(hero_plane.m_x + Plane_speed, hero_plane.m_y);
        D_free = true;
    };break;
    case Qt::Key_P :
    {
        /*单倍子弹,标记子弹单倍子弹*/
      if (hero_plane.Num_bullet >= 0)
      {
          hero_plane.flag = false;
          //hero_plane.Num_bullet --;
      }
    };break;
    case Qt::Key_L :
    {
       /*双倍子弹,标记双倍子弹*/
      if (hero_plane.Num_bullet >= 2)
      {
          hero_plane.flag2 = false;
          hero_plane.Num_bullet -= 2;
      }
    };break;
    case Qt::Key_Q :
    {
      hero_plane.Num_bomb--;
    };break;

    case Qt::Key_O :
    {
        /*如果炸弹还有剩余*/
        if (hero_plane.Num_bomb > 0)
        {
            hero_plane.check_bomb = false;
            hero_plane.Num_bomb --;
            for (int i = 0; i < Enemy_num; i ++)
            {
                /*让所有子弹的状态改变*/
               m_enemy[i].m_free = true;
               if (m_enemy[i].type == 3 || m_enemy[i].type == 4 )
               {
                   score += 1;
                   /*出中等飞机的爆炸效果*/
                   for (int k = 0; k < Bomb1_num; k ++)
                   {
                       if (m_bomb1[k].m_free)
                       {
                           m_bomb1[k].m_free = false;
                           m_bomb1[k].m_x = m_enemy[i].m_x;
                           m_bomb1[k].m_y = m_enemy[i].m_y;
                           break;
                       }
                   }
               }
               else if (m_enemy[i].type == 0 || m_enemy[i].type == 1 || m_enemy[i].type == 2)
               {
                     score += 2;
                     for (int k = 0; k < Bomb2_num; k ++)
                     {
                         if (m_bomb2[k].m_free)
                         {
                             m_bomb2[k].m_free = false;
                             m_bomb2[k].m_x = m_enemy[i].m_x;
                             m_bomb2[k].m_y = m_enemy[i].m_y;
                             break;
                         }
                     }
               }
               else if (m_enemy[i].type == 5)
               {
                   score += 3;
                   for (int k = 0; k < Bomb2_num; k ++)
                   {
                       if (m_bomb3[k].m_free)
                       {
                           m_bomb3[k].m_free = false;
                           m_bomb3[k].m_x = m_enemy[i].m_x;
                           m_bomb3[k].m_y = m_enemy[i].m_y;
                           break;
                       }
                   }
               }
            }
        }
        else
        {

        }
    };break;
    case Qt::Key_I :
    {
        /*判断是否暂停并且改变位置*/
        if (m_Timer.isActive())
        {
            m_Timer.stop();
            STOP.m_free = false;
            update();
        }
        else
        {
             m_Timer.start();
             STOP.m_free = true;
        }
    };break;
    case Qt::Key_K :
    {
        /*关闭窗口*/
        this->close();
    }break;
        /*只按下空格时只修改压下去的状态*/
    case Qt::Key_Space:
                    if(game_state == 1)
                        pause_state.pause_pressed = true;
            break;
       /*其余任意按键*/
    default:
    if (op){
        /*展示结束画面*/
        this -> close();
       //QString str = "Your score is " + QString::number(get_score());
        //x -> ui -> label_2 -> setText(str);
        Ends *x = new Ends();

        //x -> score = score;

        x -> show();
        /*传递参数*/
        x->get_score(score);

        /*更新当前用户的分数信息*/
       QString update_sql = QString("update forms set score = '%1' where username = '%2'").arg(score).arg(username);
        QSqlQuery sql_query;
       // sql_query.exec(update_sql);


        if (sql_query.exec(update_sql))
        {
            // QMessageBox :: information(this, username, QString::number(score));
        }
    }
    }
}


void Mainscene::keyReleaseEvent(QKeyEvent *event){
    /*按键松开修改状态*/
    switch(event->key()){
        case Qt::Key_W:
        {
             W_free = false;
        }break;
        case Qt::Key_S:
        {
         S_free = false;
        }break;
        case Qt::Key_A:
        {
        A_free = false;
    }break;
        case Qt::Key_D:
        {
        D_free = false;
    }break;
        /*松开空格时改变按压状态,并且修改游戏状态*/
    case Qt::Key_Space:
                    if(game_state == 1)
                    {
                        pause_state.pause_pressed = false;
                        pause_state.game_paused = !pause_state.game_paused;
                    }
                break;//松开
    }


}

void Mainscene::mouseMoveEvent(QMouseEvent *event)
{
    /*修改飞机位置*/
    hero_plane.setPosition(event -> x() - hero_plane.widths * 0.5, event -> y() - hero_plane.heights * 0.5);
    int x = event->x();
          int y = event->y();
          /*暂停位置状态修改*/
          if(x >= 0 && y >= 0 && x <=pause_state.pause_pic1.width() && y <= pause_state.pause_pic1.height())
          {
              if(game_state == 1)
              {
                  pause_state.pause_pressed = true;
              }
          }

          /*结束游戏状态修改*/
          if (x >= Game_width * 0.17 && y >= Game_height * 0.8 && x <= (Game_width * 0.17 + pause_state.game_ends.width()) && y <= (Game_height * 0.8 + pause_state.game_ends.height()))
              if (pause_state.game_paused)    this -> close();
}

void Mainscene::mouseReleaseEvent(QMouseEvent *event)
    {
        int x = event->x();
        int y = event->y();
        /*鼠标松开的状态修改*/
        if(x >= 0 && y >= 0 && x <= pause_state.pause_pic1.width() && y <= pause_state.pause_pic1.height())
        {
            if(game_state == 1)
            {
                pause_state.pause_pressed = false;
                pause_state.game_paused = !pause_state.game_paused;
            }
        }
        /*游戏结束*/
        if (x >= Game_width * 0.17 && y >= Game_height * 0.8 && x <= (Game_width * 0.17 + pause_state.game_ends.width()) && y <= (Game_height * 0.8 + pause_state.game_ends.height()))
            if (pause_state.game_paused)    this -> close();
}

void Mainscene::get_info(QString str)
{
    /*从开始界面得到用户的昵称*/
    username = str;
}

void Mainscene::Enemyshouw()
{
    enemy_recoder ++;
    /*根据间隔确定敌机是否出现*/
    if (enemy_recoder <= Enemy_interval)
    {
        return;
    }



    enemy_recoder = 0;

    /*找到任意一个当前空闲的敌机*/
    for (int i = 0; i < Enemy_num; i ++)
    {
        if (m_enemy[i].m_free)
        {
            m_enemy[i].m_free = false;

            /*横坐标与敌机的类型都是随机放置*/
            m_enemy[i].m_x = rand() % (Game_width - m_enemy[i].m_Rect.width());
            m_enemy[i].m_y = -m_enemy[i].m_Rect.height();
            m_enemy[i].type = rand() % 8;
            break;
        }
    }
}

void Mainscene::conflict1()
{
    for (int i = 0; i < Enemy_num; i ++)
    {
        if (m_enemy[i].m_free)      continue;

        for (int j = 0; j < Bullet_num; j ++)
        {
            if (hero_plane.m_bullets[j].m_free)
            {
                continue;
            }
            
            /*如果某个敌机与某个子弹发生碰撞*/
            if (m_enemy[i].m_Rect.intersects(hero_plane.m_bullets[j].m_rect))
            {
                m_enemy[i].m_free = true;
                if (m_enemy[i].type == 3 || m_enemy[i].type == 4 )
                {
                    score += 1;
                    /*展示对应的动画*/
                    for (int k = 0; k < Bomb1_num; k ++)
                    {
                        if (m_bomb1[k].m_free)
                        {
                            m_bomb1[k].m_free = false;
                            m_bomb1[k].m_x = m_enemy[i].m_x;
                            m_bomb1[k].m_y = m_enemy[i].m_y;
                            break;
                        }
                    }
                }
                else if (m_enemy[i].type == 0 || m_enemy[i].type == 1 || m_enemy[i].type == 2)
                {
                      score += 2;
                      for (int k = 0; k < Bomb2_num; k ++)
                      {
                          if (m_bomb2[k].m_free)
                          {
                              m_bomb2[k].m_free = false;
                              m_bomb2[k].m_x = m_enemy[i].m_x;
                              m_bomb2[k].m_y = m_enemy[i].m_y;
                              break;
                          }
                      }
                }
                else if (m_enemy[i].type == 5)
                {
                    score += 3;
                    for (int k = 0; k < Bomb3_num; k ++)
                    {
                        if (m_bomb3[k].m_free)
                        {
                            m_bomb3[k].m_free = false;
                            m_bomb3[k].m_x = m_enemy[i].m_x;
                            m_bomb3[k].m_y = m_enemy[i].m_y;
                            break;
                        }
                    }
                }
                else if (m_enemy[i].type == 6)
                {
                    hero_plane.Num_bullet += 20;
                }
                else if (m_enemy[i].type == 7)
                {
                    hero_plane.Num_bomb += 1;
                }
                hero_plane.m_bullets[j].m_free = true;
            }
        }
    }
}

void Mainscene::conflict2()
{
    for (int i = 0; i < Enemy_num; i ++)
    {
        if (m_enemy[i].m_free)      continue;

            /*如果某个敌机与飞机碰撞*/
            if (m_enemy[i].m_Rect.intersects(hero_plane.m_Rect))
            {

                m_enemy[i].m_free = true;
/*
                if (m_enemy[i].type == 6)
                {
                    hero_plane.Num_bullet += 20;
                    break;
                }
                else if (m_enemy[i].type == 7)
                {
                    hero_plane.Num_bomb += 1;
                    break;
                }
*/
                /*展示飞机的动画*/
                for (int k = 0; k < Bomb4_num; k ++)
                {
                    if (m_bomb4[k].m_free)
                    {
                        m_bomb4[k].m_free = false;
                        m_bomb4[k].m_x = hero_plane.m_x;
                        m_bomb4[k].m_y = hero_plane.m_y;
                        hero_plane.m_free = false;
                        break;
                    }
                }
                 
                /*飞机回到出生点*/
                 hero_plane.setPosition((Game_width - hero_plane.m_Rect.width()) * 0.5, Game_height-hero_plane.m_Rect.height());
                hero_plane.blood --;
                hero_plane.Num_bullet = 0;

/*
                for (int i = 0; i < Enemy_num; i ++)
                {
                   m_enemy[i].m_free = true;
                   if (m_enemy[i].type == 3 || m_enemy[i].type == 4 )
                   {
                      // score += 1;
                       for (int k = 0; k < Bomb1_num; k ++)
                       {
                           if (m_bomb1[k].m_free)
                           {
                               m_bomb1[k].m_free = false;
                               m_bomb1[k].m_x = m_enemy[i].m_x;
                               m_bomb1[k].m_y = m_enemy[i].m_y;
                               break;
                           }
                       }
                   }
                   else if (m_enemy[i].type == 0 || m_enemy[i].type == 1 || m_enemy[i].type == 2)
                   {
                         //score += 2;
                         for (int k = 0; k < Bomb2_num; k ++)
                         {
                             if (m_bomb2[k].m_free)
                             {
                                 m_bomb2[k].m_free = false;
                                 m_bomb2[k].m_x = m_enemy[i].m_x;
                                 m_bomb2[k].m_y = m_enemy[i].m_y;
                                 break;
                             }
                         }
                   }
                   else if (m_enemy[i].type == 5)
                   {
                      // score += 3;
                       for (int k = 0; k < Bomb2_num; k ++)
                       {
                           if (m_bomb3[k].m_free)
                           {
                               m_bomb3[k].m_free = false;
                               m_bomb3[k].m_x = m_enemy[i].m_x;
                               m_bomb3[k].m_y = m_enemy[i].m_y;
                               break;
                           }
                       }
                   }
                }

*/


                if (hero_plane.blood != 0)   break;


                if (hero_plane.blood == 0 && op == false)
                {
                    /*血量为0计时器关闭*/
                     m_Timer.stop();
                     Enemyshouw();
                     updatePosition();
                     updateback();
                     update();
                   // hero_plane.blood --;
                    op = true;
                    //hero_plane.blood = 3;
                   // QMessageBox :: information(this,"", "Game over!"


                     op = true;

                }
            }

    }
}

void Mainscene::updateback()
{
    /*随时根据计时器更新右上角的信息*/
    QString str = "live:" + QString::number(hero_plane.blood);
    ui->label -> setText(str);
    ui -> label -> setStyleSheet("color:blue;");
    str = "bullet:" + QString::number(hero_plane.Num_bullet);
    ui->label_2 -> setText(str);
    ui->label_2 -> setStyleSheet("color:blue;");
    str = "bomb:" + QString::number(hero_plane.Num_bomb);
    ui->label_3 -> setText(str);
    ui->label_3 -> setStyleSheet("color:blue;");
    str = "score:" + QString::number(score);
    ui->label_4 -> setText(str);
    ui->label_4 -> setStyleSheet("color:blue;");
}

int Mainscene::get_score()
{
    /*传递给结束界面窗口函数*/
    return score;
}


主函数:

#include "mainscene.h"
#include "game.h"
#include "menu.h"

#include <QApplication>
#include <QResource>
#include <QIcon>
#include <QtSql/QSqlDatabase>
#include<QDebug>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QCoreApplication>
#include"rank.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);



    QResource::registerResource("./plane.rcc");  //导入资源文件

    //创建数据库-----

    QSqlDatabase  db=QSqlDatabase::addDatabase("QSQLITE");

    //设置数据库名
    db.setDatabaseName("pwd.db");

    //打开数据库
    db.open();

    //创建数据库表

   QString cmd="create table if not exists forms(username varchar(64),password varchar(64), score int)";

   //执行sql语句
   QSqlQuery query;

   query.exec(cmd);

    menu ww;
    ww.show();  //显示主菜单界面

    Mainscene w;
    w.setWindowTitle(Game_title);
    w.setWindowIcon(QIcon(":/resource/me1.png"));
    //w.show();
    return a.exec();
}

地图函数:

#include "map.h"
#include "game.h"

map::map()
{
    m_map1.load(Map_path);
    m_map2.load(Map_path);

    m_map1_posY = -Game_height;
    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_height;
    }

    m_map2_posY += m_scroll_speed;
    if (m_map2_posY >= Game_height)
    {
        m_map2_posY = 0;
    }
}

主菜单函数:

#include "menu.h"
#include "ui_menu.h"
#include "mainscene.h"
#include "game.h"
#include "rank.h"
#include "Start.h"

#include <QLineEdit>
#include <QMessageBox>
#include <QFileDialog>
#include <QFile>
#include<QMessageBox>
#include<QKeyEvent>
#include<QtSql/QSqlQuery>
#include<QtSql/QSqlTableModel>
#include <QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QCoreApplication>

menu::menu(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::menu)
{
    ui->setupUi(this);
    /*设置登陆界面的的密码格式以及字体格式*/
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);
    ui->lineEdit->setFont(QFont("Timers" , 16 ,  QFont::Bold));
    ui->lineEdit_2->setFont(QFont("Timers" , 16 ,  QFont::Bold));
}

menu::~menu()
{
    delete ui;
}

bool menu:: check()
{
    /*检查用户密码是否正确,首先提取出用户密码*/
    QString username= this->ui->lineEdit->text();
    QString password=this->ui->lineEdit_2->text();

    //查询数据库验证
    QSqlTableModel *model =new QSqlTableModel;
    model->setTable("forms");
    model->setFilter(QString("username='%1' and password='%2'")
                     .arg(username).arg(password));
    model->select();


    //检测是否有数据
    int row=model->rowCount();//判断用户密码在数据库的第几行
    if(row>0)
    {
       return true;
    }

    return false;
    /*
    QString str;
    QString temp;
    QString filename = QFileDialog::getOpenFileName(this, "打开文件", ".", "Txt(*.txt);;c files(*.c);; cpp files(*.cpp)");
    if(filename.isEmpty() == false)
    {
        QFile file(filename);
        bool isok = file.open(QIODevice::ReadOnly);//以只读的方式打开文件
        if(isok == true)
        {
            QByteArray array;
            while(file.atEnd() == false)
            {
               str = this->ui->lineEdit->text() + " " + this->ui->lineEdit_2->text();
               temp = file.readLine();//将文件中的读取的内容保存在字节数组中。
            }
        }
        file.close();//文件读取完毕后关闭文件。

    }

    if (str == temp)   return true;
    else   return false;
    */
}

void menu::on_pushButton_clicked()
{
    if (check())
    {
        /*弹出开始页面窗口*/
       Start *w = new Start();
        //w -> setWindowTitle(Game_title);
       // w -> setWindowIcon(QIcon(":/resource/me1.png"));
        this -> close();
        w -> show();
        w -> get_str(this->ui->lineEdit->text());
    }
    else{
        /*密码不对的话弹出窗口*/
         QMessageBox :: information(this, this->ui->lineEdit->text(), "PassWord Wrong");
    }
}

void menu::save()
{

    /*读出输入的信息*/

    QString username=ui->lineEdit->text();
     QString password=ui->lineEdit_2->text();
     /*存储到数据库中*/
     QString cmd=QString("insert into forms values('%1','%2','%3')")
             .arg(username) .arg(password).arg(0);
     QSqlQuery query;
     /*执行插入语句*/
     if(query.exec(cmd))
    {
        //QMessageBox::warning(this, tr("注册提示"), tr("注册成功"), QMessageBox::Yes);
     }
    else
     {
       //  QMessageBox::warning(this, tr("注册提示"), tr("注册失败"), QMessageBox::Yes);
     }

    /*
    QString filename = QFileDialog::getSaveFileName(this, "save.txt", ".", "Txt(*.txt);;c files(*.c);;c++ files(*.cpp)");
      if(filename.isEmpty() == false)
      {
          QFile file(filename);
          bool isok = file.open(QIODevice::WriteOnly);
          if(isok == true)
          {
              QString str = this->ui->lineEdit->text() + " " + this->ui->lineEdit_2->text();
              file.write(str.toUtf8());
          }
          file.close();
      }
      */
/*
    QFile file("C:/Users/wwb0719/Desktop/save.txt");
    QString str = this->ui->lineEdit->text() + " " + this->ui->lineEdit_2->text();
    fputs(str, file);
   // file.write()
   */
}

void menu::on_pushButton_2_clicked()
{
    /*弹出提示注册成功的窗口*/
    QMessageBox :: information(this, this->ui->lineEdit->text(), "Register successful");
    save();
}

void menu::on_pushButton_3_clicked()
{
   // QSqlTableModel *model =new QSqlTableModel;
    // model->setTable("userinforms");

    /*关闭原来窗口,弹出分数排行*/
    rank *w = new rank();
    this -> close();
    w -> show();


}

我方飞机函数:

#include "my_plane.h"
#include "game.h"
#include "Enemyplane.h"
#include "mainscene.h"
#include "ui_mainscene.h"
#include "game.h"
#include "my_plane.h"
#include "my_plane.h"
#include <QIcon>
#include <QPainter>
#include <QKeyEvent>
#include <QMouseEvent>
#include <ctime>
#include <QLabel>

My_plane::My_plane()
{
    m_plane.load(Plane_path); //飞机初始化
    m_x = (Game_width - m_plane.width()) * 0.5;
    widths = m_plane.width();
    heights = m_plane.height();
    m_y = Game_height-m_plane.height();
    //Num_bullet = 80;
    Num_bomb = 1;
    blood = 3;
    Num_bullet = 20;
    m_free = true;  /*标记飞机此时的状态*/
    m_Rect.setWidth(m_plane.width());
    m_Rect.setHeight(m_plane.height());
    m_Rect.moveTo(m_x, m_y);
    flag = true;
    flag2 = true;
    check_bomb = true;
    cnt ++;
}

void My_plane::shoot_bullet()
{
    m_recorder ++;

    if (flag && flag2)
    {
        return;
    }

    /*设计单倍子弹*/
    if (flag == false)
    {
        for (int i = 0; i < Bullet_num; i ++)
        {
            if (m_bullets[i].m_free == true)
            {
                /*设置子弹初始位置,标记子弹状态*/
                m_bullets[i].m_free = false;
                m_bullets[i].m_x = m_x + m_Rect.width() * 0.5 + 5;
                m_bullets[i].m_y = m_y - 30;
                break;
            }
        }
    }

    /*设计双倍子弹*/
    if (flag2 == false)
    {
        /*连续设置两个子弹的初始位置,标记子弹状态*/
        for (int i = 0; i < Bullet_num; i ++)
        {
            if (m_bullets[i].m_free == true)
            {
                m_bullets[i].m_free = false;
                m_bullets[i].m_x = m_x ;
                m_bullets[i].m_y = m_y - 30;
                break;
            }
        }

        for (int i = 0; i < Bullet_num; i ++)
        {
            if (m_bullets[i].m_free == true)
            {
                m_bullets[i].m_free = false;
                m_bullets[i].m_x = m_x + m_Rect.width() * 0.5 + 30;
                m_bullets[i].m_y = m_y - 30;
                break;
            }
        }
    }

    /*保证只射击一次*/
    flag2 = true;
    flag = true;


}

void My_plane::shoot_bomb()
{
    if (check_bomb)
    {
        return;
    }

    check_bomb = true;
    for (int i = 0; i < Enemy_num; i ++)
    {
  //     m_enemy[i].m_free = true;
    }
    return;
}

void My_plane::setPosition(int x, int y)
{
    /*更改飞机位置并且移动边框*/
    if (x <= 0)             x = 0;
    if (x >= Game_width - m_plane.width())    x = Game_width - m_plane.width();
    if (y <= 0)             y = 0;
    if (y >= Game_height - m_plane.height())    y = Game_height - m_plane.height();
    m_x = x;
    m_y = y;
    m_Rect.moveTo(m_x, m_y);
}

暂停函数:

#include "Pause.h"

Pause::Pause()
{
    /*是否按下去,游戏是否进行*/
    pause_pressed = false;
    game_paused = false;
    pause_background.load(Map_path);
    pause_pic1.load(Pause1_path);
    pause_pic2.load(Pause2_path);
    start_pic1.load(Pause3_path);
    start_pic2.load(Pause4_path);
    game_ends.load(Game_ends);
    pause_pic_posX = 0;
    pause_pic_posY = 0;
}

等级函数:

#include "rank.h"
#include "ui_rank.h"
#include "menu.h"
#include<QtSql/QSqlQuery>
#include<QtSql/QSqlTableModel>
#include <QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QMessageBox>
#include <QCoreApplication>

rank::rank(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::rank)
{
     ui->setupUi(this);
     //QString update_sql = QString("update forms set score = %1 where username = %2").arg(10).arg("123");
      //QSqlQuery sql_query;
     // sql_query.exec(update_sql);


      //if (sql_query.exec(update_sql))
      //{
           //QMessageBox :: information(this, username, QString::number(score));
      //}

    /*查询数据库的语句*/
    QString cmd=QString("select username, score from forms order by score desc");

    QSqlQuery query;


    /*执行查询操作*/
    if(query.exec(cmd))
   {

      // QMessageBox::warning(this, tr("注册提示"), tr("注册成功"), QMessageBox::Yes);
    }
   else
    {
        //QMessageBox::warning(this, tr("注册提示"), tr("注册失败"), QMessageBox::Yes);
    }



    while(query.next())
    {
        /*提取出姓名和分数*/
        QString name = query.value(0).toString();
        QString score = query.value(1).toString();
       // QMessageBox::warning(this, tr("注册提示"), tr("注册成功"), QMessageBox::Yes);
        //QMessageBox :: information(this, name, score);
        QString temp = name + "            " + score;
        /*加入大的编辑框*/
        ui -> textEdit->append(temp);
    }


}

rank::~rank()
{
    delete ui;
}

void rank::on_pushButton_clicked()
{
    /*槽,返回菜单界面*/
    menu *w = new menu();
    this -> close();
    w -> show();
}

开始函数:

#include "Start.h"
#include "ui_Start.h"
#include "mainscene.h"

Start::Start(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Start)
{
    ui->setupUi(this);
}

Start::~Start()
{
    delete ui;
}

void Start::get_str(QString username)
{
    str = username;
}

void Start::on_pushButton_clicked()
{
    /*弹出游戏页面*/
    Mainscene *w = new Mainscene();
    w -> setWindowTitle(Game_title);
    w -> setWindowIcon(QIcon(":/resource/me1.png"));
    this -> close();
    w -> show();
    w->get_info(str);
}

另外一个暂停函数:

#include "stop.h"
#include "game.h"
#include <QPixmap>

stop::stop()
{
    Stop.load(Stop_path);

    m_x = Game_width * 0.5;
    m_y = Game_height * 0.5;

    m_free = true;
}

结束游戏UI
在这里插入图片描述

主场景UI
在这里插入图片描述
登陆界面UI
在这里插入图片描述

排行榜UI
在这里插入图片描述
开始游戏UI
在这里插入图片描述

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值