Implementation of QT gold coin flipping game (2)

3. Select the configuration of the level scene

3.1 Creation of selected levels
After completing the previous steps, you need to go to the select level interface. Add a C++ file and create a new file with a class name of ChooseLevelScene and a base class of QMainWindow. The QMainWindow file created here needs to be the same as the previous main scene interface. Create a new object for selecting the level scene in the chooselevelScene.h file. Then set a delay of 0.5s to enter the level selection page. The specific method is:

connect(startBtn,&MyPushButton::clicked,[=](){
    
    
        startSound->play();
        startBtn->zoom1();
        startBtn->zoom2();

        QTimer::singleShot(500,this,[=](){
    
    
            this->hide();//主场景隐藏
            chooseScene->show();//选择关卡界面显示
        });

    });

The result is like this:
Insert image description here
3.2 Select the basic configuration of the level
It’s ugly, that’s okay! ! ! Continue to repeat the previous steps, which is to set the size, icon, name, etc. of the window, and then draw the background. The code is very simple, and the results are as follows:
3.3 Adding an exit button
One problem currently encountered is that you can only exit by clicking "X" on the level selection interface. There is no way to exit, so add an exit interface and use the code to implement it as follows:

QMenuBar* bar = menuBar();
QMenu* statrMenu = menuBar()->addMenu("开始");
QAction* quitMenu = statrMenu->addAction("退出"); connect(quitMenu,&QAction::triggered,[=](){
    
    
        this->close();
    });

Insert image description here
3.4 Add a return button
After the exit button is implemented, it needs to be implemented to return to the main scene interface, so a return button is added. The return button has two different effects: normal display and click display, so the mouse press and mouse release events in MyPushButton need to be rewritten.

void MyPushButton::mousePressEvent(QMouseEvent *e)
{
    
    
    if(pressImgPath!="")
    {
    
    
        QPixmap pix;
        bool ret = pix.load(pressImgPath);
        if(!ret)
        {
    
    
            qDebug()<<"图片加载失败了";
        }
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mousePressEvent(e);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    
    
    if(normalImgPath!="")
    {
    
    
        QPixmap pix;
        bool ret = pix.load(normalImgPath);
        if(!ret)
        {
    
    
            qDebug()<<"图片加载失败了";
        }
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mouseReleaseEvent(e);

}

3.5 Use the return button to switch scenes
Then create a return button in chooselevelscene.cpp, and use the connect function to connect the main scene and the level selection interface. Here select Delay Switching takes 500ms. The specific code and results are as follows:

 MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    
connect(backBtn,&MyPushButton::clicked,[=](){
    
    
        QTimer::singleShot(500,this,[=](){
    
    
            this->hide();//选择关卡界面隐藏
            emit this->chooseSceneback();//这里定义了一个选择返回主场景的信号,触发以后就返回主界面,并且在主界面的界面需要显示出来。
      /*mianscene.cpp中的代码是这*/ 
      connect(chooseScene,&ChooseLevelScene::chooseSceneback,[=](){
    
    
        this->show();//主场景的页面显示
    });
        });

Insert image description here
3.6 Add the background image of the level
After the basic interface and buttons are implemented, the most important function must be implemented in this interface, which is to select the level button. Use a for loop to write a matrix, using the relationship between remainder and rounding. There are 20 levels in total. For example, the position of Then the position of Y is i/4. When i=0, 1, 2, 3, rounding 4 is itself, i=4, 5, 6, 7, then the result of taking the remainder is also itself, so that Implemented using a for to write a matrix. The specific code and results are as follows:

for(int i=0;i<20;i++)
    {
    
    
        MyPushButton *levelMenu = new MyPushButton(":/res/LevelIcon.png");
        levelMenu->setParent(this);
        levelMenu->move(25+(i%4)*70,130+(i/4)*70);
    }

Insert image description here
3.7 Increase the number of levels
Then use QLabel to label each level. It is necessary to explain the mouse penetration event: if it is not set, you will not be able to click in. The reason is that the label covers the Levelmenu. When a specific level number is clicked, all click events are absorbed by the label, so mouse penetration events must be added. The code and results are as follows:

  QLabel *label = new QLabel;
        label->setParent(this);
        label->setFixedSize(levelMenu->width(),levelMenu->height());//让label的大小与levelMenu一样
        label->setText(QString::number(i+1));//关卡数字+1
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);//设置居中 水平方向与垂直方向
        label->move(25+(i%4)*70,130+(i/4)*70);//与levelMenu的位置相同
        label->setAttribute(Qt::WA_TransparentForMouseEvents);//设置鼠标穿透事件 

Insert image description here

Guess you like

Origin blog.csdn.net/appup/article/details/116781088