杨记

碎片化学习令人焦虑,系统化学习使人进步

0%

翻金币游戏

学习自黑马程序员的QT项目之翻金币

CoinFlip

主界面

mainscene.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef MAINSCENE_H
#define MAINSCENE_H

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

namespace Ui {
class MainScene;
}

class MainScene : public QMainWindow
{
Q_OBJECT

public:
explicit MainScene(QWidget *parent = 0);
~MainScene();

//重写paintEvent事件 或背景图
void paintEvent(QPaintEvent *);

private:
Ui::MainScene *ui;
ChooseLevelScene *chooseLevelScene = NULL;
};

#endif // MAINSCENE_H

mainscene.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include <QPixmap>
#include "mypushbutton.h"
#include <QTimer>
#include <QSound>

MainScene::MainScene(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainScene)
{
ui->setupUi(this);

//配置主场景

//设置窗口标题
setWindowTitle("翻金币");

//设置固定大小
setFixedSize(320, 588);

//设置窗体左上角图标
setWindowIcon(QIcon(":/res/Coin0001.png"));

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

//准备点击开始按钮的音效
QSound *startSound = new QSound(":/res/TapButtonSound.wav", this);


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

this->chooseLevelScene = new ChooseLevelScene(this);

//按钮点击后 按钮跳到
connect(startBtn, &QPushButton::clicked, [=](){
startSound->play(); //播放音效
startBtn->zoomDown(); //向下跳
startBtn->zoomUp(); //向上跳
//延时进入选择关卡
QTimer *timer = new QTimer(this);
timer->start(500);
connect(timer, &QTimer::timeout, [=](){
//设置场景的出现位置和切换的窗口位置一致
this->chooseLevelScene->setGeometry(this->geometry());
this->chooseLevelScene->show();
this->hide();
timer->stop();
});
//5.3版本不支持
// QTimer::singleShot(500, this, [=](){
// this->hide();
// this->chooseLevelScene->show();
// });


});

//监听选择关卡的返回按钮的信号
connect(chooseLevelScene, &ChooseLevelScene::chooseSceneBack, this, [=](){
chooseLevelScene->hide(); //将选择关卡场景 隐藏
//设置场景的出现位置和切换的窗口位置一致
this->setGeometry(chooseLevelScene->geometry());
this->show(); //显示主场景
});
}

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

//重写paintEvent事件 或背景图
void MainScene::paintEvent(QPaintEvent *)
{
//创建画家
QPainter painter(this);
QPixmap pix(":/res/PlayLevelSceneBg.png");
//设置起点0,0 和 图片的长高 使其铺满屏幕
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);
}

选择关卡界面

chooselevelscene.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef CHOOSELEVELSCENE_H
#define CHOOSELEVELSCENE_H

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

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

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

PlayScene *playScene;

signals:
//写一个自定义信号,告诉主场景 点击了返回
void chooseSceneBack();

public slots:
};

#endif // CHOOSELEVELSCENE_H

chooselevelscene.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "chooselevelscene.h"
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QLabel>
#include <QSound>

ChooseLevelScene::ChooseLevelScene(QWidget *parent) :
QMainWindow(parent)
{
setFixedSize(320, 588);

setWindowTitle("选择关卡场景");

setWindowIcon(QIcon(":/res/Coin0001.png"));

//创建菜单栏
QMenuBar *menuBar = new QMenuBar();
setMenuBar(menuBar);

//创建开始菜单
QMenu *startMenu = menuBar->addMenu("开始");
//退出
QAction *quitAction = startMenu->addAction("退出");
connect(quitAction, &QAction::triggered, [=](){
this->close();
});

//选择关卡的音效
QSound *chooseSound = new QSound(":/res/TapButtonSound.wav", this);
//返回按钮的音效
QSound *backSound = new QSound(":/res/BackButtonSound.wav", this);

//返回按钮
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();
//告诉主场景 返回 主场景监听ChooseLevelScene的返回按钮
this->hide();
emit 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, [=](){
//播放选择关卡的音效
chooseSound->play();
//qDebug() << "这是第" << i + 1 << "关";
playScene = new PlayScene(i + 1);
this->hide();
//设置场景的出现位置和切换的窗口位置一致
playScene->setGeometry(this->geometry());
playScene->show();

connect(playScene, &PlayScene::playSceneBack, [=](){
//设置场景的出现位置和切换的窗口位置一致
this->setGeometry(this->geometry());
this->show();
delete playScene;
playScene = NULL;
backSound->play();
});
});

QLabel *label = new QLabel(menuBtn);
label->setText(QString::number(i + 1));
label->setFixedSize(menuBtn->width(), menuBtn->height());
//设置文字对齐方式 水平居中 Qt::AlignHCenter 垂直居中Qt::AlignVCenter 中间Qt::AlignCenter
label->setAlignment(Qt::AlignCenter);

//便签覆盖在按钮上,会挡住鼠标的点击,要设置label的属性为穿透鼠标事件
label->setAttribute(Qt::WA_TransparentForMouseEvents);
}
}

//重写绘图事件
void ChooseLevelScene::paintEvent(QPaintEvent *)
{
QPainter painter(this);
//加载背景
QPixmap pix(":/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);
}

翻金币界面

playscene.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef PLAYSCENE_H
#define PLAYSCENE_H

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

class PlayScene : public QMainWindow
{
Q_OBJECT
public:
//explicit PlayScene(QWidget *parent = 0);

PlayScene(int levelNum);

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

//游戏地图
void gameMap();

//翻转金币
void turnCoin(MyCoin *coin);

//判断是否胜利
bool justWin();

//胜利的动画
void animationOfWin();

int levelIndex; //记录关卡号

int gameArray[4][4];

MyCoin *coinBtn[4][4];

bool isAnimation = false; //是否有金币正在翻转,金币翻转时不能点击

bool isWin = false;

QLabel *winLabel; //胜利的图片

QSound *flipSound; //点击音乐的音效

QSound *backSound; //点击返回的音效

QSound *winSound; //胜利的音效

signals:
void playSceneBack();

public slots:

};

#endif // PLAYSCENE_H

playscene.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "playscene.h"
#include <QIcon>
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QFont>
#include "mycoin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>

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

PlayScene::PlayScene(int levelNum)
{
this->levelIndex = levelNum;

//初始化游戏场景
//设置固定大小
this->setFixedSize(320, 588);
//设置图标
this->setWindowIcon(QIcon(":/res/Coin0001.png"));
//设置标题
this->setWindowTitle("翻金币场景");

//创建菜单栏
QMenuBar *menuBar = new QMenuBar();
setMenuBar(menuBar);

//创建开始菜单
QMenu *startMenu = menuBar->addMenu("开始");
//退出
QAction *quitAction = startMenu->addAction("退出");
connect(quitAction, &QAction::triggered, [=](){
this->close();
});

//胜利的音效
winSound = new QSound(":/res/LevelWinSound.wav", this);
//点击金币的音效
flipSound = new QSound(":/res/ConFlipSound.wav", this);
//返回按钮的音效
backSound = new QSound(":/res/BackButtonSound.wav", this);

//返回按钮
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, [=](){
//由于点击返回按钮之后,选择关卡页面会把该游戏界面的内存释放掉,速度很快,所以听不到返回音效
backSound->play();

//告诉主场景 返回 主场景监听ChooseLevelScene的返回按钮
this->hide();
emit playSceneBack();
});

QLabel *label = new QLabel(this);
//设置字体
QFont font;
font.setFamily("华文新魏");
font.setPointSize(20); //设置字号
QString str = QString("Level: %1").arg(this->levelIndex);//显示的文字
//将字体设置到标签控件中
label->setFont(font);
label->setText(str);
//设置标签位置
label->setGeometry(QRect(30, this->height() - 50, 150, 50));

//初始化游戏地图
dataConfig data;

for(int i = 0; i < 4; ++i)
{
for(int j = 0; j < 4; ++j)
{
gameArray[i][j] = data.mData[this->levelIndex][i][j];
}
}

//胜利的标签和图片
winLabel = new QLabel(this);
QPixmap pix(":/res/LevelCompletedDialogBg.png");
winLabel->setGeometry(0, 0, pix.width(), pix.height()); //标签大小
winLabel->setPixmap(pix); //设置图片
winLabel->move((this->width() - pix.width()) * 0.5, -pix.height());

gameMap();
}

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

//创建金币
QString str = QString(":/res/Coin0001.png");
if(gameArray[i][j] == 0)
{
str = ":/res/Coin0008.png";
}
MyCoin *coin = new MyCoin(str);
//给金币属性赋值
coin->posX = i;
coin->posY = j;
coin->flag = gameArray[i][j]; //1正面 0反面
coin->setParent(label);
coin->move((label->width() - coin->width()) * 0.5, (label->height() - coin->height()) * 0.5);

//将金币放入到金币的二维数组,以便后期 的维护
coinBtn[i][j] = coin;

//监听金币是否翻转
connect(coin, &QPushButton::clicked, [=](){
if(this->isAnimation || isWin) return;
flipSound->play(); //播放金币翻转的音效
turnCoin(coin); //翻转金币
if(justWin())
{
animationOfWin();
}
});
}
}
}

//翻转金币
void PlayScene::turnCoin(MyCoin *coin)
{
this->isAnimation = true;
coin->changeFlag();

//翻转周围硬币操作
if(coin->posX + 1 < 4) //右侧金币
{
coinBtn[coin->posX + 1][coin->posY]->changeFlag();
}
if(coin->posX - 1 >= 0) //左侧金币
{
coinBtn[coin->posX - 1][coin->posY]->changeFlag();
}
if(coin->posY + 1 < 4) //下方金币
{
coinBtn[coin->posX][coin->posY + 1]->changeFlag();
}
if(coin->posY - 1 >= 0) //上方金币
{
coinBtn[coin->posX][coin->posY - 1]->changeFlag();
}
this->isAnimation = false;
}

//判断是否胜利
bool PlayScene::justWin()
{
//判断是否胜利
this->isWin = true;
for(int i = 0; i < 4; ++i)
{
for(int j = 0; j < 4; ++j)
{
if(coinBtn[i][j]->flag == false)
{
this->isWin = false;
i = 4; // 跳出循环
break;
}
}
}
return this->isWin;
}

//胜利的特效
void PlayScene::animationOfWin()
{
winSound->play(); //播放胜利的音效
//动效
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();
}

//重写paintEvent事件
void PlayScene::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");
painter.drawPixmap((this->width() - pix.width()) * 0.5, 30, pix.width(), pix.height(), pix);
}

自定义按钮

mypushbutton.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H

#include <QPushButton>

class MyPushButton : public QPushButton
{
Q_OBJECT
public:
//explicit MyPushButton(QWidget *parent = 0);

//构造函数 参数1 正常显示的图片路径 参数2 按下后显示的图片路径
MyPushButton(QString normalImg, QString pressImg = "");

//按钮图片和大小
void initPushButton(QString pixPath);

//按钮弹跳特效
void zoomUp();
void zoomDown();

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

QString normalImgPath;
QString pressImgPath;

signals:

public slots:

};

#endif // MYPUSHBUTTON_H

mypushbutton.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "mypushbutton.h"
#include <QDebug>
#include <QPropertyAnimation>

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

MyPushButton::MyPushButton(QString normalImg, QString pressImg)
{
this->normalImgPath = normalImg;
this->pressImgPath = pressImg;

//设置按钮图片和大小
initPushButton(normalImg);
}

//按钮图片和大小
void MyPushButton::initPushButton(QString pixPath)
{
QPixmap pix;
bool ret = pix.load(pixPath);
if(!ret)
{
qDebug() << "图片加载失败" << endl;
}

//设置按钮固定大小
this->setFixedSize(pix.width(), pix.height());

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

//设置图标
this->setIcon(pix);

//设置图标大小
this->setIconSize(QSize(pix.width(), pix.height()));
}

//向下跳
void MyPushButton::zoomDown()
{
//创建动态对象
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::OutElastic);

//执行动画
animation->start();
}

//向上跳
void MyPushButton::zoomUp()
{
//创建动态对象
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::OutElastic);

//执行动画
animation->start();
}

void MyPushButton::mousePressEvent(QMouseEvent *e)
{
if(this->pressImgPath != "") //传入的按下图片不为空 说明需要有按下状态 切换图片
{
initPushButton(this->pressImgPath);
}

return QPushButton::mousePressEvent(e);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
if(this->pressImgPath != "") //传入的按下图片不为空 说明需要有按下状态 切换图片
{
initPushButton(this->normalImgPath);
}

return QPushButton::mouseReleaseEvent(e);
}

金币类

mycoin.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef MYCOIN_H
#define MYCOIN_H

#include <QPushButton>
#include <QTimer>

class MyCoin : public QPushButton
{
Q_OBJECT
public:
//explicit MyCoin(QWidget *parent = 0);

MyCoin(QString btnImg);

//设置金币图片
void initCoinPicture(QString path);

int posX; //x坐标
int posY; //y坐标
bool flag; //正反标识

//改变标志的方法
void changeFlag();

QTimer *postToNega; //正面翻反面的定时器
QTimer *negaToPost; //反面翻正面的定时器

bool isAnimation = false; //正在翻转动画

int min = 1;
int max = 8;

signals:

public slots:

};

#endif // MYCOIN_H

mycoin.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "mycoin.h"
#include <QDebug>

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

MyCoin::MyCoin(QString btnImg)
{
initCoinPicture(btnImg);

//初始化定时器对象
postToNega = new QTimer(this);
negaToPost = new QTimer(this);

//监听正面翻反面的信号,并翻转金币
connect(postToNega, &QTimer::timeout, [=](){
QString str = QString(":/res/Coin000%1").arg(this->min++);
initCoinPicture(str);

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

//监听正面翻反面的信号,并翻转金币
connect(negaToPost, &QTimer::timeout, [=](){
QString str = QString(":/res/Coin000%1").arg(this->max--);
initCoinPicture(str);

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

void MyCoin::initCoinPicture(QString path)
{
QPixmap pix;
bool ret = pix.load(path);
if(!ret)
{
qDebug() << path << "图片加载失败";
}
//设置按钮大小
this->setFixedSize(pix.width(), pix.height());
//设置图片样式
this->setStyleSheet("QPushButton{border:0px;}");
//按钮添加图片
this->setIcon(pix);
//设置图片大小
this->setIconSize(QSize(pix.width(), pix.height()));
}

//改变标志的方法
void MyCoin::changeFlag()
{
if(this->isAnimation)
{
return;
}
this->isAnimation = true;
if(this->flag)
{
postToNega->start(30);
}
else
{
negaToPost->start(30);
}
this->flag = !flag;
}

游戏地图数据

dataconfig.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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;


signals:

public slots:
};

#endif // DATACONFIG_H

dataconfig.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#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);


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


//测试数据
// 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;
// }


}

效果

image-20220314200636824

image-20220314201039691

image-20220314201051661

欢迎关注我的其它发布渠道