#include "enemyplane.h"
#include"config.h"
EnemyPlane::EnemyPlane()
{
//初始化敌机图片
m_enemyPlane.load(ENEMY_PATH);
//初始化敌机位置
m_X=0;
m_Y=0;
//初始化敌机速度
m_speed=ENEMY_SPEED;
//初始化敌机状态
isFree=true;
//初始化矩形框
m_rect.setWidth(m_enemyPlane.width());
m_rect.setHeight(m_enemyPlane.height());
m_rect.moveTo(m_X,m_Y);
}
void EnemyPlane::updatePosition()
{
//如果敌机空闲,则不用计算敌机坐标
if(isFree==true)
{
return;
}
//敌机不空闲,计算敌机坐标
m_Y+=m_speed;
//更新敌机矩形框位置
m_rect.moveTo(m_X,m_Y);
//超出屏幕,重置空闲状态
if(m_Y>GAME_HEIGHT+m_rect.height())
isFree=true;
}
```c++
#ifndef ENEMYPLANE_H
#define ENEMYPLANE_H
#include"qpixmap.h"
#include"qrect.h"
class EnemyPlane
{
public:
EnemyPlane();
//敌机的图片
QPixmap m_enemyPlane;
//敌机的位置
int m_X;
int m_Y;
//敌机的速度
int m_speed;
//敌机的空闲状态
bool isFree;
//更新敌机的位置
void updatePosition();
//给敌机加矩形框,用于碰撞检测
QRect m_rect;
};
#endif // ENEMYPLANE_H
#ifndef MAINSCENE_H
#define MAINSCENE_H
#include <QWidget>
#include"qtimer.h"
#include"qpainter.h"
#include"map.h"
#include"heroplane.h"
#include"QMouseEvent"
#include"bullet.h"
#include"enemyplane.h"
namespace Ui {
class MainScene;
}
class MainScene : public QWidget
{
Q_OBJECT
public:
explicit MainScene(QWidget *parent = 0);
~MainScene();
//初始化
void initScene();
//启动游戏
void playGame();
//设置定时器
QTimer m_timer;
//加入地图对象作为背景
Map m_map;
//地图中元素位置更新
void updatePosition();
//把地图对象绘制到地图中
void paintEvent(QPaintEvent *event);
//加入飞机对象
HeroPlane m_plane;
//重写鼠标事件,实现对飞机的拖拽移动
void mouseMoveEvent(QMouseEvent *event);
//加入敌机对象
EnemyPlane m_enemyPlane[ENEMY_NUM];
//记录敌机出现间隔时间变量
int m_record=0;
//敌机出现
void enemyToScene();
private:
Ui::MainScene *ui;
};
#endif // MAINSCENE_H
```c++
#include "mainscene.h"
#include "ui_mainscene.h"
#include"config.h"
#include"qicon.h"
MainScene::MainScene(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainScene)
{
ui->setupUi(this);
initScene();
//启动游戏
playGame();
//不用一直按着飞机才会动了
setMouseTracking(true);
}
MainScene::~MainScene()
{
delete ui;
}
//初始化游戏场景
void MainScene::initScene()
{
//设置窗口大小
setFixedSize(GAME_WIDTH,GAME_HEIGHT);
//设置窗口标题
setWindowTitle(GAME_TITLE);
//设置窗口图标
setWindowIcon(QIcon(":/res/app.ico"));
//设置刷新间隔
m_timer.setInterval(GAME_RATE);
}
void MainScene::playGame()
{
//启动定时器
m_timer.start(10);
//监听信号实现更新地图位置
connect(&m_timer,&QTimer::timeout,[=](){
//敌机出现
enemyToScene();
//每隔一段时间更新游戏中元素的位置
updatePosition();
//每隔一段时间调用painterevent实现绘制
update();
});
}
void MainScene::updatePosition()
{
//更新地图元素的坐标
m_map.mapUpdate();
//发射子弹
m_plane.shoot();
//更新所有非空闲子弹的坐标
for(int i=0;i<BULLET_NUM;i++)
{
if(m_plane.m_bullet[i].isFree==false)
m_plane.m_bullet[i].updatePosition();
}
//更新所有敌机的位置
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_enemyPlane[i].isFree==false)
m_enemyPlane[i].updatePosition();
}
}
void MainScene::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
//画背景图
//直接画不了map这个对象,只能分别画map里的成员pixmap1、2
painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_pix1);
painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_pix2);
//画飞机
painter.drawPixmap(m_plane.m_X,m_plane.m_Y,m_plane.m_plane);
//画子弹
for(int i=0;i<BULLET_NUM;i++)
{
painter.drawPixmap(m_plane.m_bullet[i].m_X,m_plane.m_bullet[i].m_Y,m_plane.m_bullet[i].m_bullet);
}
//画敌机
for(int i=0;i<ENEMY_NUM;i++)
{
painter.drawPixmap(m_enemyPlane[i].m_X,m_enemyPlane[i].m_Y,m_enemyPlane[i].m_enemyPlane);
}
}
void MainScene::mouseMoveEvent(QMouseEvent *event)
{
//鼠标拖拽实现飞机移动
//偏移量实现鼠标拖拽飞机中心移动(不懂为什么是减去)
int x=event->x()-m_plane.m_plane.width()*0.5;
int y=event->y()-m_plane.m_plane.height()*0.5;
//飞机位置不可超出窗口 边界检测
if(x<=0)
x=0;
if(x>=GAME_WIDTH-m_plane.m_plane.width())
x=GAME_WIDTH-m_plane.m_plane.width();
if(y<=0)
y=0;
if(y>=GAME_HEIGHT-m_plane.m_plane.height())
y=GAME_HEIGHT-m_plane.m_plane.height();
//拖动飞机
m_plane.setPosition(x,y);
}
void MainScene::enemyToScene()
{
//启动记录
m_record++;
//未达到间隔时间,敌机不出现
if(m_record<ENEMY_INTERVAL)
return;
//达到时间,下一辆敌机可以出现
m_record=0;
//还要再找空闲的敌机
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_enemyPlane[i].isFree==true)
//如果空闲,发出敌机
m_enemyPlane[i].isFree=false;
//坐标
m_enemyPlane[i].m_X=rand()%(GAME_WIDTH-m_enemyPlane[i].m_rect.width());
m_enemyPlane[i].m_Y=-m_enemyPlane[i].m_rect.height();
//找到一个就退出循环
break;
}
}