项目实践
前言
本项目的实践主要是以翻金币的项目为例,进行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;
// }
}