主界面
#ifndef MYMAINWINDOW_H #define MYMAINWINDOW_H #includeQT_BEGIN_NAMESPACE namespace Ui { class MyMainWindow; } QT_END_NAMESPACE class MyMainWindow : public QMainWindow { Q_OBJECT public: MyMainWindow(QWidget *parent = nullptr); ~MyMainWindow(); protected: void paintEvent(QPaintEvent *e); private: Ui::MyMainWindow *ui; }; #endif // MYMAINWINDOW_H
开始场景
#ifndef STARTSCENE_H #define STARTSCENE_H #include#include #include #include "selectscene.h" class StartScene : public MyMainWindow { Q_OBJECT public: explicit StartScene(QWidget *parent = nullptr); protected: signals: private: SelectScene mySelectScene; }; #endif // STARTSCENE_H
选择关卡场景
#ifndef SELECTSCENE_H #define SELECTSCENE_H #include#include "mymainwindow.h" class SelectScene : public MyMainWindow { Q_OBJECT public: explicit SelectScene(QWidget *parent = nullptr); protected: void paintEvent(QPaintEvent *e) override; signals: void backBtnClicked(); }; #endif // SELECTSCENE_H
游戏界面
#ifndef PLAYSCENE_H #define PLAYSCENE_H #include#include "mymainwindow.h" #include "coinpushbutton.h" class PlayScene : public MyMainWindow { Q_OBJECT public: explicit PlayScene(int level,QWidget *parent = nullptr); protected: void paintEvent(QPaintEvent *e) override; void filp(int row ,int col); void judgeWin(); signals: void backBtnClicked(); private: CoinPushButton *mCoins[4][4]; bool winStat; }; #endif // PLAYSCENE_H
重写按钮
#ifndef MYPUSHBUTTON_H #define MYPUSHBUTTON_H #include#include #include class MyPushButton : public QPushButton { Q_OBJECT public: explicit MyPushButton(QWidget *parent = nullptr); explicit MyPushButton(QString normalImage,QString pressedImage,QWidget *parent = nullptr); void moveUp(); void moveDown(); enum PushButtonStat{ Normal, Press }; protected: void paintEvent(QPaintEvent *) override; void mousePressEvent(QMouseEvent *e) override; void mouseReleaseEvent(QMouseEvent *e) override; signals: private: QString mNormalImage; QString mPressedImage; PushButtonStat pushButtonStat; }; #endif // MYPUSHBUTTON_H
重写金币按钮
#ifndef COINPUSHBUTTON_H #define COINPUSHBUTTON_H #include#include #include class CoinPushButton : public QPushButton { Q_OBJECT public: explicit CoinPushButton(QWidget *parent = nullptr); int getMstat() const; void setMstat(int value); void setMstatWithAnimattion(int stat); void filp(); signals: protected: void paintEvent(QPaintEvent *) override; private: int mstat; int frame; QTimer mtimer; }; #endif // COINPUSHBUTTON_H
游戏数据
#ifndef DATACONFIG_H #define DATACONFIG_H #includecpp文件#include #include class dataConfig : public QObject { Q_OBJECT public: explicit dataConfig(QObject *parent = 0); public: QMap > >mData; signals: public slots: }; #endif // DATACONFIG_H
主界面
#include "mymainwindow.h" #include#include "startscene.h" #include "selectscene.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); // MyMainWindow w; // w.show(); StartScene st; st.show(); // SelectScene sc; // sc.show(); return a.exec(); }
开始场景
#include "startscene.h" #include#include "mypushbutton.h" #include #include StartScene::StartScene(QWidget *parent) : MyMainWindow(parent) { this->setWindowTitle("开始场景"); MyPushButton *btnStart= new MyPushButton(":/res/MenuSceneStartButton.png",":/res/MenuSceneStartButton.png",this); btnStart->setFixedSize(114,114); btnStart->move((this->width()-btnStart->width())/2,(this->height()*3/4-btnStart->height()/2)); connect(btnStart,&MyPushButton::clicked,[=](){ QSound::play(":/res/TapButtonSound.wav"); btnStart->setEnabled(false); btnStart->moveDown(); QTimer::singleShot(150,[=](){ btnStart->moveUp(); }); QTimer::singleShot(300,[=](){ btnStart->setEnabled(true); this->hide(); this->mySelectScene.show(); this->mySelectScene.move(this->pos()); }); }); connect(&this->mySelectScene,&SelectScene::backBtnClicked,[=](){ QSound::play(":/res/BackButtonSound.wav"); this->show(); this->move(this->mySelectScene.pos()); this->mySelectScene.hide(); }); }
选择关卡场景
#include "selectscene.h" #include#include "mypushbutton.h" #include #include #include "playscene.h" #include SelectScene::SelectScene(QWidget *parent) : MyMainWindow(parent) { this->setWindowTitle("选择关卡"); MyPushButton *btnBack = new MyPushButton(":/res/BackButtonSelected.png",":/res/BackButton.png",this); btnBack->resize(72,32); btnBack->move(this->width() - btnBack->width(),this->height() - btnBack->height()); connect(btnBack,&QPushButton::clicked,this,&SelectScene::backBtnClicked); //关卡按钮的实现 const int colWidth= 70; const int rowHeight =70; const int xOffset =25; const int yOffset =130; for(int i=0;i<20;i++) { int row=i/4; int col =i%4; int x=col*colWidth+xOffset; int y=row*rowHeight+yOffset; MyPushButton *btns = new MyPushButton(":/res/LevelIcon.png",":/res/LevelIcon.png",this); btns->setText(QString::number(i+1)); btns->resize(57,57); btns->move(x,y); connect(btns,&MyPushButton::clicked,[=](){ QSound::play(":/res/TapButtonSound.wav"); PlayScene *playScene = new PlayScene(i+1,this); playScene->setAttribute(Qt::WA_DeleteOnClose); this->hide(); playScene->show(); playScene->move(this->pos()); connect(playScene,&PlayScene::backBtnClicked,[=](){ QSound::play(":/res/BackButtonSound.wav"); this->show(); this->move(playScene->pos()); playScene->close(); }); }); } } void SelectScene::paintEvent(QPaintEvent *e) { QPainter painter(this); painter.translate(0,this->menuBar()->height()); QPixmap pix(":/res/OtherSceneBg.png"); painter.drawPixmap(0,0,this->width(),this->height(),pix); pix.load(":/res/Title.png"); painter.drawPixmap(0,0,pix); }
游戏界面
#include "playscene.h" #include "mypushbutton.h" #include#include #include #include "coinpushbutton.h" #include "dataconfig.h" #include #include #include #include #include #include PlayScene::PlayScene(int level,QWidget *parent) : MyMainWindow(parent) { this->winStat=false; this->setWindowTitle("开始游戏"); MyPushButton *btnBack = new MyPushButton(":/res/BackButtonSelected.png",":/res/BackButton.png",this); btnBack->resize(72,32); btnBack->move(this->width() - btnBack->width(),this->height() - btnBack->height()); connect(btnBack,&QPushButton::clicked,this,&PlayScene::backBtnClicked); QLabel *lable = new QLabel(this); lable->resize(150,50); lable->setText(QString("LEVEL:%1").arg(level)); lable->setFont(QFont("华文新魏",20)); lable->move(30,this->height()-lable->height()); dataConfig data; QVector > dataArray=data.mData[level]; const int colWith=50; const int rowHeight=50; const int xOffset=57; const int yOffset=200; for(int i=0;i<16;i++) { int row =i/4; int col=i%4; int x=col*colWith+xOffset; int y=row*rowHeight+yOffset; CoinPushButton *coinPushButton = new CoinPushButton(this); mCoins[row][col]=coinPushButton; coinPushButton->move(x,y); //从dataconfig中获取数据 coinPushButton->setMstat(dataArray[row][col]); connect(coinPushButton,&CoinPushButton::clicked,[=](){ // coinPushButton->filp(); if(!this->winStat)this->filp(row,col); }); } } void PlayScene::paintEvent(QPaintEvent *e) { QPainter painter(this); painter.translate(0,this->menuBar()->height()); 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(0,0,pix); } void PlayScene::filp(int row, int col) { QSound::play(":/res/ConFlipSound.wav"); this->mCoins[row][col]->filp(); QTimer::singleShot(240,[=](){ if(row+1<4)this->mCoins[row+1][col]->filp(); if(row-1>=0)this->mCoins[row-1][col]->filp(); if(col+1<4)this->mCoins[row][col+1]->filp(); if(col-1>=0)this->mCoins[row][col-1]->filp(); this->judgeWin(); }); } void PlayScene::judgeWin() { for(int i= 0;i<4;++i) { for(int j =0;j<4;++j) { // qDebug()<< QString("记录"); qDebug()<< QString("%1 %2 %3").arg(this->mCoins[i][j]->getMstat()).arg(i).arg(j); if(!this->mCoins[i][j]->getMstat()) { return ; } // } } // QMessageBox::information(this,"恭喜",""); // qDebug()<< QString("你已经胜利了"); this->winStat=true; QSound::play(":/res/LevelWinSound.wav"); // qDebug()<< QString("%1").arg(this->winStat); QLabel *labelWin1 = new QLabel(this); QPixmap pix =QPixmap(":/res/LevelCompletedDialogBg.png"); labelWin1->setPixmap(pix); labelWin1->resize(pix.size()); labelWin1->show(); labelWin1->move((this->width()-labelWin1->width())/2,-labelWin1->height()); QPropertyAnimation *animation = new QPropertyAnimation(labelWin1,"geometry",this); animation->setStartValue(labelWin1->geometry()); animation->setEndValue(QRect(labelWin1->x(),labelWin1->y()+100,labelWin1->width(),labelWin1->height())); animation->setDuration(1000); animation->setEasingCurve(QEasingCurve::OutBounce); animation->start(QAbstractAnimation::DeleteWhenStopped); }
重写按钮
#include "mypushbutton.h" #include#include #include MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent) { } MyPushButton::MyPushButton(QString normalImage, QString pressedImage, QWidget *parent):QPushButton(parent),mNormalImage(normalImage),mPressedImage(pressedImage),pushButtonStat(Normal) { } void MyPushButton::moveUp() { QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry",this); animation->setStartValue(this->geometry()); animation->setEndValue(QRect(this->x(),this->y()-10,this->width(),this->height())); animation->setDuration(100); animation->start(QAbstractAnimation::DeleteWhenStopped); } void MyPushButton::moveDown() { QPropertyAnimation *animation = new QPropertyAnimation(this,"geometry",this); animation->setStartValue(this->geometry()); animation->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height())); animation->setDuration(100); animation->start(QAbstractAnimation::DeleteWhenStopped); } void MyPushButton::paintEvent(QPaintEvent *) { QPainter painter(this); if(this->pushButtonStat==Normal) { QPixmap pix(mNormalImage); } if(this->pushButtonStat==Press) { QPixmap pix(mPressedImage); } QPixmap pix(mNormalImage); painter.drawPixmap(0,0,this->width(),this->height(),pix); painter.drawText(0,0,this->width(),this->height(),Qt::AlignHCenter|Qt::AlignVCenter,this->text()); } void MyPushButton::mousePressEvent(QMouseEvent *e) { this->pushButtonStat=Press; update(); QPushButton::mousePressEvent(e); } void MyPushButton::mouseReleaseEvent(QMouseEvent *e) { this->pushButtonStat=Normal; update(); QPushButton::mouseReleaseEvent(e); }
重写金币按钮
#include "coinpushbutton.h" #include#include #include CoinPushButton::CoinPushButton(QWidget *parent) : QPushButton(parent) { this->resize(50,50); this->setMstat(0); this->setStyleSheet("QPushButton{border:0px}"); connect(&this->mtimer,&QTimer::timeout,[=](){ if(this->mstat) { this->frame--; } else { this->frame++; } // qDebug() << QString("%1").arg(this->frame); QString frameName = QString(":/res/Coin000%1.png").arg(this->frame); this->setIcon(QIcon(frameName)); if(this->frame==8||this->frame==1){ this->mtimer.stop(); } }); } void CoinPushButton::paintEvent(QPaintEvent *e) { QPainter painter(this); QPixmap pix; pix.load(":/res/BoardNode.png"); painter.drawPixmap(0,0,this->width(),this->height(),pix); QPushButton::paintEvent(e); } int CoinPushButton::getMstat() const { return mstat; } void CoinPushButton::setMstat(int value) { this->mstat = value; if(this->mstat) { this->setIcon(QIcon(":/res/Coin0001.png")); } else { this->setIcon(QIcon(":/res/Coin0008.png")); } this->setIconSize(this->size()); } void CoinPushButton::setMstatWithAnimattion(int stat) { this->mstat = stat; qDebug()<< QString("mstat%1").arg(this->getMstat()); if(this->mstat){ //由银币变化金币 this->frame=8; } else { this->frame=1; } this->mtimer.start(30); } void CoinPushButton::filp() { // this->setMstat(!this->getMstat()); this->setMstatWithAnimattion(!this->getMstat()); }
游戏数据
#include "dataconfig.h" #includedataConfig::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 > v; for(int i = 0 ; i < 4;i++) { QVector 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 v1; for(int j = 0 ; j < 4;j++) { v1.push_back(array20[i][j]); } v.push_back(v1); } mData.insert(20,v); //测试数据 // for( QMap > >::iterator it = mData.begin();it != mData.end();it++ ) // { // for(QVector< QVector >::iterator it2 = (*it).begin(); it2!= (*it).end();it2++) // { // for(QVector ::iterator it3 = (*it2).begin(); it3 != (*it2).end(); it3++ ) // { // qDebug() << *it3 ; // } // } // qDebug() << endl; // } }



