选择关卡类定义与实现

#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);


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值