QT翻金币

【QT项目】QT基础入门——项目案例翻金币小游戏 翻金币项目是一款经典的益智类游戏,我们需要将金币都翻成同色,才视为胜利。 阅读详情

QT翻金币

在B站跟着视频进行QT学习,现把代码全部贴上来,备忘
整体解决方案文件结构如下:
在这里插入图片描述
chooselevelscene.h

#ifndef CHOOSELEVELSCENE_H
#define CHOOSELEVELSCENE_H

#include <QMainWindow>
#include"playscene.h"

class ChooseLevelScene : public QMainWindow
{
    Q_OBJECT
public:
    explicit ChooseLevelScene(QWidget *parent = nullptr);

    void  paintEvent(QPaintEvent *event);
    playscene *play = NULL;

signals:
    void chooseSceneBack();
};

#endif // CHOOSELEVELSCENE_H

dataconfig.h

#ifndef DATACONFIG_H
#define DATACONFIG_H

#include <QObject>
#include<QMap>
#include<QVector>

class DataConfig : public QObject
{
    Q_OBJECT
public:
    explicit DataConfig(QObject *parent = nullptr);

public:
    QMap<int, QVector <QVector<int>>>mData;
signals:
};

#endif // DATACONFIG_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void paintEvent(QPaintEvent *event);

private slots:
    void on_actiontuichu_triggered();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mycoin.h.h

#ifndef MYCOIN_H
#define MYCOIN_H

#include <QPushButton>>
#include<QTimer>
class MyCoin : public QPushButton
{
    Q_OBJECT
public:
    //explicit MyCoin(QWidget *parent = nullptr);
    MyCoin(QString btnImage);
    void  mousePressEvent(QMouseEvent * e);
    int posX;
    int posY;
    bool flag;

    void changeFlag();
    QTimer *time1;
    QTimer *time2;
    int min;
    int max;
    bool isAnimation = false;
    bool iswin = false;



signals:
};

#endif // MYCOIN_H

mypushbutton.h

#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H

#include <QWidget>
#include<QPushButton>

class MyPushButton : public QPushButton
{
    Q_OBJECT
public:
    MyPushButton(QString p_naormalimage , QString p_pressimage="");
    void zoom1();
    void zoom2();

    QString m_NormalImagePath;
    QString m_PressImagePath;

    void mousePressEvent(QMouseEvent *e) ;
    void mouseReleaseEvent(QMouseEvent *e) ;
signals:
};

#endif // MYPUSHBUTTON_H

playscene.h

#ifndef PLAYSCENE_H
#define PLAYSCENE_H

#include <QMainWindow>
#include"mycoin.h"

class playscene : public QMainWindow
{
    Q_OBJECT
public:
    void  paintEvent(QPaintEvent *event);
    playscene(int levelnumber);
    int levelindex;//记住所选的关卡
    bool isWin();
    bool  SetWin();

    int gameArray[4][4];
    MyCoin *coinBtn[4][4];
    bool iswin;
signals:
    void playSceneBack();
};

#endif // PLAYSCENE_H

chooselevelscene.cpp

#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include"mypushbutton.h"
#include<QDebug>
#include<qtimer.h>
#include<QLabel>
#include<playscene.h>
ChooseLevelScene::ChooseLevelScene(QWidget *parent)
    : QMainWindow{parent}
{
    setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setFixedSize(320 , 588);
    setWindowTitle("选择关卡");


    QMenuBar *bar = menuBar();
    setMenuBar(bar);
    QMenu *startbar = bar->addMenu("开始");
    QAction * quitAction = startbar->addAction("退出");

    connect(quitAction , &QAction::triggered,[=](){
        this->close();
    });

    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 , &QPushButton::clicked,[=](){
        QTimer::singleShot(200,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 , &QPushButton::clicked,[=](){
            qDebug()<<"kaishiguanqia";
            play = new playscene(i+1);
            connect(play , &playscene::playSceneBack,[=](){
                this->show();
                play->hide();
            });

            this->hide();
            play->show();
        });

        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->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        label->setAttribute(Qt::WA_TransparentForMouseEvents);

    }

}


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

    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()/2 , pix.height()/2);
    painter.drawPixmap(10 ,30 , pix);

}

dataconfig.cpp

//dataconfig.cpp
#include "dataconfig.h"
#include <QDebug>
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);//把二维数组 计入到之前的 map  地图中  key =1  value = 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);
}

main.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>
#include<QPainter>
#include"mypushbutton.h"
#include<qthread.h>
#include<qtimer.h>
#include<chooselevelscene.h>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //配置主场景

    setFixedSize(320 , 588);
    setWindowIcon(QIcon(":/res/Coin0001.png"));
    setWindowTitle("翻金币主场景");
    //退出
    // connect(ui->actiontuichu , &QAction::triggered , [=](){
    //     this->close();
    // });

    MyPushButton *startbutton = new MyPushButton(":/res/MenuSceneStartButton.png");
    startbutton->setParent(this);
    startbutton->move(this->width()/2-startbutton->width()/2 , this->height()*0.7);

    ChooseLevelScene *choosesceen = new ChooseLevelScene;

    connect(choosesceen , &ChooseLevelScene::chooseSceneBack,[=](){
        this->show();
        choosesceen->hide();
    });

    connect(startbutton , &QPushButton::clicked , [=](){
        startbutton->zoom1();
        startbutton->zoom2();
        QTimer::singleShot(200 , this , [=](){
            this->hide();
            choosesceen->show();
        });

    });



}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow:: paintEvent(QPaintEvent *event)
{
    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()/2 , pix.height()/2);
    painter.drawPixmap(10 ,30 , pix);

}

void MainWindow::on_actiontuichu_triggered()
{
    qDebug()<<"退出程序";
    this->close();
}


mycoin.cpp

#include "mycoin.h"
#include<QPixmap>

MyCoin::MyCoin(QString btnImage)
{

    QPixmap pix;
    bool ret = pix.load(btnImage);
    if(ret!=true)
    {
        qDebug()<<"图片加载失败";
    }

    this->setFixedSize(QSize(pix.width() , pix.height()));

    //设置不规则样式
    this->setStyleSheet("QPushButton{border:0px;}");

    this->setIcon(pix);

    this->setIconSize(QSize(pix.width() , pix.height()));

    time1 = new QTimer();
    time2 = new QTimer();
    max = 8;
    min = 1;

    connect(time1 , &QTimer::timeout,[=](){
        QPixmap pix;
        QString str = QString(":/res/Coin000%1").arg(min++);
        pix.load(str);


        this->setFixedSize(QSize(pix.width() , pix.height()));

        //设置不规则样式
        this->setStyleSheet("QPushButton{border:0px;}");

        this->setIcon(pix);

        this->setIconSize(QSize(pix.width() , pix.height()));

        if(min>max)
        {
            min=1;
            this->isAnimation = false;
            time1->stop();
        }
    });

        connect(time2 , &QTimer::timeout,[=](){
            QPixmap pix;
            QString str = QString(":/res/Coin000%1").arg(max--);
            pix.load(str);


            this->setFixedSize(QSize(pix.width() , pix.height()));

            //设置不规则样式
            this->setStyleSheet("QPushButton{border:0px;}");

            this->setIcon(pix);

            this->setIconSize(QSize(pix.width() , pix.height()));

            if(max<min)
            {
                max=8;
                this->isAnimation = false;
                time2->stop();
            }
    });
}
void MyCoin:: mousePressEvent(QMouseEvent * e)
{
    if((this->isAnimation==true) || (this->iswin==true)  )
    {
        return;
    }
    else
    {
        QPushButton::mousePressEvent(e);
    }

 }

void MyCoin::changeFlag()
{
    if(this->flag==1)
    {
        isAnimation = true;
        time1->start(30);
        this->flag = 0;
    }
    else
    {
        isAnimation = true;
        time2->start(30);
        this->flag = 1;
    }

}

mypushbutton.cpp

#include "mypushbutton.h"
#include<QDebug>
#include<qpropertyanimation.h>

MyPushButton::MyPushButton(QString p_naormalimage , QString p_pressimage)
{

    this-> m_NormalImagePath = p_naormalimage;
    this-> m_PressImagePath = p_pressimage;

    QPixmap pix;
    bool ret = pix.load(p_naormalimage);
    if(ret!=true)
    {
        qDebug()<<"图片加载失败";
    }

    this->setFixedSize(QSize(pix.width() , pix.height()));

    //设置不规则样式
    this->setStyleSheet("QPushButton{border:0px;}");

    this->setIcon(pix);

    this->setIconSize(QSize(pix.width() , pix.height()));

}

void MyPushButton::zoom1()
{
    QPropertyAnimation *animationl = new QPropertyAnimation(this , "geometry");
    animationl->setDuration(200);
    animationl->setStartValue(QRect(this->x() , this->y() , this->width() , this->height()));
    animationl->setEndValue(QRect(this->x() , this->y()+10 , this->width() , this->height()));
    animationl->setEasingCurve(QEasingCurve::OutBounce);
    animationl->start();

}
void MyPushButton::zoom2()
{
    QPropertyAnimation *animationl = new QPropertyAnimation(this , "geometry");
    animationl->setDuration(200);
    animationl->setStartValue(QRect(this->x() , this->y()+10 , this->width() , this->height()));
    animationl->setEndValue(QRect(this->x() , this->y() , this->width() , this->height()));
    animationl->setEasingCurve(QEasingCurve::OutBounce);
    animationl->start();
}
void MyPushButton:: mousePressEvent(QMouseEvent *e)
{
    if(this->m_PressImagePath.length()!=0)
    {
        QPixmap pix;
        bool ret = pix.load(m_PressImagePath);
        if(ret!=true)
        {
            qDebug()<<"图片加载失败";
        }

        this->setFixedSize(QSize(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->m_PressImagePath.length()!=0)
    {
        QPixmap pix;
        bool ret = pix.load(m_NormalImagePath);
        if(ret!=true)
        {
            qDebug()<<"图片加载失败";
        }

        this->setFixedSize(QSize(pix.width() , pix.height()));

        //设置不规则样式
        this->setStyleSheet("QPushButton{border:0px;}");

        this->setIcon(pix);

        this->setIconSize(QSize(pix.width() , pix.height()));

    }
    return QPushButton::mouseReleaseEvent(e);
}

playscene.cpp

#include "playscene.h"
#include<QDebug>
#include<QMenuBar>
#include<QPainter>
#include"mypushbutton.h"
#include<QTimer>
#include<QLabel>
#include"mycoin.h"
#include<QDebug>
#include<QRect>
#include"dataconfig.h"
#include<QPropertyAnimation>
playscene::playscene(int levelnumber)
{
    QString str = QString("进入了第 %1 关").arg(levelnumber);
    qDebug()<<str;
    this->levelindex = levelnumber;

    //初始化场景
    QMenuBar *bar = menuBar();
    setMenuBar(bar);
    QMenu *startbar = bar->addMenu("开始");
    QAction * quitAction = startbar->addAction("退出");
    connect(quitAction , &QAction::triggered,[=](){
        this->close();
    });

    setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setFixedSize(320 , 588);
    setWindowTitle("选择关卡");

    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 , &QPushButton::clicked,[=](){
        QTimer::singleShot(200,this,[=](){
            emit this->playSceneBack();
        });
    });

    QFont font;
    font.setFamily("微软雅黑");
    font.setPointSize(20);
    QLabel *lable = new QLabel;
    lable->setParent(this);
    lable->setText("Level "+QString::number(this->levelindex));
    lable->setGeometry(30 , this->height()-50 , 120 ,50);
    lable->setFont(font);

    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 tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width() , tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tmpPix.width())/2, -tmpPix.height());


    //显示金币背景图案
    for(int i=0;i<4;++i)
    {
        for(int j=0;j<4;++j)
        {
            QLabel *lable = new QLabel;
            lable->setGeometry(0,0,50,50);
            lable->setPixmap(QPixmap(":/res/BoardNode.png"));
            lable->setParent(this);
            lable->move(57+i*50 , 200+j*50);

            QString str_tep;
            if(this->gameArray[i][j]==1)
            {
                str_tep = ":/res/Coin0001.png";

            }
            else
            {
                str_tep = ":/res/Coin0008.png";
            }
            MyCoin *btn = new MyCoin(str_tep);
            btn->setGeometry(0,0,50,50);
            btn->setParent(this);
            btn->move(59+i*50 , 204+j*50);

            btn->posX = i;
            btn->posY = j;
            btn->flag = this->gameArray[i][j];
            coinBtn[i][j] = btn;


            connect(btn , &QPushButton::clicked,[=](){
                btn->changeFlag();
                this->gameArray[i][j] = this->gameArray[i][j]==0 ? 1:0;

                //开始翻转周围
                QTimer::singleShot(100,this , [=](){
                    //右侧
                    if(btn->posX+1<=3)
                    {
                        this->coinBtn[btn->posX+1][j]->changeFlag();
                        this->gameArray[btn->posX+1][j] = this->gameArray[btn->posX+1][j]==0 ? 1:0;
                    }
                    //左侧
                    if(btn->posX-1>=0)
                    {
                        this->coinBtn[btn->posX-1][j]->changeFlag();
                        this->gameArray[btn->posX-1][j] = this->gameArray[btn->posX-1][j]==0 ? 1:0;
                    }
                    //下
                    if(btn->posY+1<=3)
                    {
                        this->coinBtn[btn->posX][btn->posY+1]->changeFlag();
                        this->gameArray[btn->posX][btn->posY+1] = this->gameArray[btn->posX][btn->posY+1]==0 ? 1:0;
                    }
                    //上
                    if(btn->posY-1>=0)
                    {
                        this->coinBtn[btn->posX][btn->posY-1]->changeFlag();
                        this->gameArray[btn->posX][btn->posY-1] = this->gameArray[btn->posX][btn->posY-1]==0 ? 1:0;
                    }
                    //开始判断是否胜利
                    if(true == isWin())
                    {
                        qDebug()<<"shengli";
                        SetWin();
                        //开始显示胜利图片
                        QPropertyAnimation *animation1 = new QPropertyAnimation(winLabel , "geometry");
                        animation1->setDuration(500);
                        animation1->setStartValue(QRect(winLabel->x() , winLabel->y() ,
                                                        winLabel->width() , winLabel->height()));
                        animation1->setEndValue(QRect(winLabel->x() , winLabel->y()+114 ,
                                                      winLabel->width() , winLabel->height()));
                        animation1->setEasingCurve(QEasingCurve::OutBounce);
                        animation1->start();
                    }


                });

            });
        }
    }
}

bool playscene:: isWin()
{
    for(int i=0;i<4;++i)
    {
        for(int j=0;j<4;++j)
        {
            if(coinBtn[i][j]->flag == 0)
            {
                return false;
            }
        }
    }
    return true;

}

bool playscene:: SetWin()
{
    for(int i=0;i<4;++i)
    {
        for(int j=0;j<4;++j)
        {
            coinBtn[i][j]->iswin = true;
        }
    }
    return true;

}


void playscene:: paintEvent(QPaintEvent *event)
{
    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()/2 , pix.height()/2);
    painter.drawPixmap(10 ,30 , pix);

}

QT翻金币游戏 声明:QT翻金币游戏代码并非本人所写,主要通过学习腾讯课堂视频之后所写。代码中间略有不同。 我在这里提供一下程序所需图片资源与背景音乐资源:链接:https://pan.baidu.com/s/1XIUyXumqQ7_jBlB7q_YpHA 提取码:8hyq 视频教程没有这个,你自己没法做。 下面我给一下自己的源码,我建议还是自己按照视频理解每个函数的作用敲一遍 源码:https://pan.baidu.com/s/1xtaTcPELxdE59UWOCDFvwQ 提取码:lmkq 这个游戏里面我完成了一下 阅读详情

相关推荐

qt 实现翻金币游戏

游戏玩法介绍: 游戏设置关卡二十关,通过选关界面可以选择进入到对应的关卡中。 进入对应关卡之后,点击任意金币,可以使该硬币以及周边(上,下,左, 右)金边翻转。如果硬币都翻转为金币,则游戏胜利。 游戏界面设置: 开始界面: 开始场景中需要自定义一个按钮,点击开始按钮可以实现一个动图弹跳的显示效果。 同时弹出选关窗口,关闭当前窗口。 对于我这个新手来说,这一部分实现的难点是按钮的弹跳效果的实现。 对应代码: #include "mainScene.h" #include "mybutton.h" #inc

GG802312的博客 2809

qt练手项目->翻金币游戏设计思路和总结

游戏场景相对复杂一些,这里对于游戏场景的设计,有几个部分,首先最上方图标和文字的显示,让后图片coinFile的显示,还有关卡level的显示,还有back按钮的显示,最重要的是中间的金币银币的显示,和翻转问题的显示,这里我们创建了一个类,play的游戏场景的一个类。最后就是按钮的设计了,这里我们创建了一个按钮类,用于和其他场景的一个互动,点击切换场景的效果,同时我们关联了槽函数,用于点击以后关闭本场景跳转到下一个场景去,这里我们隐藏自身的场景,显示关卡场景,并且用到了定时器去延时进入下一个关卡去。

m0_58168670的博客 853

Qt 翻金币源代码及其设计流程

使用Qt(C++)实现翻金币小游戏的设计,项目含金币翻转特效,按钮点击音效及其相关关卡资源,翻金币项目是Qt学习的一个较好的学习项目,对于初学者能够有较大的提升。

使用Qt实现翻金币游戏的示例代码

使用qt 实现一个简单的翻金币游戏

csdnwu2020的博客 473

QT学习之翻金币源代码及参考资料

挺好玩的一个QT小游戏,用到了不少QT基础知识,对于学习QT很有帮助

qt翻金币的代码和教程

qt翻金币的代码和教程

Qt—翻金币小游戏 (附源码和资源文件)

Qt实现翻金币小游戏。

qq_19693355的博客 2961

基于QT的翻金币小游戏

最近为了学习C++和qt5,跟着教程写了一个翻金币的小游戏 3、程序代码 3.1 项目工程配置文件 CoinFlip.pro 3.2 界面文件 mainscene.ui 和 res.qrc 资源添加 3.3 自定义按钮类 mypushbutton.h 和 mypushbutton.cpp 3.4 金币类 mycoin.h 和 mycoin.cpp 3.5 关卡数据类 dataconfig.h 和 dataconfig.cpp 3.6 首页主界面 mainscene.h 和 mainscene.cpp 3.7 选择关卡界面 chooselevelscene.h 和 chooselevelscene.h 3.8 翻金币界面 playscene.h 和 playscene.cpp 3.9 主函数文件 main.cpp

翻金币小游戏(Qt)

开始按钮点击后有弹跳效果,这个效果是我们利用自定义控件实现的(QPushButton不会自带这类特效),我们可以自己封装出一个按钮控件,来实现这些效果。创建MyPushButton,继承与QPushButton点击完成。修改MyPushButton的父类。

热爱编程小枫的博客 2949

QT翻金币游戏.rar

本游戏分3个界面,开始,菜单,关卡,场景,自定义按钮的特效,动画的曲线设计,窗口来回切换,20关卡的设计,界面美观。

基于QT实现的翻金币游戏源码+项目说明.zip

【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于QT实现的翻金币游戏源码+项目说明.zip基于QT实现的翻金币游戏源码+项目说明.zip 基于QT实现的翻金币游戏源码+项目说明.zip基于QT实现的翻金币游戏源码+项目说明.zip 基于QT实现的翻金币游戏源码+项目说明.zip基于QT实现的翻金币游戏源码+项目说明.zip 基于QT实现的翻金币游戏源码+项目说明.zip基于QT实现的翻金币游戏源码+项目说明.zip 基于QT实现的翻金币游戏源码+项目说明.zip基于QT实现的翻金币游戏源码+项目说明.zip 基于QT实现的翻金币游戏源码+项目说明.zip基于QT实现的翻金币游戏源码+项目说明.zip 基于QT实现的翻金币游戏源码+项目说明.zip基于QT实现的翻金币游戏源码+项目说明.zip 基于QT实现的翻金币游戏源码+项目说明.zip基于QT实现的翻金币游戏源码+项目说

QT实战之翻金币游戏【详细过程及介绍】

本文将从头到尾循序渐进地对“CoinFlip”游戏的实现进行介绍,会相对详细一点,主要从整体分析、项目创建、项目实现、项目打包这几个方面进行介绍。

m0_59800431的博客 3936

期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码.zip

期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码期末大作业课程设计基于C++的Qt框架开发的翻金币小游戏源码

Qt项目翻金币教程

QT项目翻金币教程

weixin_41560737的博客 2640

PVE 9.0 保姆级安装及优化教程(换源、网络配置、远程唤醒等)【基础篇】

最近,Proxmox VE 推出了基于 Debian 13 的 9.0 版本。正巧我手头有一台闲置的老电脑,与其闲置,不如装上 PVE,让它化身 All in One,继续发挥它的余热。G4560 算是一颗 NAS 神 U,十几元的售价就能有双核四线程,比起什么 J1900 不知道强到哪里去了,尽管定位上是奔腾处理器,但性能甚至能和同代的酷睿 i3-7100 掰掰手腕,并且它还自带 HD 610 的核显,对于解码的支持也还不错。

zc_mk的博客 7534

AI Agent辅助企业财务分析与预测

企业财务分析与预测是企业管理中至关重要的环节,准确的财务分析与预测能够帮助企业管理者做出明智的决策,合理规划资源,降低风险。然而,传统的财务分析与预测方法往往依赖于人工经验和简单的统计模型,存在效率低、准确性差等问题。随着人工智能技术的飞速发展,AI Agent作为一种智能的软件实体,能够自主地感知环境、做出决策并采取行动,为企业财务分析与预测带来了新的解决方案。

AI天才研究院 927

基于QT写的一个翻金币小游戏

基于QT写的一个翻金币小游戏基于QT写的一个翻金币小游戏 基于QT写的一个翻金币小游戏 这是我参考创智学院的教程做的一个小游戏,素材均来自于创智学院。因为我是做嵌入式有快一年了,之前接触的东西都有限,想玩一玩QT,提升一下自己的知识面,觉得这个游戏作为QT入门还是不错,所以将这个项目总结一下。 本来很久之前就决定将自己在入门嵌入式的遇到的一些问题总结出来,总是因为没有时间。以后会陆陆续续的分享一...

qq_37200742的博客 3646

C#实战:如何用HslCommunication快速搞定三菱FX5U PLC通讯(附完整代码)

本文详细介绍了如何利用HslCommunication库在C#环境中快速实现与三菱FX5U PLC的通讯。通过实战代码演示,从环境搭建、建立连接、核心数据读写到构建健壮的监控服务,系统讲解了如何高效完成数据采集与交互,大幅提升工业自动化项目的开发效率。

s4t5u6v的博客 1208

qt实战-翻金币游戏

qt学习之翻金币游戏

qq_46066548的博客 3027

如何正确理解和获取S参数

差分四端口的S参数是由双端口S参数逐步演化而来,因此,在数学中,其各分量均可以从单端S参数推导而来,在实际的仿真和测试中,软件和测量设备也是通过获取单端S参数矩阵再进行运算得到混合S参数矩阵的,并非直接获取。

一只豌豆象的博客 5162

【数据集】中国大气污染物数据集CHAP(1km空间分辨率)-韦晶团队

中国大气污染物数据集CHAP(1km空间分辨率)-韦晶团队

WW、forever的博客 2631

【QT】翻金币项目(QT综合案例)

目录 1. 项目简介 2、程序框架 3、程序代码 3.1 界面文件 mainscene.ui 3.2 res.qrc 资源添加 3.3 自定义按钮类mypushbutton.h 和 mypushbutton.cpp 3.4 金币类 mycoin.h 和mycoin.cpp 3.5 关卡数据类dataconfig.h 和 dataconfig.cpp 3.6 首页主界面 mainscene.h 和mainscene.cpp 3.7 选择关卡界面chooselevelsce...

风浪云HH的博客 6136

沁恒CH32V208GBU6读写芯片内部FLASH:代码空间;使用OTA时候的代码空间;保存结构体到FLASH

沁恒CH32V208GBU6读写芯片内部FLASH:代码空间;使用OTA时候的代码空间;保存结构体到FLASH

cheng___yi的博客 557

QT学习之翻金币案例

完整源代码及资源文件: 首先创建一个QMainWindow项目,然后添加以下代码即可(以下为部分源代码) 开发平台:ubuntu16.04 QT版本:QT5.14.0 GCC:gcc-5.3.1 (需支持C++11) 1、游戏主场景 mainscene.h #ifndef MAINSCENE_H #define MAINSCENE_H #include <QMainWindow> #include "chooselevelscene.h" QT_BEGIN_NAMESPACE namesp

阿乐的博客 5387

【ComfyUI专栏】ComfyUI 常用工作流下载与分享网址

首先我们来分享第一个网站,Openart 网站,这个网站同时也能提供比较多的其他类型。但是如果希望下载工作流,我们可以访问到如下的这个站点。作为ComfyUI的学习用户来说,我们通常需要借助他山之石来帮助我们快速成长,因此我们需要找到能够分享和下载的网址。,当前网站是综合性网站,在网站中可以下载各种不同的类型,其中工作流只是可以下载的非常小的部分。这个网站也可以浏览不同的工作流,这个工作流的地址和访问如下的站点。

技术与管理的平衡木- 7884

【JTAG】1149.6协议详解

JTAG1149.6协议是1149.1协议中Boundary-Scan结构的扩展,1149.1对交流耦合、差分或两者兼有的情况并没有得到充分的处理。该协议支持新的I/O测试结构。需要时变(AC)信号的高级I/O引脚允许测试它们之间的连接,这包括所有差分信号引脚或单端信号引脚,通过设计支持交流耦合。..................

xuhe0206的博客 4590
上一篇: QT工具栏开始,退出
下一篇: MFC结合GDI+
越甲八千
博客等级 码龄11年 2972粉丝 553原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值