-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamewidget.cpp
591 lines (512 loc) · 19.7 KB
/
gamewidget.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#include "gamewidget.h"
#include "ui_gamewidget.h"
#include <QFileInfo>
#include <QPainter>
#include <QBrush>
#include <QRandomGenerator>
#include <QMouseEvent>
#include <QTime>
#include<QPropertyAnimation>
#include <algorithm>
GameWidget::GameWidget(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::GameWidget)
{
ui->setupUi(this);
//窗口基本设置
setWindowFlag(Qt::Window); //设置为独立窗口
setWindowTitle("Rockem Blocks");
setFixedSize(1024,768);
setWindowIcon(QIcon(":/pic/Gem/Gold.png"));
len = this->width()/100*6;
//初始化场景、子控件及音效
initBgm();
initWidgets();
initScene();
initSound();
settingWidget->raise();
connect(resetButton, &QPushButton::clicked, this, &GameWidget::reset);
connect(menuButton, &QPushButton::clicked, [=](){
settingWidget->raise();
settingWidget->show();
progressTimer->stop();
});
}
GameWidget::~GameWidget()
{
delete ui;
}
void GameWidget::initBgm(){
bgGame = new QMediaPlayer(this);
bgGame->setVolume(90);
//使用前务必修改为对应路径
QFileInfo file(QApplication::applicationDirPath() + "/music/Classic.mp3");
Q_ASSERT(file.exists());
bgGame->setMedia(QUrl::fromLocalFile(QApplication::applicationDirPath() + "/music/Classic.mp3"));
connect(bgGame, &QMediaPlayer::mediaStatusChanged, [=](QMediaPlayer::MediaStatus status){
if(status == QMediaPlayer::EndOfMedia && !is_end)
bgGame->play();
});
}
void GameWidget::paintEvent(QPaintEvent *event){
QPainter painter(this);
//背景图片
painter.drawPixmap(0,0,this->width(), this->height(), QPixmap(":/pic/InGame/background.jpg"));
//方格
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(0,0,0,255));
painter.setOpacity(0.6);
for(int i = 0; i < 10; ++i)
for(int j = 0; j < 10; ++j)
if((i + j) % 2 == 0)
painter.drawRect(this->width()/20*7 + len*j, (this->height()- 10*len)/2 + len*i, len, len);
painter.setOpacity(0.5);
for(int i = 0; i < 10; ++i)
for(int j = 0; j < 10; ++j)
if((i + j) % 2 == 1)
painter.drawRect(this->width()/20*7 + len*j, (this->height()- 10*len)/2 + len*i, len, len);
//棋盘上下装饰
painter.setOpacity(1);
painter.drawPixmap(this->width()/20*7, (this->height()- 10*len)/2 - len*3/8, 10*len, len*5/8, QPixmap(":/pic/InGame/top frame.png"));
//painter.drawPixmap(this->width()/20*7, this->height() - len, 10*len, len*7/10, QPixmap(":/pic/InGame/bottom frame.png"));
//记分器
painter.drawPixmap(this->width()/20, this->height()/8, this->width()/5, (int)this->width() * 0.1157, QPixmap(":/pic/InGame/score.png"));
//菜单栏
painter.drawPixmap(this->width()/20, this->height()/20*11, this->width()/5, this->width()*14/50, QPixmap(":/pic/InGame/bottom widget classic.png"));
QWidget::paintEvent(event);
}
void GameWidget::initWidgets(){
menuButton = new HoverButton(this);
resetButton = new HoverButton(this);
scoreLabel = new QLabel("0", this);
progressBar = new QProgressBar(this);
decorateLabel = new QLabel(this);
menuButton->setGeometry(this->width()/20+(int)this->width()/5 * 50.16/288, this->height()/20*11+this->width()*14/50 * 57.79/396, \
(int)this->width()*187.11/5/288, (int)this->width()*187.11/5/288);
menuButton->setImage(":/pic/InGame/menu.png", ":/pic/InGame/menu_hover.png", menuButton->width(), menuButton->height());
resetButton->setGeometry(this->width()/20 + (int)this->width()/5*93.18/288, this->height()/20*11 + this->width()*14/50*276.26/396, \
(int)this->width()/5*97.5/288, (int)this->width()/5*97.5/288);
resetButton->setImage(":/pic/InGame/reset.png", ":/pic/InGame/reset_hover.png", resetButton->width(), resetButton->height());
scoreLabel->setGeometry(this->width()/20 + (int)this->width()/5*22.68/280, this->height()/8 + (int)this->width()*0.1157*38.23/162, \
(int)this->width()/5*235.73/280, (int)this->width()*0.1157*57.43/162);
scoreLabel->setFont(QFont("Microsoft YaHei", 12, 100));
scoreLabel->setAlignment(Qt::AlignCenter);
scoreLabel->setStyleSheet("QLabel{color:white;}");
decorateLabel->setStyleSheet("QLabel{border-image:url(:/pic/InGame/bottom frame.png);}");
progressBar->setGeometry(this->width()/20*7 + 20, this->height() - len, 10*len - 40, len*7/10 - 15);
decorateLabel->setGeometry(this->width()/20*7 - 20, this->height() - len - 10, 10*len + 30, len*7/10 + 5);
progressBar->setRange(0,99);
progressBar->setValue(0);
progressBar->setFormat("%p%");
progressBar->setAlignment(Qt::AlignCenter);
progressBar->setFont(QFont("Microsoft YaHei", 12, 100));
progressBar->setStyleSheet("QProgressBar{color:grey;} QProgressBar::chunk{background-color:#24247e}");
progressTimer = new QTimer(this);
progressTimer->setInterval(1800);
connect(progressTimer, &QTimer::timeout, [=](){
if(progressBar->value() == 99)
end();
else
progressBar->setValue(progressBar->value()+1);
});
settingWidget = new SettingWidget(2, DIFFICULITY-3, this);
settingWidget->button()->setSound(":/sound/button_mouseover.wav", ":/sound/button_mouseleave.wav", ":/sound/button_press.wav", ":/sound/button_release.wav");
settingWidget->setGeometry(0,0,1024,768);
settingWidget->hide();
connect(settingWidget->mask(), &QPushButton::clicked, [=](){
settingWidget->hide();
progressTimer->start();
});
connect(settingWidget->button(), &HoverButton::clicked, this, &GameWidget::returnToStart);
settlementDialog = new Dialog(this);
settlementDialog->hide();
settlementDialog->setGeometry(0,0,1024,768);
settlementDialog->descriptionLabel()->setText("您的得分为");
settlementDialog->lineEdit()->setReadOnly(true);
connect(settlementDialog->button(), &HoverButton::clicked, this, &GameWidget::returnToStart);
}
void GameWidget::initSound(){
soundGo = new QSound(":/sound/voice_go.wav", this);
soundGood = new QSound(":/sound/voice_good.wav", this);
soundExcellent = new QSound(":/sound/voice_excellent.wav", this);
soundAwesome = new QSound(":/sound/voice_awesome.wav", this);
soundBadmove = new QSound(":/sound/badmove.wav", this);
soundAct = new QSound(":/sound/combo_2.wav", this);
soundFall = new QSound(":/sound/gem_hit.wav", this);
soundGenerate = new QSound(":/sound/gem_hit.wav", this);
soundUnbelievable = new QSound(":/sound/voice_unbelievable.wav", this);
soundTimeUp = new QSound(":/sound/voice_timeup.wav", this);
soundNoMoreMoves = new QSound(":/sound/voice_nomoremoves.wav", this);
soundMagic = new QSound(":/sound/gem_shatters.wav", this);
}
int GameWidget::randomGem(bool allowMagic){
if(allowMagic && QRandomGenerator::global()->bounded(0, 85) == 1) //Magic方块1/85生成概率
return 0;
return QRandomGenerator::global()->bounded(1, DIFFICULITY+1);
}
void GameWidget::initScene(){
boardWidget = new QWidget(this);
boardWidget->show();
boardWidget->setGeometry(1024/20*7, (768- 10*len)/2, 10*len, 10*len);
QRandomGenerator::global()->fillRange(gemBoard[0], 100);
for(int i = 0; i < 10; ++i)
for(int j = 0; j < 10; ++j){
gemBoard[i][j] = gemBoard[i][j] % DIFFICULITY + 1;
gems[i][j] = new Gem(gemBoard[i][j], len, i, j, boardWidget);
gems[i][j]->installEventFilter(this);
connect(gems[i][j], &Gem::mouseClicked, this, &GameWidget::act);
}
}
void GameWidget::start(){
settingWidget->hide();
settingWidget->raise();
settlementDialog->hide();
settlementDialog->raise();
resetButton->setImage(":/pic/InGame/reset.png", ":/pic/InGame/reset_hover.png", resetButton->width(), resetButton->height());
resetButton->setEnabled(true);
progressBar->setValue(0);
score = 0;
scoreLabel->setNum(score);
progressBar->setValue(0);
soundGo->play();
progressTimer->start();
}
void GameWidget::act(Gem* gem){
is_acting = true;
bool allowMagic = (gem->type()!=0);
toBomb.clear();
BombInfo bombInfo;
bombInfo= (gem->type() != 0 ? gemBomb(gem, gem->type()) : magicBomb(gem));
int cntStraight = bombInfo.num_straight;
Q_ASSERT(bombInfo.cnt == (int)toBomb.size());
if(gem->type()==0 && bombInfo.cnt < 3)
bombInfo.cnt = 3;
if(bombInfo.cnt < 3){
QTimer::singleShot(290, this, [=](){
is_acting=false;
});
toBomb.clear();
badMove(gem);
return;
}
soundAct->play();
score += bombInfo.cnt * SCORE_PER_GEM;
if(cntStraight >= 3 || (cntStraight == 2 && bombInfo.cnt >= 4))
score += (BONUS_HAVE_STRAIGHT + BONUS_PRE_STRAIGHT * cntStraight);
scoreLabel->setNum(score);
if(cntStraight == 2 && bombInfo.cnt >= 4)
soundGood->play();
else if(cntStraight == 3)
soundExcellent->play();
else if(cntStraight >= 4)
soundAwesome->play();
else if(bombInfo.cnt == 6)
soundGood->play();
else if(bombInfo.cnt == 7)
soundExcellent->play();
else if(bombInfo.cnt == 8)
soundAwesome->play();
else if(bombInfo.cnt >= 9)
soundUnbelievable->play();
for(int i = 0; i < 10; ++i)
for(int j = 0; j < 10; ++j)
fallBoard[i][j]=0;
for(Gem* gem : toBomb){
gemBoard[gem->x()][gem->y()] = 100;
fallBoard[gem->x()][gem->y()] = -1;
for(int i = gem->y() - 1; i >= 0; --i)
if(fallBoard[gem->x()][i] != -1)
fallBoard[gem->x()][i]++;
}
for(Gem* gem : toBomb)
gem->bomb();
fall();
QTimer::singleShot(200, this, [=](){
fillBoard(allowMagic);
QTimer::singleShot(450, this, [=](){
is_acting=false;
if(isFail()){
if(resetButton->isEnabled())
reset();
else
end();
}
});
});
}
void GameWidget::fall(){
QTimer::singleShot(500, this, [=](){
soundFall->play();
});
for(int i = 0; i < 10; ++i)
for(int j = 9; j >= 0; --j){
if(fallBoard[i][j] != -1 && fallBoard[i][j] != 0 && gemBoard[i][j] != 100){
gemBoard[i][j + fallBoard[i][j]] = gemBoard[i][j];
gems[i][j]->setY(gems[i][j]->y() + fallBoard[i][j]);
gems[i][j + fallBoard[i][j]] = gems[i][j];
gemBoard[i][j] = 100;
fallAnimation(gems[i][j], fallBoard[i][j]);
}
}
}
BombInfo GameWidget::gemBomb(Gem* gem, int type, Direction dir){
bool flag=true;
BombInfo bombInfo{1, true, 0}, tmp;
toBomb.push_back(gem);
int cntStraight=0, cntHand=0;
for(auto _gem = toBomb.begin(); _gem!=toBomb.end(); ++_gem) //防止绕环
if(*_gem == gems[gem->x()-1][gem->y()])
flag=false;
if(flag && dir!=Right && gem->x()>0 && gems[gem->x()-1][gem->y()]->type()==type){
cntHand++;
tmp = gemBomb(gems[gem->x()-1][gem->y()], type, Left);
bombInfo.cnt += tmp.cnt;
if(dir != Left && dir!=Center)
bombInfo.is_straight = false;
else if(!tmp.is_straight)
bombInfo.is_straight = false;
if(dir==Center && tmp.is_straight)
cntStraight++;
}
flag = true;
for(auto _gem = toBomb.begin(); _gem!=toBomb.end(); ++_gem)
if(*_gem == gems[gem->x()+1][gem->y()])
flag=false;
if(flag && dir!=Left && gem->x()<9 && gems[gem->x()+1][gem->y()]->type()==type){
cntHand++;
tmp = gemBomb(gems[gem->x()+1][gem->y()], type, Right);
bombInfo.cnt += tmp.cnt;
if(dir != Right && dir!=Center)
bombInfo.is_straight = false;
else if(!tmp.is_straight)
bombInfo.is_straight = false;
if(dir==Center && tmp.is_straight)
cntStraight++;
}
flag = true;
for(auto _gem = toBomb.begin(); _gem!=toBomb.end(); ++_gem)
if(*_gem == gems[gem->x()][gem->y()+1])
flag = false;
if(flag && dir!=Up && gem->y()<9 && gems[gem->x()][gem->y()+1]->type() == type){
cntHand++;
tmp = gemBomb(gems[gem->x()][gem->y()+1], type, Down);
bombInfo.cnt += tmp.cnt;
if(dir != Down && dir!=Center)
bombInfo.is_straight = false;
else if(!tmp.is_straight)
bombInfo.is_straight = false;
if(dir==Center && tmp.is_straight)
cntStraight++;
}
flag = true;
for(auto _gem = toBomb.begin(); _gem!=toBomb.end(); ++_gem)
if(*_gem == gems[gem->x()][gem->y()-1])
flag = false;
if(flag && dir!=Down && gem->y()>0 && gems[gem->x()][gem->y()-1]->type() == type){
cntHand++;
tmp = gemBomb(gems[gem->x()][gem->y()-1], type, Up);
bombInfo.cnt += tmp.cnt;
if(dir != Up && dir!=Center)
bombInfo.is_straight = false;
else if(!tmp.is_straight)
bombInfo.is_straight = false;
if(dir==Center && tmp.is_straight)
cntStraight++;
}
if(cntHand > cntStraight)
bombInfo.num_straight = 0;
else
bombInfo.num_straight = cntStraight;
return bombInfo;
}
BombInfo GameWidget::magicBomb(Gem *gem){
soundMagic->play();
BombInfo bombInfo{1, true, 0};
int type;
toBomb.push_back(gem);
if(gem->y()!=0)
type=gems[gem->x()][gem->y()-1]->type();
else
type=gems[gem->x()][gem->y()+1]->type();
for(int i = 0; i < 10; ++i)
for(int j = 0; j < 10; ++j)
if(gems[i][j]->type() == type && gems[i][j] != gem){
toBomb.push_back(gems[i][j]);
bombInfo.cnt++;
}
return bombInfo;
}
void GameWidget::badMove(Gem *gem){
gemShack(gem);
soundBadmove->play();
}
void GameWidget::end(){
progressTimer->stop();
if(progressBar->value() == 99){
settlementDialog->title()->setText("时间到");
soundTimeUp->play();
}
else{
settlementDialog->title()->setText("无法消去了");
soundNoMoreMoves->play();
}
settlementDialog->lineEdit()->setText(QString::number(score));
settlementDialog->raise();
settlementDialog->show();
QFile recordFile(QApplication::applicationDirPath() + "/record");
recordFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream out(&recordFile);
out.setCodec("utf-8");
out << userName << " " << score << "\n";
recordFile.close();
sort();
}
void GameWidget::fallAnimation(Gem *gem, int h){
QPropertyAnimation* animation = new QPropertyAnimation(gem, "geometry", this);
animation->setDuration(500);
animation->setStartValue(gem->geometry());
animation->setEndValue(QRect(gem->geometry().x(), gem->geometry().y() + len*h, gem->width(), gem->height()));
animation->setEasingCurve(QEasingCurve::InQuad);
animation->start();
QTimer::singleShot(1000, this, [=](){
delete animation;
});
}
void GameWidget::gemShack(Gem *gem){
QPropertyAnimation* animation = new QPropertyAnimation(gem, "geometry", this);
animation->setDuration(300);
animation->setStartValue(gem->geometry());
animation->setKeyValueAt(0.25, QRect(gem->geometry().x() + 10, gem->geometry().y(), gem->width(), gem->height()));
animation->setKeyValueAt(0.75, QRect(gem->geometry().x() - 10, gem->geometry().y(), gem->width(), gem->height()));
animation->setEndValue(gem->geometry());
animation->setEasingCurve(QEasingCurve::InOutCubic);
animation->start();
QTimer::singleShot(1000, this, [=](){
delete animation;
});
}
void GameWidget::fillBoard(bool allowMagic){
int lack[10] = {0};
for(int i = 0; i < 10; ++i){
for(int j = 0; j < 10; ++j)
if(fallBoard[i][j] == -1)
lack[i]++;
else if(fallBoard[i][j] != 0){
lack[i] += fallBoard[i][j];
break;
}
}
for(int i = 0; i < 10; ++i)
for(int j = 0; j < lack[i]; ++j){
gems[i][lack[i]-j-1] = new Gem(randomGem(allowMagic), len, i, lack[i]-j-1, boardWidget, -lack[i]);
gemBoard[i][lack[i]-j-1] = gems[i][lack[i]-j-1]->type();
gems[i][lack[i]-j-1] -> installEventFilter(this);
connect(gems[i][lack[i]-j-1], &Gem::mouseClicked, this, &GameWidget::act);
}
QTimer::singleShot(500, this, [=](){
soundGenerate->play();
});
for(int i = 0; i < 10; ++i)
for(int j = 0; j < lack[i]; ++j){
fallAnimation(gems[i][lack[i]-j-1], lack[i]);
}
}
bool GameWidget::eventFilter(QObject *watched, QEvent *event){ //动画进行中禁用点击事件
if(watched->metaObject()->className() == QStringLiteral("Gem"))
if(is_acting && (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick))
return true;
return QMainWindow::eventFilter(watched, event);
}
void GameWidget::reset(){
for(int i = 0; i < 10; ++i)
for(int j = 0; j < 10; ++j)
gems[i][j]->bomb();
delete boardWidget;
initScene();
for(int i = 0; i < 10; ++i)
for(int j = 0; j < 10; ++j){
gemShack(gems[i][j]);
}
resetButton->setImage(":/pic/InGame/reset_disabled.png", ":/pic/InGame/reset_disabled.png", resetButton->width(), resetButton->height());
resetButton->setDisabled(true);
}
void GameWidget::setDifficulity(int d){
switch (d) {
case 1:
DIFFICULITY = 4;
break;
case 2:
DIFFICULITY = 5;
break;
case 3:
DIFFICULITY = 6;
break;
}
settingWidget->setDifficulity(DIFFICULITY-3);
reset();
resetButton->setImage(":/pic/InGame/reset.png", ":/pic/InGame/reset_hover.png", resetButton->width(), resetButton->height());
resetButton->setEnabled(true);
}
void GameWidget::returnToStart(){
settlementDialog->hide();
settingWidget->hide();
emit returnToMenu();
QTimer::singleShot(500, this, [=](){
for(int i = 0; i < 10; ++i)
for(int j = 0; j < 10; ++j)
gems[i][j]->bomb();
delete boardWidget;
initScene();
this->hide();
});
}
bool GameWidget::isFail(){
bool flag=true;
bool result=true;
for(int i = 0; i < 10 && flag; ++i)
for(int j = 0; j < 10 && flag; ++j)
if(isEliminable(gems[i][j])){
result=false;
flag=false;
}
return result;
}
bool GameWidget::isEliminable(Gem *gem){ //只判断是否有三个相邻,不记录具体个数
int cnt=1;
if(gem->x()>0 && gems[gem->x()-1][gem->y()]->type() == gem->type())
cnt++;
if(gem->x()<9 && gems[gem->x()+1][gem->y()]->type() == gem->type())
cnt++;
if(gem->y()>0 && gems[gem->x()][gem->y()-1]->type() == gem->type())
cnt++;
if(gem->y()<9 && gems[gem->x()][gem->y()+1]->type() == gem->type())
cnt++;
return cnt>=3;
}
void GameWidget::sort(){
QFile recordFile(QApplication::applicationDirPath() + "/record");
recordFile.open(QIODevice::ReadOnly);
QTextStream file(&recordFile);
file.setCodec("utf-8");
std::vector<NameScorePair> list;
NameScorePair pair;
QString userName;
list.clear();
file >> userName;
while (!file.atEnd()) {
file >> pair.name;
if(pair.name == QStringLiteral(""))
break;
file >> pair.score;
list.push_back(pair);
}
recordFile.close();
std::sort(list.begin(), list.end(), [](NameScorePair p1, NameScorePair p2){return p1.score > p2.score;}); //降序排列
recordFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
file.setDevice(&recordFile);
file.setCodec("utf-8");
file << userName + '\n';
for(NameScorePair p : list)
file << p.name << " " << p.score << '\n';
recordFile.close();
}