• Qt的简单应用:翻金币游戏( 含源码 )



    一、项目简介

    本游戏一共有二十关,你可以从第一关开始接着来,当你完成第一关之后会自动跳到下一关,你还可以直接选择自己想要闯的关卡。进入关卡之后会有一个4*4的网格,里面分布着金币和银币,点击银币它本身和附近的币就会翻面,当网格中只剩金币时,闯关成功。

    实现效果:

    在这里插入图片描述
    在这里插入图片描述

    二、图片和音效资源

    链接:https://pan.baidu.com/s/1DyG-INZZVFnbaVPzd0EyaQ?pwd=zkh4
    提取码:zkh4

    三、程序设计

    1. 创建主场景

    mainscene

    MainScene::MainScene(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainScene)
    {
        ui->setupUi(this);
    
        //配置主场景
    
        //设置窗口固定大小
        setFixedSize(320,588);
    
        //设置窗口图标
        setWindowIcon(QIcon(":/res/Coin0001.png"));
    
        //设置窗口标题
        setWindowTitle("翻金币主场景");
    
        //退出按钮实现
        connect(ui->actionquit,&QAction::triggered,[=](){
            this->close();
        });
    
        //准备开始按钮的音效
        QSound *startSound=new QSound(":/res/TapButtonSound.wav");
        //startSound->setLoops(10);   //代表循环次数,如果是-1代表无线循环
        startSound->play();
    
        //开始按钮
        MyPushButton *startBtn=new MyPushButton(":/res/MenuSceneStartButton.png");
        startBtn->setParent(this);
        startBtn->move(this->width()*0.35,this->height()*0.7);   //移动到合适的位置
    
        //实例化选择关卡场景
        chooseSence=new ChooseLevelScene;
    
        //监听选择关卡的返回按钮信号
        connect(chooseSence,&ChooseLevelScene::chooseSceneBack,this,[=](){
            this->setGeometry(chooseSence->geometry());
            chooseSence->hide();    //将选择关卡隐藏掉
            this->show();   //重新显示主场景
        });
    
        //点击start按钮 实现弹跳效果
        connect(startBtn,&MyPushButton::clicked,[=](){
    
            //播放开始音效资源
            startSound->play();
    
            //弹跳效果
            startBtn->zoom1();
            startBtn->zoom2();
    
            //延时进入到选择关卡场景中
            QTimer::singleShot(500,this,[=](){    //延时500ms在进行以下操作
    
                //设置chooseScene场景的位置
                chooseSence->setGeometry(this->geometry());
    
                //自身隐藏
                this->hide();
                //显示选择关卡场景
                chooseSence->show();
            });
    
    
        });
    
    }
    
    void MainScene::paintEvent(QPaintEvent *)
    {
        QPainter painter(this);   //声明画家
        //1、画背景图
        QPixmap pix;   //声明对象
        pix.load(":/res/PlayLevelSceneBg.png");   //在对象中加载图片
        painter.drawPixmap(0,0,this->width(),this->height(),pix);   //画背景图
    
        //2、画背景上图标
        pix.load(":/res/Title.png");   //加载图片
        pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);  //因图标有点大,所以在这里按比例缩小一倍
        painter.drawPixmap(10,30,pix);    //画上图标
    }
    
    MainScene::~MainScene()
    {
        delete ui;
    }
    
    • 1
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    2. 自定义开始按钮

    mypushbutton

    MyPushButton::MyPushButton (QString normalImg,QString pressImg)
    {
        this->normalImgPath=normalImg;
        this->pressImgPath=pressImg;
    
        QPixmap pix;
        bool ret=pix.load(normalImg);
        if(!ret)
        {
            qDebug()<<"图片加载失败";
            return;
        }
    
        //设置图片固定大小
        this->setFixedSize(pix.width(),pix.height());
    
        //设置不规则图片样式
        this->setStyleSheet("QPushButton{border:0px;}");
    
        //设置图标
        this->setIcon(pix);
    
        //设置图标大小
        this->setIconSize(QSize(pix.width(),pix.height()));
    
    }
    
    
    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();
    }
    
    void MyPushButton::zoom2()
    {
        //创建动态对象
        QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");
    
        //设置动画时间间隔
        animation->setDuration(200);  //200ms
    
        //起始位置
        animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
        //结束位置
        animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    
        //设置弹跳曲线
        animation->setEasingCurve(QEasingCurve::OutBounce);
    
        //执行动画
        animation->start();
    }
    
    
    void MyPushButton:: mousePressEvent(QMouseEvent *e)
    {
        if(this->pressImgPath!=" ")    //传入的按下图片不为空 说明需要有按下状态 切换图片
        {
            QPixmap pix;
            bool ret=pix.load(pressImgPath);
            if(!ret)
            {
                qDebug()<<"图片加载失败";
                return;
            }
    
            //设置图片固定大小
            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(this->pressImgPath!=" ")    //传入的按下图片不为空 说明需要有按下状态 切换成初始图片
        {
            QPixmap pix;
            bool ret=pix.load(normalImgPath);
            if(!ret)
            {
                qDebug()<<"图片加载失败";
                return;
            }
    
            //设置图片固定大小
            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);
    }
    
    • 1
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122

    3. 创建选择关卡场景

    chooselevelscene

    ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
    {
        //配置选择关卡场景
        this->setFixedSize(320,588);
    
        //设置图标
        this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    
        //设置标题
        this->setWindowTitle("选择关卡场景");
    
        //创建菜单栏
        QMenuBar *bar=menuBar();
        setMenuBar(bar);
    
        //创建开始菜单
        QMenu *startMenu=bar->addMenu("开始");
    
        //创建退出 菜单项
        QAction *quitAction=startMenu->addAction("退出");
    
        //点击退出 实现退出游戏
        connect(quitAction,&QAction::triggered,[=](){
            this->close();
        });
    
        //选择关卡按钮音效
        QSound *chooseSound=new QSound(":/res/TapButtonSound.wav");
        //返回按钮音效
        QSound *backSound=new QSound(":/res/BackButtonSound.wav");
    
        //返回按钮
        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,[=](){
            //播放返回按钮音效
            backSound->play();
    
            //qDebug()<<"点击了返回按钮";
            //告诉主场景 我返回了,主场景监听ChooseSceneBack的返回按钮
            //延时返回
            QTimer::singleShot(500,this,[=](){
                emit this->chooseSceneBack();
            });
        });
    
        //创建选择关卡的按钮
        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,[=](){
    
                //播放选择关卡音效
                chooseSound->play();
    
                QString str=QString("你选择的是第 %1 关").arg(i+1);
                qDebug()<<str;
    
                //进入到游戏场景
                this->hide();   //将选关场景隐藏掉
                play=new PlayScene(i+1);  //创建游戏场景
    
                //设置游戏场景的初始位置
                play->setGeometry(this->geometry());
    
                play->show();   //显示游戏场景
    
    
                connect(play,&PlayScene::chooseSceneBack,[=](){
                    this->setGeometry(play->geometry());
                    this->show();
                    delete play;
                    play=NULL;
                });
            });
    
            QLabel *label=new QLabel;
            label->setParent(this);
            label->setFixedSize(menuBtn->width(),menuBtn->height());
            label->setText(QString::number(i+1));
            label->move(25+i%4*70,130+i/4*70);
    
            //设置label上的文字对齐方式  水平居中和垂直居中
            label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            //设置让鼠标进行穿透  51号属性
            label->setAttribute(Qt::WA_TransparentForMouseEvents);
        }
    }
    
    
    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
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110

    4. 创建翻金币场景

    playscene

    PlayScene::PlayScene(int levelNum)
    {
        QString str=QString("进入了第 %1 关").arg(levelNum);
        qDebug()<<str;
        this->levelIndex=levelNum;
    
        //初始化游戏场景
        //设置固定大小
        this->setFixedSize(320,588);
    
        //设置图标
        this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    
       //设置标题
         this->setWindowTitle("翻金币场景");
    
       //创建菜单栏
        QMenuBar *bar=menuBar();
        setMenuBar(bar);
    
        //创建开始菜单
        QMenu *startMenu=bar->addMenu("开始");
    
        //创建退出 菜单项
        QAction *quitAction=startMenu->addAction("退出");
    
        //点击退出 实现退出游戏
        connect(quitAction,&QAction::triggered,[=](){
        this->close();
            });
    
        //添加音效资源
        //返回按钮音效
        QSound *backSound=new QSound(":/res/BackButtonSelected.png",this);
        //翻金币音效
        QSound *flipSound=new QSound(":/res/ConFlipSound.wav",this);
        //胜利按钮音效
        QSound *winSound=new QSound(":/res/LevelWinSound.wav",this);
    
    
        //返回按钮
        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,[=](){
            //播放返回按钮音效
            backSound->play();
    
            qDebug()<<"翻金币场景中:点击了返回按钮";
    
            QTimer::singleShot(500,this,[=](){
                emit this->chooseSceneBack();
            });
        });
    
        //显示当前关卡数
        QLabel *label=new QLabel;   //创建QLabel类控件对象
        label->setParent(this);
        QFont font;    //创建字体对象
        font.setFamily("华文新魏");   //设置字体
        font.setPointSize(20);   //设置字号大小
        QString str1=QString("Level: %1").arg(this->levelIndex);
        label->setFont(font);   //将字体设置到标签控件中
        label->setText(str1);   //设置控件文本( Level: %1 )
        label->setGeometry(30,this->height()-50,120,50);   //设置控件的大小和位置
    
        dataConfig config;
        //初始化每个关卡的二维数组
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
            }
        }
    
        //胜利图片显示
        QLabel *winLabel=new QLabel;
        QPixmap temPix;
        temPix.load(":/res/LevelCompletedDialogBg.png");
        winLabel->setGeometry(0,0,temPix.width(),temPix.height());
        winLabel->setPixmap(temPix);
        winLabel->setParent(this);
        winLabel->move((this->width()-temPix.width()*0.5),-temPix.height());
    
        //显示金币的背景图案
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                //绘制背景图片
                QPixmap pix=QPixmap(":/res/BoardNode(1).png");
                QLabel *label1=new QLabel;
                label1->setGeometry(0,0,pix.width(),pix.height());
                label1->setPixmap(pix);
                label1->setParent(this);
                label1->move(57+i*50,200+j*50);
    
                //创建金币
                QString str;
                if(this->gameArray[i][j]==1)
                {
                    //显示金币
                    str=":/res/Coin0001.png";
                }
                else
                {
                    str=":/res/Coin0008.png";
                }
    
    
                MyCoin *coin=new MyCoin(str);
                coin->setParent(this);
                coin->move(59+i*50,204+j*50);
    
                //给金币赋值
                coin->posX=i;
                coin->posY=j;
                coin->flag=this->gameArray[i][j];   //1正面 0反面
    
                //将金币放入到 金币的二维数组里 以便于后期的维护
                coinBtn[i][j]=coin;
    
                //点击金币 进行翻转
                connect(coin,&MyCoin::clicked,[=](){
                    //翻金币音效
                    flipSound->play();
    
                    for(int i=0;i<4;i++)
                    {
                        for(int j=0;j<4;j++)
                        {
                            this->coinBtn[i][j]->isWin=true;
                        }
                    }
    
                coin->changeFlag();
    
                this->gameArray[i][j]=this->gameArray[i][j]==0?1:0;
    
                //翻转周围硬币,延时翻转
                QTimer::singleShot(300,this,[=](){
                    //周围的右侧金币反转的条件
                    if(coin->posX+1<=3)
                    {
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY]=this->gameArray[coin->posX+1][coin->posY]==0?1:0;
                    }
                    //周围的左侧金币反转的条件
                    if(coin->posX-1>=0)
                    {
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX-1][coin->posY]=this->gameArray[coin->posX-1][coin->posY]==0?1:0;
                    }
                    //周围上侧的金币反转的条件
                    if(coin->posY+1<=3)
                    {
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY+1]=this->gameArray[coin->posX][coin->posY+1]==0?1:0;
                    }
                    //周围上侧的金币反转的条件
                    if(coin->posY-1>=0)
                    {
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY-1]=this->gameArray[coin->posX][coin->posY-1]==0?1:0;
                    }
    
                    for(int i=0;i<4;i++)
                    {
                        for(int j=0;j<4;j++)
                        {
                            this->coinBtn[i][j]->isWin=false;
                        }
                    }
    
    
                    //判断是否胜利
                    this->isWin=true;
                    for(int i=0;i<4;i++)
                    {
                        for(int j=0;j<4;j++)
                        {
                            if(coinBtn[i][j]->flag==false)  //只要有一个是反面,那就算失败
                            {
                                this->isWin=false;
                                break;
                            }
                        }
                    }
                    if(this->isWin==true)
                    {
                        //添加胜利音效
                        winSound->play();
                        qDebug()<<"游戏胜利";
    
                        //将所有按钮的胜利标志改为true  如果再次点击按钮,直接return,不作响应
                        for(int i=0;i<4;i++)
                        {
                            for(int j=0;j<4;j++)
                            {
                                coinBtn[i][j]->isWin=true;
                            }
                        }
    
                        //将胜利的图片移动下来
                        QPropertyAnimation *animation=new QPropertyAnimation(winLabel,"geometry");
    
                        //设置时间间隔
                        animation->setDuration(1000);
    
                        //设置开始位置
                        animation->setStartValue(QRect(winLabel->x()-150,winLabel->y(),winLabel->width(),winLabel->height()));
    
                        //设置结束位置
                        animation->setEndValue(QRect(winLabel->x()-150,winLabel->y()+120,winLabel->width(),winLabel->height()));
    
                        //设置缓和曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);
    
                        //执行动画
                        animation->start();
                    }
                });
    
    
                });
            }
        }
    }
    
    
    void PlayScene::paintEvent(QPaintEvent *)
    {
        //创建背景
        QPainter painter(this);
        QPixmap pix;
        pix.load(":/res/PlayLevelSceneBg.png");
        painter.drawPixmap(0,0,this->width(),this->height(),pix);
    
        //加载标题
        pix.load(":/res/Title.png");
        pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
        painter.drawPixmap(10,30,pix.width(),pix.height(),pix);
    }
    
    • 1
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246

    5. 设置金币及其翻转效果

    mycoin

    MyCoin::MyCoin(QString btnImg)
     {
         QPixmap pix;
         bool ret=pix.load(btnImg);
         if(!ret)
         {
             QString str=QString("图片 %1 加载失败").arg(btnImg);
             qDebug()<<str;
             return;
         }
         this->setFixedSize(pix.width(),pix.height());
         this->setStyleSheet("QPushButton{border:0px;}");
         this->setIcon(pix);
         this->setIconSize(QSize(pix.width(),pix.height()));
    
         //初始化定时器对象
         timer1=new QTimer(this);
         timer2=new QTimer(this);
    
         //监听正面翻反面的信号,并且翻转金币
         connect(timer1,&QTimer::timeout,[=](){
             QPixmap pix;
             QString str=QString(":/res/Coin000%1.png").arg(this->min++);
             pix.load(str);
    
             this->setFixedSize(pix.width(),pix.height());
             this->setStyleSheet("QPushButton{border:0px;}");
             this->setIcon(pix);
             this->setIconSize(QSize(pix.width(),pix.height()));
    
             //判断 如果翻完了,将min重置为1
             if(this->min > this->max)
             {
                 this->min=1;
                 isAnimation=false;   //关闭做动画
                 timer1->stop();
             }
    
         });
    
    
         //监听反面翻正面的信号,并且翻转金币
         connect(timer2,&QTimer::timeout,[=](){
             QPixmap pix;
             QString str=QString(":/res/Coin000%1.png").arg(this->max--);
             pix.load(str);
    
             this->setFixedSize(pix.width(),pix.height());
             this->setStyleSheet("QPushButton{border:0px;}");
             this->setIcon(pix);
             this->setIconSize(QSize(pix.width(),pix.height()));
    
             //判断 如果翻完了,将max重置为8
             if(this->max < this->min)
             {
                 this->max=8;
                 isAnimation=false;   //关闭做动画
                 timer2->stop();
             }
    
         });
     }
    
    
    //改变正反面标志的方法
     void MyCoin::changeFlag()
     {
         //如果是正面 翻成反面
         if(this->flag)
         {
             //开始正面翻反面的定时器
             timer1->start(30);
             isAnimation=true;   //开始做动画
             this->flag=false;
         }
         else    //反面翻正面
         {
              timer2->start(30);
              isAnimation=true;   //开始做动画
              this->flag=true;
         }
     }
    
    
     void MyCoin::mousePressEvent(QMouseEvent *e)
     {
         if(this->isAnimation|this->isWin)
         {
             return;
         }
         else
         {
            QPushButton::mousePressEvent(e);
         }
     }
    
    
    • 1
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    6. 设置关卡的难易程度

    dataconfig

    dataConfig::dataConfig(QObject *parent) : QObject(parent)
    {
    
         int array1[4][4] = {{1, 1, 1, 1},
                            {1, 1, 0, 1},
                            {1, 0, 0, 0},
                            {1, 1, 0, 1} } ;
    
         QVector< QVector<int>> v;
         for(int i = 0 ; i < 4;i++)
         {
             QVector<int>v1;
             for(int j = 0 ; j < 4;j++)
             {
    
                v1.push_back(array1[i][j]);
             }
             v.push_back(v1);
         }
    
         mData.insert(1,v);
    
    
         int array2[4][4] = { {1, 0, 1, 1},
                              {0, 0, 1, 1},
                              {1, 1, 0, 0},
                              {1, 1, 0, 1}} ;
    
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array2[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(2,v);
    
    
    
         int array3[4][4] = {  {0, 0, 0, 0},
                               {0, 1, 1, 0},
                               {0, 1, 1, 0},
                               {0, 0, 0, 0}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array3[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(3,v);
    
    
         int array4[4][4] = {   {0, 1, 1, 1},
                                {1, 0, 0, 1},
                                {1, 0, 1, 1},
                                {1, 1, 1, 1}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array4[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(4,v);
    
    
         int array5[4][4] = {  {1, 0, 0, 1},
                               {0, 0, 0, 0},
                               {0, 0, 0, 0},
                               {1, 0, 0, 1}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array5[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(5,v);
    
    
         int array6[4][4] = {   {1, 0, 0, 1},
                                {0, 1, 1, 0},
                                {0, 1, 1, 0},
                                {1, 0, 0, 1}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array6[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(6,v);
    
    
         int array7[4][4] = {   {0, 1, 1, 1},
                                {1, 0, 1, 1},
                                {1, 1, 0, 1},
                                {1, 1, 1, 0}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array7[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(7,v);
    
         int array8[4][4] = {  {0, 1, 0, 1},
                               {1, 0, 0, 0},
                               {0, 0, 0, 1},
                               {1, 0, 1, 0}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array8[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(8,v);
    
         int array9[4][4] = {   {1, 0, 1, 0},
                                {1, 0, 1, 0},
                                {0, 0, 1, 0},
                                {1, 0, 0, 1}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array9[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(9,v);
    
    
    
         int array10[4][4] = {  {1, 0, 1, 1},
                                {1, 1, 0, 0},
                                {0, 0, 1, 1},
                                {1, 1, 0, 1}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array10[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(10,v);
    
    
         int array11[4][4] = {  {0, 1, 1, 0},
                                {1, 0, 0, 1},
                                {1, 0, 0, 1},
                                {0, 1, 1, 0}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array11[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(11,v);
    
         int array12[4][4] = {  {0, 1, 1, 0},
                                {0, 0, 0, 0},
                                {1, 1, 1, 1},
                                {0, 0, 0, 0}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array12[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(12,v);
    
    
         int array13[4][4] = {    {0, 1, 1, 0},
                                  {0, 0, 0, 0},
                                  {0, 0, 0, 0},
                                  {0, 1, 1, 0}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array13[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(13,v);
    
         int array14[4][4] = {    {1, 0, 1, 1},
                                  {0, 1, 0, 1},
                                  {1, 0, 1, 0},
                                  {1, 1, 0, 1}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array14[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(14,v);
    
    
         int array15[4][4] = {   {0, 1, 0, 1},
                                 {1, 0, 0, 0},
                                 {1, 0, 0, 0},
                                 {0, 1, 0, 1}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array15[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(15,v);
    
    
         int array16[4][4] = {   {0, 1, 1, 0},
                                 {1, 1, 1, 1},
                                 {1, 1, 1, 1},
                                 {0, 1, 1, 0}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array16[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(16,v);
    
         int array17[4][4] = {  {0, 1, 1, 1},
                                {0, 1, 0, 0},
                                {0, 0, 1, 0},
                                {1, 1, 1, 0}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array17[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(17,v);
    
    
         int array18[4][4] = { {0, 0, 0, 1},
                               {0, 0, 1, 0},
                               {0, 1, 0, 0},
                               {1, 0, 0, 0}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array18[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(18,v);
    
         int array19[4][4] = {   {0, 1, 0, 0},
                                 {0, 1, 1, 0},
                                 {0, 0, 1, 1},
                                 {0, 0, 0, 0}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array19[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(19,v);
    
         int array20[4][4] = {  {0, 0, 0, 0},
                                {0, 0, 0, 0},
                                {0, 0, 0, 0},
                                {0, 0, 0, 0}} ;
         v.clear();
         for(int i = 0 ; i < 4;i++)
         {
              QVector<int>v1;
              for(int j = 0 ; j < 4;j++)
              {
                 v1.push_back(array20[i][j]);
              }
              v.push_back(v1);
         }
    
         mData.insert(20,v);
    
    • 1
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
  • 相关阅读:
    LVS+keepalived高可用集群
    nginx学习笔记
    【Vue 开发实战】基础篇 # 7:理解虚拟DOM及key属性的作用
    设计人工智能产品:技术可能性、用户合意性、商业可行性
    用python开发一个炸金花小游戏
    Python读取TCP的4字节浮点数
    初始网络原理
    2022 年杭电多校第七场补题记录
    猴子吃桃问题--c语言
    银河麒麟V10安装MySQL8.0.28并实现远程访问
  • 原文地址:https://blog.csdn.net/Dustinthewine/article/details/126785175