-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayscene.cpp
226 lines (200 loc) · 7.34 KB
/
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
218
219
220
221
222
223
224
225
226
#include "playscene.h"
#include <QMenuBar>
#include <QAction>
#include <QPainter>
#include <QTimer>
#include <QLabel>
#include <QDebug>
#include <QPropertyAnimation>
#include "mycoin.h"
#include "dataconfig.h"
//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{
//}
PlayScene::PlayScene(int levelNum)
{
this->levelIndex = levelNum;
//固定宽高
this->setFixedSize(320,588);
//设置应用图片
this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
//设置窗口标题
this->setWindowTitle("翻金币");
//无ui文件,自定义
//创建菜单栏
QMenuBar *bar = menuBar();
setMenuBar(bar);
//创建开始菜单
QMenu *startMenu = bar->addMenu("开始");
//创建退出菜单项
QAction *quitAction = startMenu->addAction("退出");
connect(quitAction,&QAction::triggered,[=](){
this->close();
});
coinsoud = new QSound(":/res/ConFlipSound.wav",this);
winsoud = new QSound(":/res/LevelWinSound.wav",this);
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,this,&PlayScene::back);
//显示当前关卡数
QLabel * label = new QLabel(this);
QFont font;
font.setFamily("华文新魏");
font.setPointSize(20);
label->setFont(font);
//设置位置和大小
label->setGeometry(30, this->height()-50, 150, 50);
label->setText(QString("Level:%1").arg(levelNum));
this->todo(); //游戏场景
}
void PlayScene::paintEvent(QPaintEvent *)
{
QPainter paint(this);
QPixmap pix;
pix.load(":/res/PlayLevelSceneBg.png");//背景
paint.drawPixmap(0,0,this->width(),this->height(),pix);
//画背景上的图标
pix.load(":/res/Title.png");
paint.drawPixmap(this->width() * 0.5 - pix.width() * 0.5 ,30,pix.width() ,pix.height(),pix);
}
void PlayScene::back()
{
winsoud->stop();
coinsoud->stop();
QSound * backSound = new QSound(":/res/TapButtonSound.wav",this);
backSound->play();
//告诉选择场景返回信号,选择场景监听信号
QTimer::singleShot(300,[=](){
emit this->chooseBackCenario();
});
}
void PlayScene::todo()
{
//胜利图片显示 默认在背景外
QLabel * winLabel = new QLabel;
QPixmap tmpPix;
tmpPix.load(":/res/LevelCompletedDialogBg.png");
winLabel->setPixmap(tmpPix);
winLabel->setParent(this);
winLabel->move((this->width() - tmpPix.width()) * 0.5, -400);
//初始化每个关卡的二维数组
dataConfig config;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
}
}
//金币
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
//绘制背景图片
QPixmap pix=QPixmap(":/res/BoardNode.png");
QLabel *label=new QLabel;
label->setGeometry(0,0,50,50);
label->setPixmap(pix);
label->setParent(this);
label->move(57+i*50,200+j*50);
//判断是金币还是银币
QString str;
if(this->gameArray[i][j]==1)
{
str=":/res/Coin0001.png";
}
else
{
str=":/res/Coin0008.png";
}
//创建金币
MyCoin *coin=new MyCoin(str);
coin->setParent(this);
coin->move(59+i*50,204+j*50);
//设置金币属性
coin->posX=i;
coin->posY=j;
coin->flag=this->gameArray[i][j]; // 1正面 0反面
//将金币放入到金币的二维数组里面
this->coinBtn[i][j] = coin;
//点击金币进行翻转
connect(coin,&QPushButton::clicked,[=](){
coinsoud->play();
//在翻转金币的时候禁用其他按钮
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(100,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<=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;
}
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;
}
//在翻转完金币的时候释放其他按钮
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(coinBtn[i][j]->flag==false){
this->isWin=false;
break;
}
}
}
if(this->isWin==true){
//this->backBtn->m_flag = true;
//胜利了
qDebug() << "胜利了";
//将所有按钮胜利的标志改为 true,如果再次点击按钮直接return
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++){
coinBtn[i][j]->isWin=true;
}
}
//将胜利图片砸下来
QPropertyAnimation * an = new QPropertyAnimation(winLabel,"geometry");
an->setDuration(1000);
an->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
an->setEndValue(QRect(winLabel->x(),winLabel->y() + 300,winLabel->width(),winLabel->height()));
an->setEasingCurve(QEasingCurve::OutBounce);
an->start();
winsoud->play();
}
});
});
}
}
}