#ifndef CHOOSELEVELSCENE_H
#define CHOOSELEVELSCENE_H
#include <QMainWindow>
#include"playscene.h"
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif
class ChooseLevelScene : public QMainWindow
{
Q_OBJECT
public:
explicit ChooseLevelScene(QWidget *parent = 0);
void paintEvent(QPaintEvent *);
PlayScene *pScene; //创建闯关对象指针
signals:
void chooseSceneBack();
public slots:
};
#endif // CHOOSELEVELSCENE_H
chooselevelscene.cpp
#include "chooselevelscene.h"
#include<QMenu>
#include<QAction>
#include<QMenuBar>
#include<QPainter>
#include"mypushbutton.h"
#include<QTimer>
#include"playscene.h"
#include<QLabel>
ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
this->setFixedSize(320,588);
this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
this->setWindowTitle("选择关卡");
//创建菜单栏
QMenuBar * bar = menuBar();
this->setMenuBar(bar);
//创建开始菜单
QMenu * startMenu = bar->addMenu("开始");
//创建按钮菜单项
QAction * quitAction = startMenu->addAction("退出");
//点击退出 退出游戏
connect(quitAction,&QAction::triggered,[=](){this->close();});
//返回按钮
MyPushButton * closeBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
closeBtn->setParent(this);
closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());
connect(closeBtn,&MyPushButton::clicked,[=](){
//延时500,调用槽函数
QTimer::singleShot(500, this,[=](){
this->hide();
//触发自定义信号,关闭自身
emit this->chooseSceneBack();
}
);
});
pScene=NULL;//场景为空
//创建20个关卡
for(int i = 0 ; i < 20;i++)
{
MyPushButton * menuBtn = new MyPushButton(":/res/LevelIcon.png");
menuBtn->setParent(this);
menuBtn->move(25 + (i%4)*70 , 130+ (i/4)*70);
connect(menuBtn,&MyPushButton::clicked,[=](){
pScene=new PlayScene(i+1);
pScene->show();
});
connect(pScene,&PlayScene::chooseSceneBack,[=](){
this->show();
delete pScene;
pScene = NULL;
});
//按钮上显示的文字
QLabel * label = new QLabel;
label->setParent(this);
label->setFixedSize(menuBtn->width(),menuBtn->height());
label->setText(QString::number(i+1));
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); //设置居中
label->move(25 + (i%4)*70 , 130+ (i/4)*70);
label->setAttribute(Qt::WA_TransparentForMouseEvents,true); //鼠标事件穿透
}
}
void ChooseLevelScene::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap pix;
pix.load(":/res/OtherSceneBg.png");
painter.drawPixmap(0,0,this->width(),this->height(),pix);
//加载标题
pix.load(":/res/Title.png");
painter.drawPixmap( (this->width() - pix.width())*0.5,30,pix.width(),pix.height(),pix);
}