-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayscene.cpp
More file actions
170 lines (130 loc) · 4.86 KB
/
playscene.cpp
File metadata and controls
170 lines (130 loc) · 4.86 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
#include "playscene.h"
playScene::playScene(int level,QWidget *parent)
: MyMainWindow{parent}
{
MyPushButton *btnBackButton=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png",this);
btnBackButton->resize(72,32);
btnBackButton->move(this->width()-btnBackButton->width(),this->height()-btnBackButton->height());
connect(btnBackButton,&QPushButton::clicked,this,&playScene::backBtnClicked);
//信号可以连接信号
//设置关卡表示的label
QLabel *levelLabel=new QLabel(this);
levelLabel->setText(QString("Level:%1").arg(level));
levelLabel->setFont(QFont("华文新魏",20));//设置字体为华文新魏 大小20号
levelLabel->move(30,this->height()-levelLabel->height());
const int rowWidth=50;
const int colWidth=50;//设置行宽和列宽
const int x_Offset=57;
const int y_Offset=200;//设置x y的偏移量
dataConfig levelData;
QVector<QVector<int>> dataArray=levelData.mData[level];//导入关卡数据
mHasWin=false;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
coinButton *coinBtn=new coinButton(this);
int x=i*rowWidth+x_Offset;
int y=j*colWidth+y_Offset;//计算coinBtn's x/y position
coinBtn->setGeometry(x,y,50,50);
coinBtn->setCoin_Status(dataArray[i][j]);//重新调用一下coinBtn的set方法 为了使IconSize==coinBtnSize
allBtns[i][j]=coinBtn;//可以访问到每个按钮了
//点击按钮发生硬币翻转
connect(coinBtn,&QPushButton::clicked,[=](){
if(!mHasWin)//赢了就不准翻 也不准播放翻硬币的声音
{
//设置按钮音效
QSoundEffect *sound=new QSoundEffect(this);
sound->setSource(QUrl::fromLocalFile(":/res/ConFlipSound.wav"));
sound->play();
}
this->flip(i,j);
});
}
}
}
void playScene::flip(int row, int col)
{
if(mHasWin)
{
return;//判断胜利后不再允许翻动 直接返回
}
allBtns[row][col]->flip();
//点击之后 除了自身 上下左右四个硬币(按钮)都要翻转
//为了防止数组越界导致程序挂掉 做出显示
QTimer::singleShot(250,[=]()//设置个定时器 等中间的按钮翻转完成以后 再翻动四方按钮
{
if(row+1<4)
{
allBtns[row+1][col]->flip();
}
if(row-1>=0)
{
allBtns[row-1][col]->flip();
}
if(col+1<4)
{
allBtns[row][col+1]->flip();
}
if(col-1>=0)
{
allBtns[row][col-1]->flip();
}
});
// 再延迟一点时间,确保所有硬币都完成了翻转动画
//因为动画是异步执行的 所以需要等所有动画都完成后再判断胜利条件
QTimer::singleShot(300,[=](){
this->judgeWin();
});
}
void playScene::judgeWin()
{
// qDebug()<<"call to judgeWon()";
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
//如果游戏要胜利的话 必须全部都是金币 只要有个btn是银币就没胜利
if(allBtns[i][j]->getCoin_Status()==0)
{
// qDebug()<<"666";
return;
}
}
}
// qDebug()<<"win";
// QMessageBox::information(this,"恭喜!","你获得了游戏胜利!");
mHasWin=true;
//设置按钮音效
QSoundEffect *sound=new QSoundEffect(this);
sound->setSource(QUrl::fromLocalFile(":/res/LevelWinSound.wav"));
sound->play();
//播放胜利动画
QLabel *winLabel=new QLabel(this);
QPixmap pix(":/res/LevelCompletedDialogBg.png");
winLabel->setPixmap(pix);
winLabel->resize(pix.size());
winLabel->show();
//先把labe隐藏起来
winLabel->move((this->width()-winLabel->width())/2,-winLabel->height());
//制作动画
QPropertyAnimation *successAnimation=new QPropertyAnimation(winLabel,"geometry",this);
successAnimation->setStartValue(winLabel->geometry());
successAnimation->setEndValue(QRect(winLabel->x(),winLabel->y()+100,winLabel->width(),winLabel->height()));
successAnimation->setDuration(1000);
successAnimation->setEasingCurve(QEasingCurve::OutBounce);//设置动画曲线
successAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}
void playScene::paintEvent(QPaintEvent *e)
{
//绘制背景图
QPainter painter(this);
painter.translate(0,this->menuBar()->height());//设置画家开始绘画的位置
QPixmap pix(":/res/PlayLevelSceneBg.png");
painter.drawPixmap(0,0,this->width(),this->height(),pix);
//绘制logo
pix.load(":/res/Title.png");
//logo缩放到%50
pix=pix.scaled(pix.width()/2,pix.height()/2);
painter.drawPixmap(0,0,pix);
}