Implementation of QT gold coin flipping game (1)

It should be noted that:
This project is based on the click here video explanation in Station B It's done. If you have any questions, please don't complain.

1. Basic configuration of the project

Create a Qt project, select the base class as MianWindow and check Create Interface, click Finish, and the project is created successfully. Then add resource files, which mainly include: start, return, background, etc. pictures and corresponding sound effects, etc. The specific steps for adding resources are relatively simple, so I won’t go into details here.

2. Configuration of the main scene of the game

2.1 Create menu bar
Open the .ui file and create *"Start"menu bar and its sub-items< a i=3> "Exit"*, and delete the built-in toolbar and status bar, because they will not be used in subsequent steps. The creation results are as follows:
Insert image description here
2.2 Basic configuration of the main scene
Then, return to the .cpp file of the main scene and create the size, icon and window of the main scene. title. The code and results are as follows:

    this->setFixedSize(320,588);

    this->setWindowIcon( QPixmap(":/res/Coin0001.png"));

    this->setWindowTitle("玩一玩,爽得很");

Insert image description here
2.3 Draw the background of the main scene
Implement the previously defined exit button and add the background image. Otherwise it would be uncomfortable to watch. Add paintEvent to draw the background, the code is:

 connect(ui->quitaction,&QAction::triggered,[=](){
    
    
        this->close();
});
///这是分开的/
void MainWindow::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(10,30,pix.width(),pix.height(),pix);

}

It does look much better after joining.
Insert image description here
2.4 Create a start button
The next step is to create a "Start Game" button and configure sound effects for it. Here you need to add a MyPushButton.cpp file. Specially used for the implementation of pressing buttons. In the .cpp file, comment out the previous constructor and create a new constructor by yourself to display the effects of pressing different pictures. When adding sound effects, you need to pay attention to: Add multimedia to the .pro file, otherwise the sound effect header file will not be displayed...
I am too lazy to take a video to show the sound effect results. The code and results of the ""Start Game" button As follows:

MyPushButton::MyPushButton(QString normalImg, QString pressImg)
{
    
          
        normalImgPath = normalImg;
        pressedImgPath = pressImg;
        //创建QPixmap对象
        QPixmap pixmap;
        bool ret = pixmap.load(normalImgPath);
        if(!ret)
        {
    
    
            qDebug() << normalImg << "加载图片失败!";
        }
        this->setFixedSize( pixmap.width(), pixmap.height() );
        //设置不规则图片的样式表
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pixmap);
      this->setIconSize(QSize(pixmap.width(),pixmap.height()));
}

Insert image description here
2.5 Add special effects of the start button
After the start game button is ready, add some special effects to it so that the user will have a jumping effect after clicking it to increase the user experience. The specific method is: first add the Zoom1 function to the MyPushButton.h file to indicate a downward jump, add the zoom2 function to indicate an upward jump, and implement the above two functions in the .cpp file. To realize jumping up and down, you need to use a header file #include<QPropertyAnimation>. The specific code of zoom1 is as follows. The difference from zoom1 is that zoom2 is only different in position:

void MyPushButton::zoom1()
{
    
    
    QPropertyAnimation* animation = new QPropertyAnimation(this,"geometry");
    animation->setDuration(200);//时间间隔为200ms

    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));

    animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));

    animation->setEasingCurve(QEasingCurve::OutBounce);//特效的跳跃效果

    animation->start();//开始执行特效
}

The final result of the main scene is:
Insert image description here

Guess you like

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