-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayscene.cpp
More file actions
179 lines (166 loc) · 6.46 KB
/
playscene.cpp
File metadata and controls
179 lines (166 loc) · 6.46 KB
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
#include "playscene.h"
#include "mypushbutton.h"
#include "mycoin.h"
#include "dataconfig.h"
#include <QDebug>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <QTimer>
#include <QLabel>
#include <QPixmap>
#include <QPropertyAnimation>
#include <QSound>
//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{
//
//}
PlayScene::PlayScene(int playLevel)
: m_playLevel{playLevel}
{
//设置窗口固定大小
this->setFixedSize(320, 588);
//设置图标
this->setWindowIcon(QPixmap(":/img/Coin0001.png"));
//设置标题
this->setWindowTitle("play scene");
//创建菜单栏
QMenuBar *bar = this->menuBar();
this->setMenuBar(bar);
//创建开始菜单
QMenu *startMenu = bar->addMenu("start");
//创建按钮菜单项
QAction *quitAction = startMenu->addAction("quit");
//点击退出 退出游戏
connect(quitAction, &QAction::triggered, [=]() { this->close(); });
MyPushButton *backButton{new MyPushButton(":/img/BackButton.png", ":/img/BackButtonSelected")};
backButton->setParent(this);
backButton->move(this->width() - backButton->width(), this->height() - backButton->height());
QSound *backSound = new QSound(":/img/BackButtonSound.wav", this);
connect(backButton, &QPushButton::clicked, [=]() {
backSound->play();
QTimer::singleShot(500, this, [=]() {
emit this->chooseSecneBack();
});
});
//创建具体用户选择的关卡标签
QLabel *label = new QLabel;
label->setParent(this);
QString str = QString("leavel: %1").arg(this->m_playLevel);
label->setText(str);
//设置字体和字号
QFont font;
font.setFamily("华文新魏");
font.setPointSize(20);
label->setFont(font);
//设置位置和大小
label->setGeometry(QRect(30, this->height() - 50, 120, 50));
dataConfig config;
// to create success img
QLabel *winLabel = new QLabel;
QPixmap tmpPix;
tmpPix.load(":/img/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 *playLabel = new QLabel;
QPixmap pix;
pix.load(":/img/BoardNode.png");
playLabel->setGeometry(0, 0, pix.width(), pix.height());
playLabel->setPixmap(pix);
playLabel->setParent(this);
playLabel->move(57 + i * pix.width(), 200 + j * pix.height());
this->m_array[i][j] = config.mData[this->m_playLevel][i][j];
QString imagePath{};
if (this->m_array[i][j])
{
imagePath = ":/img/Coin0001.png";
}
else
{
imagePath = ":/img/Coin0008.png";
}
MyCoin *coin = new MyCoin(imagePath);
coin->setParent(this);
coin->move(59 + i * pix.width(), 204 + j * pix.height());
this->m_coinList[i][j] = coin;
coin->m_posX = i;
coin->m_posY = j;
coin->m_flag = this->m_array[i][j];
QSound *flipSound = new QSound(":/img/ConFlipSound.wav", this);
QSound *winSound = new QSound(":/img/LevelWinSound.wav", this);
connect(coin, &MyCoin::clicked, [=]() {
flipSound->play();
coin->changeFlag();
this->m_array[i][j] = this->m_array[i][j] == 1 ? 0 : 1;
QTimer::singleShot(30, this, [=]() {
if (coin->m_posX > 0)
{
this->m_coinList[coin->m_posX - 1][coin->m_posY]->changeFlag();
this->m_array[i - 1][j] = this->m_array[i - 1][j] == 1 ? 0 : 1;
}
if (coin->m_posX < 3)
{
this->m_coinList[coin->m_posX + 1][coin->m_posY]->changeFlag();
this->m_array[i + 1][j] = this->m_array[i + 1][j] == 1 ? 0 : 1;
}
if (coin->m_posY > 0)
{
this->m_coinList[coin->m_posX][coin->m_posY - 1]->changeFlag();
this->m_array[i][j - 1] = this->m_array[i][j - 1] == 1 ? 0 : 1;
}
if (coin->m_posY < 3)
{
this->m_coinList[coin->m_posX][coin->m_posY + 1]->changeFlag();
this->m_array[i][j + 1] = this->m_array[i][j + 1] == 1 ? 0 : 1;
}
this->m_isWin = true;
for (int i{0}; i < 4; ++i)
{
for (int j{0}; j < 4; ++j)
{
if (this->m_array[i][j] == false)
{
this->m_isWin = false;
}
}
}
if (this->m_isWin)
{
for (int i{0}; i < 4; ++i)
{
for (int j{0}; j < 4; ++j)
{
this->m_coinList[i][j]->m_isWin = true;
}
}
qDebug() << "win!";
winSound->play();
QPropertyAnimation *animation1 = new QPropertyAnimation(winLabel, "geometry");
animation1->setDuration(1000);
animation1->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
animation1->setEndValue(QRect(winLabel->x(), winLabel->y() + 114, winLabel->width(), winLabel->height()));
animation1->setEasingCurve(QEasingCurve::OutBounce);
animation1->start(QAbstractAnimation::DeleteWhenStopped);
}
});
});
}
}
}
void PlayScene::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPixmap pix;
pix.load(":/img/PlayLevelSceneBg.png");
painter.drawPixmap(0, 0, this->width(), this->height(), pix);
pix.load(":/img/Title.png");
pix = pix.scaled(pix.width() * 0.5, pix.height() * 0.5);
painter.drawPixmap(10, 30, pix.width(), pix.height(), pix);
}