#include "mainscene.h"
#include "ui_mainscene.h"
#include"config.h"
#include"qicon.h"
#include"ctime"
#include"QSound"
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);
//初始化时间间隔
m_record=0;
//随机数种子
srand((unsigned int)time (NULL));
}
void MainScene::playGame()
{
//播放背景音乐
QSound::play(SOUND_BACKGROUND);
//启动定时器
m_timer.start(10);
//监听信号实现更新地图位置
connect(&m_timer,&QTimer::timeout,[=](){
//敌机出现
enemyToScene();
enemyTwoToScene();
//每隔一段时间更新游戏中元素的位置
updatePosition();
//每隔一段时间调用painterevent实现绘制
update();
//碰撞检测
collisionDetection();
collisionDetection2();
});
}
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();
}
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_enemyPlaneTwo[i].isFree==false)
m_enemyPlaneTwo[i].updatePosition();
}
//播放爆炸动画
for(int i=0;i<BOMB_NUM;i++)
{
if(m_bomb[i].isFree==false)
m_bomb[i].updateInfo();
}
}
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++)
{
if(m_plane.m_bullet[i].isFree==false)
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++)
{
if(m_enemyPlane[i].isFree==false)
painter.drawPixmap(m_enemyPlane[i].m_X,m_enemyPlane[i].m_Y,m_enemyPlane[i].m_enemyPlane);
}
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_enemyPlaneTwo[i].isFree==false)
painter.drawPixmap(m_enemyPlaneTwo[i].m_X,m_enemyPlaneTwo[i].m_Y,m_enemyPlaneTwo[i].m_enemyPlaneTwo);
}
//画爆炸
for(int i=0;i<BOMB_NUM;i++)
{
if(m_bomb[i].isFree==false)
painter.drawPixmap(m_bomb[i].m_X,m_bomb[i].m_Y,m_bomb[i].m_pixArr[m_bomb[i].m_index]);
}
}
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;
}
}
}
void MainScene::enemyTwoToScene()
{
//启动记录
m_record++;
//未达到间隔时间,敌机不出现
if(m_record<ENEMY_INTERVAL)
return;
//达到时间,下一辆敌机可以出现
m_record=0;
//还要再找空闲的敌机
for(int i=0;i<ENEMY_NUM;i++)
{
if(m_enemyPlaneTwo[i].isFree==true)
{
//如果空闲,发出敌机
m_enemyPlaneTwo[i].isFree=false;
//坐标
//出场位置随机
m_enemyPlaneTwo[i].m_X=rand()%(GAME_WIDTH-m_enemyPlaneTwo[i].m_rect.width());
m_enemyPlaneTwo[i].m_Y=-m_enemyPlaneTwo[i].m_rect.height();
//找到一个就退出循环
break;
}
}
}
void MainScene::collisionDetection()
{
//遍历所有非空闲的子弹
for(int i=0;i<BULLET_NUM;i++)
{
if(m_plane.m_bullet[i].isFree==true)
continue;//如果空闲,跳转下一次循环
//遍历所有非空闲的敌机
for(int j=0;j<ENEMY_NUM;j++)
{
if(m_enemyPlane[j].isFree==true)
continue;//如果空闲,跳转下一次循环
//如果飞机和子弹相交,发生碰撞,则将飞机和子弹重置为空闲
if(m_plane.m_bullet[i].m_rect.intersects(m_enemyPlane[j].m_rect))
{
m_plane.m_bullet[i].isFree=true;
m_enemyPlane[j].isFree=true;
//相撞,会发生爆炸
for(int k=0;k<BOMB_NUM;k++)
{
//如果有空闲的炸弹就爆炸
if(m_bomb[k].isFree==true)
{
//播放爆炸音效
QSound::play(SOUND_BOMB);
m_bomb[k].isFree=false;
//设置炸弹的位置
m_bomb[k].m_X=m_enemyPlane[j].m_X;
m_bomb[k].m_Y=m_enemyPlane[j].m_Y;
//找到了一个空闲的就要退出循环
break;
}
}
}
}//在二重循环里,因为要先选出不空闲的,也就是在飞的飞机和子弹,再来判断是不是发生碰撞
}
}
void MainScene::collisionDetection2()
{
//遍历所有非空闲的子弹
for(int i=0;i<BULLET_NUM;i++)
{
if(m_plane.m_bullet[i].isFree==true)
continue;//如果空闲,跳转下一次循环
//遍历所有非空闲的敌机
for(int j=0;j<ENEMY_NUM;j++)
{
if(m_enemyPlaneTwo[j].isFree==true)
continue;//如果空闲,跳转下一次循环
//如果飞机和子弹相交,发生碰撞,则将飞机和子弹重置为空闲
if(m_plane.m_bullet[i].m_rect.intersects(m_enemyPlaneTwo[j].m_rect))
{
m_plane.m_bullet[i].isFree=true;
m_enemyPlaneTwo[j].isFree=true;
//相撞,会发生爆炸
for(int k=0;k<BOMB_NUM;k++)
{
//如果有空闲的炸弹就爆炸
if(m_bomb[k].isFree==true)
{
//播放爆炸音效
QSound::play(SOUND_BOMB);
m_bomb[k].isFree=false;
//设置炸弹的位置
m_bomb[k].m_X=m_enemyPlaneTwo[j].m_X;
m_bomb[k].m_Y=m_enemyPlaneTwo[j].m_Y;
//找到了一个空闲的就要退出循环
break;
}
}
}
}//在二重循环里,因为要先选出不空闲的,也就是在飞的飞机和子弹,再来判断是不是发生碰撞
}
}
为什么我的敌机只出现一种
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
关注当前的逻辑已经分离了两种敌机的生成和绘制,但是可能存在的一个问题是,你的m_record和m_record2逻辑可能会导致其中一种敌机被频繁覆盖。我们可以改进这部分代码,使每种敌机都有自己独立的生成间隔逻辑
- 修改敌机生成时间间隔
// 在 MainScene 类中添加一个新的记录器 int m_record2;// 初始化记录器 void MainScene::initScene() { ... m_record = 0; m_record2 = 0; ... }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) { 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; } } } void MainScene::enemyTwoToScene() { m_record2++; if (m_record2 < ENEMY_INTERVAL) return; m_record2 = 0; for (int i = 0; i < ENEMY_NUM; i++) { if (m_enemyPlaneTwo[i].isFree) { m_enemyPlaneTwo[i].isFree = false; m_enemyPlaneTwo[i].m_X = rand() % (GAME_WIDTH - m_enemyPlaneTwo[i].m_rect.width()); m_enemyPlaneTwo[i].m_Y = -m_enemyPlaneTwo[i].m_rect.height(); break; } } } void MainScene::collisionDetection() { for (int i = 0; i < BULLET_NUM; i++) { if (m_plane.m_bullet[i].isFree) continue; for (int j = 0; j < ENEMY_NUM; j++) { if (m_enemyPlane[j].isFree) continue; if (m_plane.m_bullet[i].m_rect.intersects(m_enemyPlane[j].m_rect)) { m_plane.m_bullet[i].isFree = true; m_enemyPlane[j].isFree = true; for (int k = 0; k < BOMB_NUM; k++) { if (m_bomb[k].isFree) { QSound::play(SOUND_BOMB); m_bomb[k].isFree = false; m_bomb[k].m_X = m_enemyPlane[j].m_X; m_bomb[k].m_Y = m_enemyPlane[j].m_Y; break; } } } } } } void MainScene::collisionDetection2() { for (int i = 0; i < BULLET_NUM; i++) { if (m_plane.m_bullet[i].isFree) continue; for (int j = 0; j < ENEMY_NUM; j++) { if (m_enemyPlaneTwo[j].isFree) continue; if (m_plane.m_bullet[i].m_rect.intersects(m_enemyPlaneTwo[j].m_rect)) { m_plane.m_bullet[i].isFree = true; m_enemyPlaneTwo[j].isFree = true; for (int k = 0; k < BOMB_NUM; k++) { if (m_bomb[k].isFree) { QSound::play(SOUND_BOMB); m_bomb[k].isFree = false; m_bomb[k].m_X = m_enemyPlaneTwo[j].m_X; m_bomb[k].m_Y = m_enemyPlaneTwo[j].m_Y; break; } } } } } } void MainScene::paintEvent(QPaintEvent *event) { QPainter painter(this); 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++) { if (!m_plane.m_bullet[i].isFree) 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++) { if (!m_enemyPlane[i].isFree) painter.drawPixmap(m_enemyPlane[i].m_X, m_enemyPlane[i].m_Y, m_enemyPlane[i].m_enemyPlane); } for (int i = 0; i < ENEMY_NUM; i++) { if (!m_enemyPlaneTwo[i].isFree) painter.drawPixmap(m_enemyPlaneTwo[i].m_X, m_enemyPlaneTwo[i].m_Y, m_enemyPlaneTwo[i].m_enemyPlaneTwo); } for (int i = 0; i < BOMB_NUM; i++) { if (!m_bomb[i].isFree) painter.drawPixmap(m_bomb[i].m_X, m_bomb[i].m_Y, m_bomb[i].m_pixArr[m_bomb[i].m_index]); } }解决 无用评论 打赏 举报