C++学习之QT综合项目二经典翻金币小游戏及打包

发布于:2025-03-11 ⋅ 阅读:(136) ⋅ 点赞:(0)

1.项目简介及创建

#include "chooselevelscene.h"
#include <QMenuBar>
#include <QMenu>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QDebug>
#include <QLabel>
#include <QSound>

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //设置窗口固定尺寸
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":/CoinRes/Coin0001.png"));
    //设置标题
    this->setWindowTitle("选择关卡");

    //选关按钮音效
    QSound * chooseSound = new QSound(":/CoinRes/TapButtonSound.wav",this);
    //返回按钮音效
    QSound * backSound = new QSound(":/CoinRes/BackButtonSound.wav",this);

    //创建菜单栏
    QMenuBar * bar =  menuBar();
    this->setMenuBar(bar);
    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");
    //创建退出菜单项
    QAction * quitAction =  startMenu->addAction("退出");
    //监听退出菜单项
    connect(quitAction,&QAction::triggered,[=](){ this->close(); });

    //返回按钮
    MyPushButton * backBtn = new MyPushButton(":/CoinRes/BackButton.png",":/CoinRes/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height() - backBtn->height());

    //监听返回按钮点击
    connect(backBtn,&MyPushButton::clicked,[=](){
        backSound->play();
        QTimer::singleShot(500,[=](){
            //延时 返回主场景,发送自定义信号
            emit this->chooseSceneBack();
        });
    });


    //创建选择关卡按钮
    for(int i = 0 ; i < 20;i++)
    {
        MyPushButton * menuBtn = new MyPushButton(":/CoinRes/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move( 25 + (i%4)*70 , 130 + (i/4)*70 );
        connect(menuBtn,&MyPushButton::clicked,[=](){
            //qDebug() << "您选择的是第" << i + 1 << "关" ;
            //播放音效
            chooseSound->play();
            if(this->pScene == NULL)
            {
                this->hide();
                this->pScene = new PlayScene(i+1);
                this->pScene->setGeometry(this->geometry());
                this->pScene->show();

                connect(pScene,&PlayScene::chooseSceneBack,[=](){
                    this->setGeometry(this->pScene->geometry());
                    this->show();
                    delete this->pScene;
                    this->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);
        //设置属性 鼠标穿透属性  51
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }

}

void ChooseLevelScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/CoinRes/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/CoinRes/Title.png");
    painter.drawPixmap(this->width()*0.5-pix.width()*0.5,30,pix.width(),pix.height(),pix);
}

2.导入资源

3.主场景搭建

4.开始创建按钮

5.开始按跳跃效果实现

6.选择关卡场景搭建

7.主场景进入选关场景

8.返回按钮创建

9.返回按钮功能实现

10.选关按钮创建

11.选关按钮数字显示

12.游戏场景搭建

13.游戏场景配置

14.游戏场景的返回按钮实现

15.游戏场景显示当前卡号

16.硬币背景图创建

17.硬币类的创建

18.关卡配置数据引入

19.初始化全部关卡

20.翻转硬币实现

21.禁用按钮

22.翻转周围硬币实现

23.游戏胜利检测

24.胜利效果展示

25.胜利后禁用硬币点击

26.音效添加

27.项目优化

28.辅助玩法介绍

29.打包发布流程说明

30.安装和卸载项目


网站公告

今日签到

点亮在社区的每一天
去签到