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_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic: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 = vint 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);}