QT学习笔记(下)

news2024/9/28 3:25:42

项目实践

前言

本项目的实践主要是以翻金币的项目为例,进行QT项目的实践。游戏分为3个场景,分别是mainscene主场景、chooselevelscene选择关卡场景、playscene游戏场景,以上的三个场景是按顺序实现的。并且定义了两个自定义的QPushButton按钮,分别是mypushbutton,以及mycoin,并且加入了游戏的配置类,dataconfig。以上是本项目中所有的文件,以下会逐一进行介绍。
在这里插入图片描述

1. mainscene

mainscene是游戏的主场景,其由一个开始按钮组成。其背景主要是通过重写paintEvent进行背景的绘制。按钮的弹跳效果则是根据mypushbuttion里面的zoom1,zoom2方法利用QPropertyAnimation来实现的。

mainscene.h

#ifndef MAINSCENE_H
#define MAINSCENE_H

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

QT_BEGIN_NAMESPACE
namespace Ui { class MainScene; }
QT_END_NAMESPACE

class MainScene : public QMainWindow
{
    Q_OBJECT

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

    void paintEvent(QPaintEvent *);

    // 选关的窗口
    ChooseLevelScene * chooseScene = NULL;

private:
    Ui::MainScene *ui;
};
#endif // MAINSCENE_H

mainscene.cpp

#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include "mypushbutton.h"
#include <QPushButton>
#include <QDebug>
#include <QTimer>
//#include <QSoundEffect>

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();
    });

    // 准备开始按钮音效
//    QSoundEffect * startSound = new QSoundEffect;
//    startSound->setSource(QUrl("qrc:/res/TapButtonSound.wav"));


    //开始按钮
    MyPushButton *startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(this->width()*0.5-startBtn->width()*0.5, this->height()*0.7);

    // 实例化选择关卡场景
    chooseScene = new ChooseLevelScene;
    // 监听选择关卡返回信号
    connect(chooseScene, &ChooseLevelScene::chooseSceneBack, this, [=](){
       this->setGeometry(chooseScene->geometry());
       chooseScene->hide();
       this->show();
    });

    connect(startBtn, &QPushButton::clicked, [=](){
        qDebug() << "点击了开始按钮";
        // 音效
//        startSound->play();
        startBtn->zoom1();
        startBtn->zoom2();
        // 过了500ms执行下面匿名函数,sleep的作用
        QTimer::singleShot(500, this, [=](){
            // 切换窗口时候使得本窗口和切换的窗口位置一致
            chooseScene->setGeometry(this->geometry());
            this->hide();
            chooseScene->show();
        });
    });

}

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


void MainScene::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);

};


2. mypushbutton

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

mypushbutton里面是重写了QPushButton,分别用于构造开始按钮、返回按钮和选择关卡按钮。通过构造函数来判断是否是属于返回按钮(如果传入按压,和释放的两张图就是返回)。返回通过重新鼠标事件来实现。开始按钮通过zoom1、zoom2两个类方法来实现。

mypushbutton.h

#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H

#include <QPushButton>

class MyPushButton : public QPushButton
{
    Q_OBJECT
public:
//    explicit MyPushButton(QWidget *parent = nullptr);
    MyPushButton(QString normalImg, QString pressImg="");
    QString normalImgPath;
    QString pressImgPath;

    // 弹跳特效
    void zoom1(); // 向下跳
    void zoom2(); // 向上跳

    // 重写按下和释放事件
    void mousePressEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);


signals:

};

#endif // MYPUSHBUTTON_H

mypushbutton.cpp

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

//MyPushButton::MyPushButton(QWidget *parent)
//    : QPushButton{parent}
//{

//}

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);
    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);
    // 按钮属性的位置不会随着动画位置的更新而更新
    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(this->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(this->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);
}

3. chooselevelscene

chooselevelscene首先也是通过重写paintEevent来绘制背景图,已经左上角的logo。其中选择关卡的按钮也是mypushbutton创建而来。通过循环创建按钮。然后在按钮上面加入QLabel来表示关卡的数目。由于QLabel是覆盖在QPushButton上面,所以就需要使用WA_TransparentForMouseEvents点击穿透来通过点击QLabel传递到QPushButton中。
在这里插入图片描述

3.1 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 *);

    // 游戏场景对象指针
    PlayScene * play = NULL;

signals:
    // 返回信号
    void chooseSceneBack();

};

#endif // CHOOSELEVELSCENE_H

3.2 chooselevelscene.cpp

#include "chooselevelscene.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include <QLabel>
//#include <QSoundEffect>

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();
    });

//    QSoundEffect * chooseSound = new QSoundEffect;
//    chooseSound->setSource(QUrl("qrc:/res/TapButtonSound.wav"));

//    QSoundEffect * backSound = new QSoundEffect;
//    chooseSound->setSource(QUrl("qrc:/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, &QPushButton::clicked, [=](){
//        qDebug() << "点击了返回按钮";
//        backSound->play();
        QTimer::singleShot(500, this, [=](){emit this->chooseSceneBack();});
    });


    // 创建选择关卡的按钮
    for (int i=0; i<20; ++i){
        MyPushButton * menuBth = new MyPushButton(":/res/LevelIcon.png");
        menuBth->setParent(this);
        menuBth->move(25+i%4*70, 130+i/4*70);

        connect(menuBth, &QPushButton::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::playSceneBack, [=](){
                this->setGeometry(play->geometry());
                this->show();
                delete play;
                play = NULL;
            });
        });


        QLabel *label = new QLabel;
        label->move(25+i%4*70, 130+i/4*70);
        label->setParent(this);
        label->setFixedSize(menuBth->width(), menuBth->height());
        label->setText(QString::number(i+1));
        // 设置水平居中和垂直居中
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        // 设置事件穿透,设置完成以后,btn也可以收到点击信号了
        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);

};

4. playscene游戏主场景

游戏主场景是由金币组成的通过加载dataconfig来初始化金币的状态。通过timer计时器来控制金币的翻转等。
在这里插入图片描述

4.1 playscene.h

#ifndef PLAYSCENE_H
#define PLAYSCENE_H

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

class PlayScene : public QMainWindow
{
    Q_OBJECT
public:
//    explicit PlayScene(QWidget *parent = nullptr);
    PlayScene(int levelNum);
    int levelIndex; // 内部成员属性

    // 重写paintEvent事件
    void paintEvent(QPaintEvent *);

    // 二维数据维护每关的具体数据
    int gameArray[4][4];

    MyCoin *coinBtn[4][4];

    // 是否是胜利的标志
    bool isWin;

signals:
    // 返回信号
    void playSceneBack();

};

#endif // PLAYSCENE_H


playscene.cpp

#include "playscene.h"
#include<QDebug>
#include <QMenuBar>
#include<QPainter>
#include<mypushbutton.h>
#include<QTimer>
#include<QLabel>
#include<QFont>
#include "mycoin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>
//#include <QSoundEffect>

//PlayScene::PlayScene(QWidget *parent)
//    : QMainWindow{parent}
//{

//}


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();
    });
    // 音效
//    QSoundEffect * backSound = new QSoundEffect;
//    backSound->setSource(QUrl("qrc:/res/TapButtonSound.wav"));

//    QSoundEffect * flipSound = new QSoundEffect;
//    flipSound->setSource(QUrl("qrc:/res/ConFlipSound.wav"));

//    QSoundEffect * winSound = new QSoundEffect;
//    winSound->setSource(QUrl("qrc:/res/LevelWinSound.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, &QPushButton::clicked, [=](){
//        qDebug() << "点击了返回按钮";
//        backSound->play();
        QTimer::singleShot(500, this, [=](){emit this->playSceneBack();});
    });

    QLabel *label = new 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);
    label->setGeometry(30, this->height()-50, 120, 50);

    // 初始化关卡的信息
    dataConfig config;
    for (int i=0; i<4; ++i){
        for (int j=0; j<4; ++j){
            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())*0.5, -tmpPix.height());

    // 创建金币背景图片
    for(int i=0; i<4; i++){
        for(int j=0; j<4; j++){
            QLabel *label = new QLabel;
            QPixmap pix = QPixmap(":/res/BoardNode.png");
            label->setGeometry(0, 0, pix.width(), pix.height());
            label->setPixmap(QPixmap(":/res/BoardNode.png"));
            label->setParent(this);
            label->move(57+i*50, 200+j*50);

            QString path;
            if (gameArray[i][j]==1){
                path = ":/res/Coin0001.png";
            }else{
                path = ":/res/Coin0008.png";
            }

            // 创建金币
            MyCoin *coin = new MyCoin(path);
            coin->setParent(this);
            coin->move(59+i*50, 204+j*50);

            // 给金币的属性赋值
            coin->posX = i;
            coin->posY = j;
            coin->flag = this->gameArray[i][j];

            // 将金币放入二维数据以便维护
            coinBtn[i][j] = coin;

            // 点击金币,进行翻转
            connect(coin, &MyCoin::clicked, [=](){
//                flipSound->play();
                // 禁用所有金币,防止同时点2个
                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){
                        this->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){
                        this->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 >=0){
                        this->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 <= 3){
                        this->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 (this->coinBtn[i][j]->flag==false){
                                this->isWin = false;
                                break;
                            }
                        }
                    }
                    if (isWin == true){
//                        winSound->play();
                        qDebug() << "胜利了";
                        // 将每个按钮的胜利标改成true,如果再次点击按钮,直接return,不作响应
                        for (int i=0; i<4; ++i){
                            for (int j=0; j<4; ++j){
                                this->coinBtn[i][j]->isWin = true;
                            }
                        }
                        // 将胜利图片移动下来
                        QPropertyAnimation *animation = new QPropertyAnimation(winLabel, "geometry");
                        // 设置时间
                        animation->setDuration(1000);
                        // 设置开始位置
                        animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
                        // 设置结束位置
                        animation->setEndValue(QRect(winLabel->x(), winLabel->y()+114, 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);
    // logo创建
    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);

}

5. mycoin

mycoin继承QPushButton用于创建金币。

mycoin.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 btnImg);

    // 金币属性
    int posX;    // y坐标
    int posY;   // x坐标
    bool flag; // 正反

    // 改变标志的方法
    void changeFlag();
    QTimer *timer1;  // 正面反面的定时器
    QTimer *timer2;  // 反面到正面
    int min = 1;
    int max = 8;

    // 执行动画的标志
    bool isAnimation = false;

    // 重新按钮的鼠标按下事件
    void mousePressEvent(QMouseEvent *e);

    // 是否胜利的标志
    bool isWin = false;


signals:

};

#endif // MYCOIN_H

mycoin.cpp

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

//MyCoin::MyCoin(QWidget *parent)
//    : QPushButton{parent}
//{

//}

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.size());
       this->setStyleSheet("QPushButton{border: 0px;}");
       this->setIcon(pix);
       this->setIconSize(pix.size());

       if (this->min > this->max){
           this->min=1;
           this->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.size());
       this->setStyleSheet("QPushButton{border: 0px;}");
       this->setIcon(pix);
       this->setIconSize(pix.size());

       if (this->max < this->min){
           this->max=8;
           this->isAnimation = false;
           timer2->stop();
       }
    });
}


void MyCoin::mousePressEvent(QMouseEvent *e){
    // 在动画或者已经赢了的状态之下就不再响应了
    if (this->isAnimation || this->isWin==true){
        return;
    }else{
        QPushButton::mousePressEvent(e);
    }
}



// 翻转方法
void MyCoin::changeFlag(){
    // 正面到反面
    if (this->flag){
        timer1->start(30);
        this->isAnimation = true;
        this->flag = false;
    } else {
        // 反面翻正面
        timer2->start(30);
        this->isAnimation = true;
        this->flag = true;
    }

}

6. dataconfig类

用于配置数据。

#ifndef DATACONFIG_H
#define DATACONFIG_H

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

class dataConfig : public QObject
{
    Q_OBJECT
public:
    explicit dataConfig(QObject *parent = 0);

public:

    QMap<int, QVector< QVector<int> > >mData;//双端数组 int 相当于关卡



signals:

public slots:
};

#endif // DATACONFIG_H

#include "dataconfig.h"
#include <QDebug>
#include <ctime>


dataConfig::dataConfig(QObject *parent) : QObject(parent)
{
    srand(time(NULL));
// 4 7


    //第一关
    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, 0, 0},
                         {1, 1, 0, 1},
                         {0, 1, 1, 1},
                         {0, 0, 1, 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] = {  {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(array3[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(3,v);


    int array4[4][4] = {   {0, 1, 1, 1},
                           {1, 1, 0, 1},
                           {1, 0, 1, 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(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, 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(array7[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(7,v);

    int array8[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(array8[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(8,v);


    int array9[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(array9[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(9,v);
    //===============10随机关==================//
    v.clear();

    int num=0;
    int sum=0;
    for(int i = 0 ; i < 4;i++)
    {
        QVector<int>v1;
        for(int j = 0 ; j < 4;j++)
        {
            num=rand()%2;
            v1.push_back(num);
            if(num==1){
                sum++;
            }
        }
        v.push_back(v1);
    }

    //还需要设置成偶数个,奇数个不行,可以利用vector的性质来做
    int changedValue=*v.begin()->begin();//改变第一个

    if((sum&1)!=0){
        //如果为奇数,则改变第一个
        if(changedValue==0){
            *v.begin()->begin()=1;
        }
        else{
            *v.begin()->begin()=0;
        }
    }
    mData.insert(10,v);

    //=================11-15 5个格子=======================


    int array11[5][5] = {  {0, 0, 0, 0, 0},
                           {0, 1, 0, 1, 0},
                           {0, 0, 1, 0, 0},
                           {0, 1, 0, 1, 0},
                           {0, 0, 0, 0, 0}};
    v.clear();
    for(int i = 0 ; i < 5;i++)
    {
        QVector<int>v1;
        for(int j = 0 ; j < 5;j++)
        {
            v1.push_back(array11[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(11,v);

    int array12[5][5] = {  {0, 0, 0, 1, 0},
                           {1, 0, 1, 1, 1},
                           {1, 0, 1, 1, 0},
                           {1, 0, 1, 1, 1},
                           {0, 0, 0, 1, 0}};
    v.clear();
    for(int i = 0 ; i < 5;i++)
    {
        QVector<int>v1;
        for(int j = 0 ; j < 5;j++)
        {
            v1.push_back(array12[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(12,v);


    int array13[5][5] = {  {1, 1, 1, 0, 0},
                           {0, 1, 0, 0, 0},
                           {0, 1, 1, 1, 1},
                           {0, 1, 0, 0, 1},
                           {1, 1, 1, 0, 1}};
    v.clear();
    for(int i = 0 ; i < 5;i++)
    {
        QVector<int>v1;
        for(int j = 0 ; j < 5;j++)
        {
            v1.push_back(array13[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(13,v);

    int array14[5][5] = {  {1, 1, 1, 1, 1},
                           {1, 0, 0, 0, 0},
                           {1, 0, 0, 0, 0},
                           {1, 0, 1, 1, 1},
                           {1, 0, 1, 1, 1}};
    v.clear();
    for(int i = 0 ; i < 5;i++)
    {
        QVector<int>v1;
        for(int j = 0 ; j < 5;j++)
        {
            v1.push_back(array14[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(14,v);


    //===============15随机关==================//
    v.clear();

    int num1=0;
    int sum1=0;
    for(int i = 0 ; i < 5;i++)
    {
        QVector<int>v1;
        for(int j = 0 ; j < 5;j++)
        {
            num1=rand()%2;
            v1.push_back(num1);
            if(num1==1){
                sum1++;
            }
        }
        v.push_back(v1);
    }

    //还需要设置成偶数个,奇数个不行,可以利用vector的性质来做
    int changedValue1=v[3][3];//改变中间那个

    if((sum1&1)!=0){
        //如果为奇数,则改变中间一个
        if(changedValue1==0){
            v[3][3]=1;
        }
        else{
            v[3][3]=0;
        }
    }
    mData.insert(15,v);


    //================6*6===================//


//    int array16[6][6] = { {1, 1, 1, 1, 1, 1},
//                          {1, 1, 1, 1, 1, 1},
//                          {1, 1, 1, 0, 1, 1},
//                          {1, 1, 0, 0, 0, 1},
//                          {1, 1, 1, 0, 1, 1},
//                          {1, 1, 1, 1, 1, 1} };


    int array16[6][6] = { {0, 0, 0, 0, 0, 0},
                          {0, 1, 1, 1, 1, 0},
                          {0, 1, 1, 1, 1, 0},
                          {0, 1, 1, 1, 1, 0},
                          {0, 1, 1, 1, 1, 0},
                          {0, 0, 0, 0, 0, 0} };
    v.clear();
    for(int i = 0 ; i < 6;i++)
    {
        QVector<int>v1;
        for(int j = 0 ; j < 6;j++)
        {
            v1.push_back(array16[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(16,v);

    int array17[6][6] = {  {1, 1, 0, 0, 0, 0},
                           {1, 1, 0, 0, 0, 1},
                           {1, 1, 0, 0, 0, 0},
                           {1, 1, 0, 0, 0, 0},
                           {1, 1, 0, 0, 0, 1},
                           {1, 1, 0, 0, 0, 0} };
    v.clear();
    for(int i = 0 ; i < 6;i++)
    {
        QVector<int>v1;
        for(int j = 0 ; j < 6;j++)
        {
            v1.push_back(array17[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(17,v);


    int array18[6][6] = {  {0, 0, 0, 0, 0, 1},
                           {0, 0, 1, 0, 0, 0},
                           {0, 1, 1, 0, 0, 0},
                           {1, 0, 0, 1, 1, 0},
                           {1, 1, 0, 1, 0, 0},
                           {1, 1, 1, 0, 0, 0} };
    v.clear();
    for(int i = 0 ; i < 6;i++)
    {
        QVector<int>v1;
        for(int j = 0 ; j < 6;j++)
        {
            v1.push_back(array18[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(18,v);

    int array19[6][6] = {  {0, 0, 0, 0, 0, 0},
                           {1, 0, 0, 0, 0, 0},
                           {0, 0, 0, 0, 0, 0},
                           {0, 0, 0, 0, 0, 0},
                           {0, 0, 0, 0, 0, 0},
                           {0, 0, 0, 0, 0, 0} };
    v.clear();
    for(int i = 0 ; i < 6;i++)
    {
        QVector<int>v1;
        for(int j = 0 ; j < 6;j++)
        {
            v1.push_back(array19[i][j]);
        }
        v.push_back(v1);
    }

    mData.insert(19,v);

    //===============20随机关==================//
    v.clear();

    int num2=0;
    int sum2=0;
    for(int i = 0 ; i < 6;i++)
    {
        QVector<int>v1;
        for(int j = 0 ; j < 6;j++)
        {
            num2=rand()%2;
            v1.push_back(num2);
            if(num2==1){
                sum2++;
            }
        }
        v.push_back(v1);
    }

    //还需要设置成偶数个,奇数个不行,可以利用vector的性质来做
    int changedValue2=v[4][4];//改变中间那个

    if((sum2&1)!=0){
        //如果为奇数,则改变中间一个
        if(changedValue2==0){
            v[4][4]=1;
        }
        else{
            v[4][4]=0;
        }
    }
    mData.insert(20,v);
    //===================额外测试关卡========================//


    //测试数据
    //    for( QMap<int, QVector< QVector<int> > >::iterator it = mData.begin();it != mData.end();it++ )
    //    {
    //         for(QVector< QVector<int> >::iterator it2 = (*it).begin(); it2!= (*it).end();it2++)
    //         {
    //            for(QVector<int>::iterator it3 = (*it2).begin(); it3 != (*it2).end(); it3++ )
    //            {
    //                qDebug() << *it3 ;
    //            }
    //         }
    //         qDebug() << endl;
    //    }


}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/139683.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据库,计算机网络、操作系统刷题笔记24

数据库&#xff0c;计算机网络、操作系统刷题笔记24 2022找工作是学历、能力和运气的超强结合体&#xff0c;遇到寒冬&#xff0c;大厂不招人&#xff0c;可能很多算法学生都得去找开发&#xff0c;测开 测开的话&#xff0c;你就得学数据库&#xff0c;sql&#xff0c;oracle…

c++下 ADO+配置数据源连接oracle数据库

测试环境&#xff1a;在本地局域网内远程连接服务器端的oracle数据库&#xff0c;VS2013、ADO方式。2、本地安装oracle数据库客户端&#xff0c;具体是安装32位还是64位的数据库客户端&#xff0c;取决于我们编译的程序是32位的还是64位的&#xff08;和计算机的系统位数没有关…

首款通过! 机器学习服务活体检测算法荣获CFCA权威安全认证

随着人脸识别技术在金融、医疗等多个领域的加速落地&#xff0c;网络安全、信息泄露等问题愈为突出&#xff0c;用户对应用稳定性和安全性的要求也更为严格。为保障各行业高效稳定的开展业务&#xff0c;提前发现和应对潜在安全风险&#xff0c;华为 HMS Core 机器学习服务&…

【回答问题】ChatGPT上线了!给我推荐20个比较流行的图神经网络模型

给我推荐20个比较流行的图神经网络模型 很高兴为您推荐流行的图神经网络模型。这里是我的推荐列表&#xff1a; 卷积神经网络 (CNN) 递归神经网络 (RNN) 自注意力机制 (Transformer) 双向递归神经网络 (BRNN) 门控循环单元 (GRU) 长短期记忆网络 (LSTM) 双流图神经网络 (Bi-D…

鲁大师2022牛角尖颁奖盛典落幕,年度最强产品揭晓!

1月4日&#xff0c;鲁大师2022年度牛角尖颁奖晚会在湖北襄阳成功举办。鲁大师的”牛角尖”奖是由过去一年上亿用户通过鲁大师测试得到的真实数据&#xff0c;以及鲁大师实验室通过专业的测试规范共同缔造的硬件奖项。颁发给的都是各大PC、手机、电动车领域最优秀、最顶尖的产品…

JAVA 的内存泄露分析

背景 前不久&#xff0c;上线了一个新项目&#xff0c;这个项目是一个压测系统&#xff0c;可以简单的看做通过回放词表&#xff08;http请求数据&#xff09;&#xff0c;不断地向服务发送请求&#xff0c;以达到压测服务的目的。在测试过程中&#xff0c;一切还算顺利&#x…

统一网关Gateway

网关功能&#xff1a; 身份验证、权限校验服务路由、负载均衡请求限流 网关实现技术&#xff1a; gatewayzuul Zuul是基于Servlet的实现&#xff0c;属于阻塞式编程。而SpringCloudGateway则是基于Spring5中提供的WebFlux&#xff0c;属于响应式编程的实现&#xff0c;具备更…

Servlet 综合案例(empProject)

✅作者简介&#xff1a;热爱国学的Java后端开发者&#xff0c;修心和技术同步精进。 &#x1f34e;个人主页&#xff1a;Java Fans的博客 &#x1f34a;个人信条&#xff1a;不迁怒&#xff0c;不贰过。小知识&#xff0c;大智慧。 &#x1f49e;当前专栏&#xff1a;Java案例分…

计算机网络进阶 ---- 网络类型 ---- 二层封装协议 ---- HDLC ---- PPP ---- pap认证 ---- chap认证 ---- 详解

一、网络类型&#xff1a; 【1】点到点 &#xff08;Peer to Peer – p2p&#xff09; ---- 在一个网段中&#xff0c;只能部署两个节点&#xff1b;【2】MA&#xff08;Multiple Access&#xff09; ---- 多路访问 ---- 一个网段中&#xff0c;可以部署的节点数量不限制&…

经典文献阅读之--OV2SLAM(高速视觉slam)

0. 简介 视觉里程计最近几年越来越受到学术界以及工业界的认可&#xff0c;以ORB和VINS为代表的视觉SLAM已经可以满足绝大多数场景&#xff0c;而OV2SLAM在其他VSLAM中脱颖而出&#xff0c;其实时性以及具体的回环性能在测试中都得到了认可。下面我们就来看一下《OV2SLAM : A …

TiDB分布式数据库架构介绍

简介 TiDB 是 PingCAP 公司自主设计、研发的开源分布式关系型数据库&#xff0c;是一款同时支持在线事务处理与在线分析处理 (Hybrid Transactional and Analytical Processing, HTAP) 的融合型分布式数据库产品&#xff0c;具备水平扩容或者缩容、金融级高可用、实时 HTAP、云…

Syncfusion Essential Studio Enterprise新功能

Syncfusion Essential Studio Enterprise新功能 添加了新的MediaQuery、Menute和Rating组件。 应用程序栏、浮动操作按钮、消息和快速拨号组件现在可以在生产中使用。 添加了对甘特图中按需加载的支持。 ASP.NET核心 添加了新的分级控件。 应用程序栏、浮动操作按钮、提及、消息…

什么是密码管理器?它安全吗?

密码管理器或密钥管理员是一类用于生成、检索、保存及管理复杂密码、数字签名的措施&#xff0c;可以由硬件或软件实现。因此&#xff0c;密码管理器一般也称作密码管理软件。复杂密码的生成一般按需要以随机算法产生&#xff0c;而密码数据则保存于一个以密码、数字签名等方式…

DP刷题(一)

目录 拆分单词 牛客题霸_牛客网 【思路梳理】​ 剑指 Offer II 100. 三角形中最小路径之和 【解法一】自顶向下 【解法二】自底向上 路径的数目 剑指 Offer II 098. 路径的数目 【解法一】 【解法二】 路径的数目&#xff08;加入障碍物&#xff09;63. 不同路径 II 【解…

unity 简单实现三阶魔方游戏

unity 简单实现三阶魔方游戏 魔方体验地址 工程文件免费下载 实现思路 一、魔方的旋转 三阶魔方由26个方块与 9个旋转轴组成。旋转轴旋转时带动在其控制范围的方块旋转。 旋转轴如何带动方块旋转&#xff1f; 把旋转轴控制范围内的方块设置成旋转轴的子物体&#xff0c;…

必背经典!这些软件测试面试题及答案别放过!

对于很多软件测试新手来说&#xff0c;技术面试往往是整个面试体系里最让人头疼的部分&#xff0c;今天我为选取了软件测试面试中&#xff0c;一些经典的问题和答案&#xff0c;供大家参考使用。并且&#xff0c;还给你们奉上了一个免费的面试刷题小程序哟&#xff01;拿走不谢…

调试前端代码二三事--(一)-调试基础

一&#xff0c;在网页上调试代码 代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"viewport…

房产管理系统中CAD图形管理分析

随着技术的不断进步和升级&#xff0c;以及高校房屋建筑物数量的不断扩充&#xff0c;建立房屋资产管理信息系统进行信息化、数字化、图形化房屋资产管理已经是势在必行。学校有庞大的房屋数据、大量的CAD图纸、复杂的房屋分类&#xff0c;建设房屋管理系统&#xff0c;能够加强…

JavaSE高级篇:运行时数据区

文章整理自深入理解Java虚拟机第一章概述第二章&#xff1a;运行时数据区域一&#xff1a;程序计数器二&#xff1a;Java虚拟机栈三&#xff1a;Java堆四&#xff1a;方法区五&#xff1a;运行时常量池六&#xff1a;直接内存第一章概述 Java程序员把内存控制的权利交给了JVM …

Spring MVC 获取参数

1..获取参数 得到单个参数&#xff1a; 运行结果&#xff1a; 得到俩个参数&#xff1a; 运行结果&#xff1a; 得到多个参数&#xff08;对象&#xff09; 运行结果&#xff1a; 通过表单传递参数&#xff1a; 但是&#xff0c;如果我用json传递就不行了&#xff0c;namenul…