Qt GAME_PlanWar(飞机大战)

1,创建窗口:
使用QWidget就行了:
在这里插入图片描述
导入资源文件:
飞机大战资源下载

导入方法

下面时所有成员的头定义,按照图片排序
在这里插入图片描述

#ifndef BOMB_H
#define BOMB_H
#include <QVector>
#include <QPixmap>


class bomb
{
public:
    bomb();

    //更新播放下标,间隔
    void updateInfo();

    //爆炸位置
    int m_X;
    int m_Y;

    //存放爆炸资源数组
    QVector<QPixmap> m_PixArr;

    bool m_state;

    //爆炸切图间隔
    int m_Recoder;

    //爆炸时加载的时间下标
    int m_index;

};

#endif // BOMB_H

#ifndef CONFIG_H
#define CONFIG_H

#define GAME_WIDTH 512
#define GAME_HEGHT 768
#define GAME_TITLE "飞机大战 v1"

#define GAME_MAP_SPEED 1
#define GAME_RATE 10    //定时器刷新时间间隔 单位毫秒
#define BULLET_NUM 6//子弹总数
#define BULLET_INTERVAL 12//子弹发射时间
#define ENEMY_SPEED 5   //敌机移动速度
#define ENEMY_NUM 6    //敌机数量
#define ENEMY_INTERVAL  40//敌机出场的时间间隔
#define SOUND_BACKGROUND ":/res/bg.wav"
#define SOUND_BOMB ":/res/bomb.wav"
#define BOMB_PATH ":/res/bomb-%1.png"
#define BOMB_NUM 20//爆炸数量
#define BOMB_MAX 7//爆炸图片索引
#define BOMB_INTERVAL 20//爆炸切图

#endif // CONFIG_H


#endif // CONFIG_H

#ifndef ENEMYPLAN_H
#define ENEMYPLAN_H
#include <QRect>
#include <QPixmap>
#include "config.h"

class enemyPlan
{
public:
    enemyPlan();

    //设置敌机的位置
    void position();

    //坐标
    int m_X;
    int m_Y;

    //设置边框
    QRect m_Rect;

    //图像
    QPixmap m_EnemyPlan;

    //敌机状态
    bool state;

    //敌机速度
    int speed;

};

#endif // ENEMYPLAN_H

#ifndef HEROPLAN_H
#define HEROPLAN_H
#include <QPixmap>
#include <QRect>
#include <QMouseEvent>
#include "shot.h"
#include "config.h"

class heroplan
{
public:
    //初始化
    heroplan();
    //发射子弹
    void shoot();
    //设置位置
    void setposition(int x, int y);
    //像素映射
    QPixmap plan;
    //位置坐标
    int m_Planx;
    int m_Plany;
    //框
    QRect m_Rect;
    //弹夹
    shot bullets[BULLET_NUM];
    //发射时间间隔
    int m_recoder;
};

#endif // HEROPLAN_H

#ifndef HEROPLAN_H
#define HEROPLAN_H
#include <QPixmap>
#include <QRect>
#include <QMouseEvent>
#include "shot.h"
#include "config.h"

class heroplan
{
public:
    //初始化
    heroplan();
    //发射子弹
    void shoot();
    //设置位置
    void setposition(int x, int y);
    //像素映射
    QPixmap plan;
    //位置坐标
    int m_Planx;
    int m_Plany;
    //框
    QRect m_Rect;
    //弹夹
    shot bullets[BULLET_NUM];
    //发射时间间隔
    int m_recoder;
};

#endif // HEROPLAN_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 SHOT_H
#define SHOT_H
#include<QPixmap>
#include <QRect>

class shot
{
public:
    shot();

    QPixmap m_Bullet;

    //bullet的坐标
    int m_BulletY;
    int m_BulletX;

    int shotspeed;

    void bulletPosition();
    void updatPosition(int x, int y);

    bool shotstate;

    QRect bulletRect;


};

#endif // SHOT_H

下面是源文件,按照图片排序

在这里插入图片描述

#include "bomb.h"
#include "config.h"

bomb::bomb()
{
    for (int i = 1; i < 8; i++)
    {
        QString str = QString(BOMB_PATH).arg(i);
        m_PixArr.push_back(QPixmap(str));
    }

    m_Y = 0;
    m_X = 0;

    m_state = true;

    m_index = 0;

    m_Recoder = 0;
}

void bomb::updateInfo()
{
    if (m_state)
    {
        return;
    }
    m_Recoder++;
    if (m_Recoder < BOMB_INTERVAL)
    {
        return;
    }
    m_Recoder = 0;
    //切换爆炸图片
    m_index++;

    if (m_index > BOMB_MAX - 1)
    {
        m_index = 0;
        m_state = true;
    }


}

#include "enemyplan.h"

enemyPlan::enemyPlan()
{
    m_EnemyPlan.load(":/res/img-plane_1.png");

    state = true;

    speed = ENEMY_SPEED;

    m_Y = 0;
    m_X = 0;

    m_Rect.setWidth(m_EnemyPlan.width());
    m_Rect.setHeight(m_EnemyPlan.height());
    m_Rect.moveTo(m_X,m_Y);

}

void enemyPlan::position()
{
    if (state == true)
    {
        return;
    }

    m_Y += speed;
    m_Rect.moveTo(m_X,m_Y);
    if (m_Y >= GAME_HEGHT)
    {
        state = true;
    }
}

#include "heroplan.h"
#include "config.h"


heroplan::heroplan()
{
    plan.load(":/res/hero2.png");

    m_Planx = (GAME_WIDTH - plan.width()) * 0.5;
    m_Plany = (GAME_HEGHT - plan.height());

    //初始化边框

    m_Rect.setWidth(plan.width());
    m_Rect.setHeight(plan.height());
    m_Rect.moveTo(m_Planx,m_Plany);

    m_recoder = 0;
}

void heroplan::shoot()
{

    m_recoder++;
    if (m_recoder < BULLET_INTERVAL)
    {
        return;
    }

    m_recoder = 0;
    //发射子弹
    for(int i = 0; i < BULLET_NUM; i++)
    {

        if(bullets[i].shotstate == true)
        {
            //将空闲状态改为false
            bullets[i].shotstate = false;
            //设置子弹坐标
            bullets[i].m_BulletX = m_Planx+m_Rect.width()*0.5-12;
            bullets[i].m_BulletY = m_Plany;
            m_Rect.moveTo(m_Planx,m_Plany);
            break;
        }
    }
}


void heroplan::setposition(int x,int y)
{
    m_Plany = y;
    m_Planx = x;
    m_Rect.moveTo(m_Planx,m_Plany);
}



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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    mainSeans w;
    w.show();

    return a.exec();
}

#include "mainseans.h"
#include "ui_mainseans.h"
#include "config.h"
#include "QIcon"
#include <QSound>
#include <bomb.h>


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

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

void mainSeans::initScene()
{
    //设置窗口的固定尺寸

    setFixedSize(GAME_WIDTH, GAME_HEGHT);

    //设置标题

    setWindowTitle(GAME_TITLE);

    //设置图标
    setWindowIcon(QIcon(":/res/app.ico"));

    //定时器设置
    m_Timer.setInterval(GAME_RATE);

    playGame();

    srand((unsigned int)time(NULL));
}

void mainSeans::playGame()
{
    QSound::play(SOUND_BACKGROUND);

    //启动定时器
    m_Timer.start();

    connect(&m_Timer,&QTimer::timeout,[=](){
        updataPosition();
        conllictionDelect();
        update();//更新计时器

    });


}

void mainSeans::updataPosition()
{
    //更新地图坐标
    m_map.mapPosition();

    //更新子弹
     herop.shoot();
    for(int i = 0; i < BULLET_NUM; i++)
    {
        if(herop.bullets[i].shotstate == false)
        {
            herop.bullets[i].bulletPosition();
        }
    }

    //更新敌机
    eplanplay();
    for (int i = 0 ; i < ENEMY_NUM; i++)
    {
        if (eplan[i].state == false)
        {
            eplan[i].position();
        }
    }

    for (int i = 0; i < BOMB_NUM; i++)
    {
        if (m_bombs[i].m_state == false)
        {
            m_bombs[i].updateInfo();
        }
    }
}

void mainSeans::eplanplay()
{
    eptime++;
    if (eptime < ENEMY_INTERVAL)
    {
        return;
    }
    eptime = 0;
    //设置随机数种子

    for (int i = 0; i < ENEMY_NUM; i++)
    {
        if (eplan[i].state == true)
        {
            eplan[i].state = false;
            eplan[i].m_X = rand() % (GAME_WIDTH - eplan[i].m_Rect.width());
            eplan[i].m_Y = 0;
            break;
        }
    }

}

void mainSeans::conllictionDelect()
{
    //遍历所有非空闲敌机
    for (int i = 0; i < ENEMY_NUM; i++)
    {
        if (eplan[i].state == true)
        {
            continue;
        }
        for (int j = 0; j < BULLET_NUM; j++)
        {
            if (herop.bullets[j].shotstate == true)
            {
               continue;
            }
            //p碰撞检测
            if (eplan[i].m_Rect.intersects(herop.bullets[i].bulletRect))
            {
                eplan[i].state = true;
                herop.bullets[j].shotstate = true;
                QSound::play(SOUND_BOMB);
                //播放爆炸效果
                for (int k = 0; k < BOMB_NUM; k++)
                {
                    if (m_bombs[k].m_state == true)
                    {
                        m_bombs[k].m_state = false;

                        m_bombs[k].m_X = eplan[i].m_X;
                        m_bombs[k].m_Y = eplan[i].m_Y;
                        break;
                    }
                }
            }
        }
    }
}

void mainSeans::paintEvent(QPaintEvent *event)
{
    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);

    //绘制飞机
    painter.drawPixmap(herop.m_Planx,herop.m_Plany,herop.plan);
    for(int i = 0; i < BULLET_NUM; i++)
    {
        if(herop.bullets[i].shotstate == false)
        {
            painter.drawPixmap(herop.bullets[i].m_BulletX,herop.bullets[i].m_BulletY,herop.bullets[i].m_Bullet);
        }
    }



    for (int i = 0; i < ENEMY_NUM; i++)
    {
        if (eplan[i].state == false)
        {
            painter.drawPixmap(eplan[i].m_X,eplan[i].m_Y,eplan[i].m_EnemyPlan);
        }

    }

    for (int i = 0; i < ENEMY_NUM; i++)
    {
        if (eplan[i].state == false)
        {
            painter.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_PixArr[m_bombs[i].m_index]);
        }

    }

}

void mainSeans::mouseMoveEvent(QMouseEvent* event)
{
    int x = event->x()-herop.m_Rect.width() * 0.5;
    int y = event->y()-herop.m_Rect.height() * 0.5;
    if (x <= 0)
    {
        x = 0;
    }
    if (x >= GAME_WIDTH - herop.m_Rect.width())
    {
        x= GAME_WIDTH - herop.m_Rect.width();
    }
    if (y <= 0)
    {
        y = 0;
    }
    if (y >= GAME_HEGHT - herop.m_Rect.height())
    {
        y = GAME_HEGHT - herop.m_Rect.height();
    }
    herop.setposition(x,y);
}


#include "map.h"
#include "config.h"

Map::Map()
{
    //初始化地图
    m_map1.load(":/res/img_bg_level_3.jpg");
    m_map2.load(":/res/img_bg_level_3.jpg");

    //初始化Y轴坐标

    m_map1_PosY = -GAME_HEGHT;
    m_map2_PosY = 0;

    m_scroll_speed = GAME_MAP_SPEED;
}

void Map::mapPosition()
{
    //处理第一张图片的滚动位置
    m_map1_PosY += m_scroll_speed;
    m_map2_PosY += m_scroll_speed;
    if(m_map1_PosY >= 0)
    {
        m_map1_PosY = -GAME_HEGHT;
    }
    if(m_map2_PosY >= GAME_HEGHT)
    {
        m_map2_PosY = 0;
    }
}

#include "shot.h"
#include "config.h"

shot::shot()
{
    m_Bullet.load(":/res/bullet_11.png");


    m_BulletX = (GAME_WIDTH - m_Bullet.width()) * 0.5;
    m_BulletY = (GAME_HEGHT - m_Bullet.height() * 3);

    shotstate = true;

    bulletRect.setWidth(m_Bullet.width());
    bulletRect.setHeight(m_Bullet.height());
    bulletRect.moveTo(m_BulletX,m_BulletY);

}

void shot::bulletPosition()
{
    if(shotstate == false)
    {
        m_BulletY -= 20;
        bulletRect.moveTo(m_BulletX,m_BulletY);
    }
    if (m_BulletY < -bulletRect.height())
    {
        shotstate = true;
    }

}

void shot::updatPosition(int x, int y)
{
    if (shotstate == true)
    {
        m_BulletX = x;
        m_BulletY = y;
    }

}

在这里插入图片描述
qt打包教程

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I Am Rex

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值