-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame.cpp
1763 lines (1563 loc) · 67.2 KB
/
game.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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "game.h"
#include "qevent.h"
#include "ui_game.h"
#include "stonelabel.h"
#include "globalvalue.h"
#include "mainwindow.h"
#include "pause.h"
#include "ui_pause.h"
#include "end.h"
#include <QLabel>
#include <random>
#include <cmath>
#include <vector>
#include <QGridLayout>
#include <QPropertyAnimation>
#include <QVector>
#include <QProgressDialog>
#include <QProgressBar>
#include <QMessageBox>
#include <QSequentialAnimationGroup>
#include <QPropertyAnimation>
#include <QSoundEffect>
#include <QParallelAnimationGroup>
#include "ShopWidget.h" // 引入 ShopWidget
#include<QMovie>
/*Space between Window and Labels*/
#define upSpacer 80
#define leftSpacer 100
/*The Widget of game district*/
QGridLayout* mainWidget;
StoneLabel* waitLabel;
Game* Game::gameInstance = nullptr;
int Game::jewelNum=8;
/**
* @brief generate random digit from 1 to 10
* @return random digit
*/
int genRandom(){
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, difficulty);
return dis(gen);
}
/**
* @brief Update the window by vector<vector<StoneLabel*>> stones
*/
void Game::update(){
for (int i = 0; i < Game::jewelNum; i++) {
for (int j = 0; j < Game::jewelNum; j++) {
StoneLabel* pic = stones.at(i).at(j);
pic->resize(48,48);
std::string pixStr=":/"+StoneLabel::stoneMode+std::to_string(pic->getIndex())+".png";
pic->setPixmap(QPixmap(QString::fromStdString(pixStr)).scaled(48,48));
pic->setAlignment(Qt::AlignCenter);
mainWidget->addWidget(pic, i, j);
}
}
}
Game::Game(QWidget *parent,Game::GameMode mode,Client* c)
: QWidget(parent)
, ui(new Ui::Game)
, gameMode(mode)
, client(c)
{
// gameItems = new GameItems(); // 初始化 GameItems
ui->setupUi(this);
// 循环播放背景音乐
sound = new QSoundEffect(this);
sound->setSource(QUrl("qrc:/music/background/music-2.wav")); // 使用 qrc 路径
sound->setLoopCount(QSoundEffect::Infinite);
sound->setVolume(1.0f); // 最大音量
sound->play();
// 设置鼠标-普通
setCursor(QCursor(QPixmap(":/mouse2.png")));
Game::jewelNum=8;
this->parent=parent;
this->score=0;
//this->ui->lcdNumber->set
connect(this, &Game::eliminateAgainSignal, this, &Game::onEliminateAgain);
connect(this, &Game::initEndSignal, this, &Game::initEnd);
gameTimer = new GameTimer(this);
connect(this, &Game::startGameTimer, this, &Game::onStartGameTimer);
connect(gameTimer, &GameTimer::timeUpdated, this, &Game::updateTimerDisplay);
connect(gameTimer, &GameTimer::timeExpired, this, &Game::onTimeExpired);
init();
initing=true;
progressDialog = new QProgressDialog("正在初始化中,请稍后...", "取消", 0, 0, this);
progressDialog->setWindowModality(Qt::WindowModal);
progressDialog->setValue(0);
progressDialog->show();
this->swapReturn=std::vector<int>(4,0);
if (checkFormatches()) {
eliminateMatches();
}else{
progressDialog->setValue(100);
progressDialog->hide();
emit startGameTimer();
}
//经典模式下相关显示与逻辑特殊处理
ui->passScoreLabel->hide();
ui->textBrowser->hide();
ui->levelNumLabel->hide();
ui->iceNumLabel->hide();
}
Game::Game(QWidget *parent,int levelNumber,Game::GameMode mode,Client* c)
: QWidget(parent)
, ui(new Ui::Game)
, gameMode(mode)
, levelNumber(levelNumber)
, client(c)
{
ui->setupUi(this);
Game::jewelNum=8;
this->parent=parent;
this->score=0;
//this->ui->lcdNumber->set
connect(this, &Game::eliminateAgainSignal, this, &Game::onEliminateAgain);
connect(this, &Game::initEndSignal, this, &Game::initEnd);
gameTimer = new GameTimer(this);
connect(this, &Game::startGameTimer, this, &Game::onStartGameTimer);
connect(gameTimer, &GameTimer::timeUpdated, this, &Game::updateTimerDisplay);
connect(gameTimer, &GameTimer::timeExpired, this, &Game::onTimeExpired);
init();
initing=true;
progressDialog = new QProgressDialog("正在初始化中,请稍后...", "取消", 0, 0, this);
progressDialog->setWindowModality(Qt::WindowModal);
progressDialog->setValue(0);
progressDialog->show();
this->swapReturn=std::vector<int>(4,0);
if (checkFormatches()) {
eliminateMatches();
}else{
progressDialog->setValue(100);
progressDialog->hide();
emit startGameTimer();
}
//冒险模式下相关显示与逻辑特殊处理
setWinScore(levelNumber);
setWinIceCount(levelNumber);
QString levelInfo=QString::fromStdString("关卡:"+std::to_string(levelNumber/8+1)+"-"+std::to_string((levelNumber-1)%8+1));
ui->levelNumLabel->setText(levelInfo);
ui->iceNumLabel->setStyleSheet("color:red;");
ui->iceNumLabel->setText(QString("冰块 %1/%2").arg(eliminatedIceCount).arg(winIceCount));
}
Game::~Game()
{
delete ui;
delete end;
}
Game* Game::instance(QWidget *parent, Game::GameMode mode, int levelNum,Client* c) {
if (gameInstance == nullptr) {
if (levelNum == -1) {
gameInstance = new Game(parent, mode,c);
}
else {
gameInstance = new Game(parent, levelNum, mode,c);
}
}
return gameInstance;
}
/**
* @brief init labels on the ui
*/
void Game::init(){
/**
* Test code*/
QWidget* centralWidget = new QWidget(this);
mainWidget = new QGridLayout();
mainWidget->setSpacing(0);
mainWidget->setContentsMargins(0, 0, 0, 0);
centralWidget->setLayout(mainWidget);
centralWidget->setGeometry(leftSpacer,upSpacer,384,384);
centralWidget->setParent(this);
// 更新炸弹道具数量标签
ui->bombLabel->setText(QString(" %1").arg(ShopWidget::bombCount));
// 更新横向消除道具数量标签
ui->horizonLabel->setText(QString(" %1").arg(ShopWidget::horizonCount));
// 更新竖向消除道具数量标签
ui->verticalLabel->setText(QString(" %1").arg(ShopWidget::verticalCount));
// 更新竖向消除道具数量标签
ui->hammerLabel->setText(QString(" %1").arg(ShopWidget::hammerCount));
updateHintCountDisplay(); // 显示初始提示次数
for (int row = 0; row < Game::jewelNum; row++) {
for (int col = 0; col < Game::jewelNum; col++) {
StoneLabel* imgLabel = new StoneLabel(this);
imgLabel->setFrozen(false);
imgLabel->setrow(row);
imgLabel->setcol(col);
imgLabel->resize(48,48);
imgLabel->setIndex(genRandom());
std::string pixStr=":/"+StoneLabel::stoneMode+std::to_string(imgLabel->getIndex())+".png";
imgLabel->setpix(pixStr);
imgLabel->setPixmap(QPixmap(QString::fromStdString(pixStr)).scaled(48,48));
// 根据冰冻状态设置初始外观样式
if (imgLabel->isFrozen) {
imgLabel->setStyleSheetForFrozen();
}
stones[row][col]=imgLabel;
mainWidget->addWidget(imgLabel, row, col); // 使用addWidget()来将QLabel添加到布局中
}
}
change=false;
waitLabel=nullptr;
pause = nullptr;
connect(ui->pauseButton, &QPushButton::clicked, this, &Game::on_pushButton_3_clicked);
}
/**
* @brief Game::mousePressEvent
* @param event
*/
bool Game::arePositionsAdjacent(int row1, int col1, int row2, int col2) {
return std::abs(row1 - row2) + std::abs(col1 - col2) == 1;
}
void Game::mousePressEvent(QMouseEvent *event) {
QPoint clickPoint = event->pos();
int x = clickPoint.x(), y = clickPoint.y();
if (x <= leftSpacer || y <= upSpacer || x >= leftSpacer + 8 * 48 || y >= upSpacer + 8 * 48)
return;
int col = (x - leftSpacer) / 48, row = (y - upSpacer) / 48;
/*左上*/
if(row>=1 && row<=4){
if(col>=1 && col<=4){
clickDistrict[0]++;
}
}
/*右上*/
if(row>=1 && row<=4){
if(col>=4 && col<=8){
clickDistrict[1]++;
}
}
/*左下*/
if(row>=4 && row<=8){
if(col>=1 && col<=4){
clickDistrict[2]++;
}
}
/*右下*/
if(row>=4 && row<=8){
if(col>=4 && col<=8){
clickDistrict[3]++;
}
}
if(isHammerMode){
useHammer(row, col);
return;
}
StoneLabel* curLabel = stones[row][col];
if (curLabel->isFrozen) { // 如果当前点击的棋子处于冰冻状态,直接返回,不做任何操作
return;
}
if (isBombMode) {
// 如果当前处于炸弹模式,触发炸弹效果
triggerBomb(row, col);
return;
}
if (!change) {
//isComboing = true;
waitLabel = curLabel;
waitLabel->setStyle(1);
change = true;
} else {
int row1 = waitLabel->getrow(), col1 = waitLabel->getcol();
int row2 = curLabel->getrow(), col2 = curLabel->getcol();
if (!arePositionsAdjacent(row1, col1, row2, col2)) {// 判断两次点击的位置是否相邻,若不则跟随移动
// 如果不相邻,忽视第一次点击,将此次点击视为第一次点击
waitLabel->setStyle();
//isComboing = true;
waitLabel = stones[row][col];
waitLabel->setStyle(1);
change = true;
return;
}
// 创建动画
QPropertyAnimation *animation1 = new QPropertyAnimation(stones[row1][col1], "pos");
QPropertyAnimation *animation2 = new QPropertyAnimation(stones[row2][col2], "pos");
// 设置动画时长和目标位置
animation1->setDuration(300); // 动画持续时间300ms
animation2->setDuration(300);
// 计算目标位置
QPoint targetPos1 = stones[row2][col2]->pos();
QPoint targetPos2 = stones[row1][col1]->pos();
animation1->setEndValue(targetPos1);
animation2->setEndValue(targetPos2);
// 启动动画
animation1->start(QAbstractAnimation::DeleteWhenStopped);
animation2->start(QAbstractAnimation::DeleteWhenStopped);
// 动画结束后交换
connect(animation1, &QPropertyAnimation::finished, [=]() {
// 交换数据
waitLabel->setStyle();
std::swap(stones[row1][col1], stones[row2][col2]);
stones[row1][col1]->setrow(row1);
stones[row1][col1]->setcol(col1);
stones[row2][col2]->setrow(row2);
stones[row2][col2]->setcol(col2);
update();
if (!hasStartedScoring) // 如果还未开始计分,在这里标记为开始计分
{
hasStartedScoring = true;
}
if (checkFormatches()) {
// 标记被选中且发生消除的棋子
stones[row1][col1]->setSelectedAndEliminated(true);
stones[row2][col2]->setSelectedAndEliminated(true);
eliminateMatches();
} else {
// 新增判断逻辑,检查交换的两个棋子是否分别为横劈和竖劈
if(stones[row1][col1]->lineKiller == 1 && stones[row2][col2]->lineKiller == 1){//两个横劈交换,劈一(两)行
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
stones[row1][temp_Col]->setMatched(true);
}
if(row1 != row2){
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
stones[row2][temp_Col]->setMatched(true);
}
}
eliminateMatches();
return;
}
else if(stones[row1][col1]->lineKiller == 2 && stones[row2][col2]->lineKiller == 2){//两个竖劈交换,劈一(两)列
for (int temp_row = 0; temp_row < 8; ++temp_row) {
stones[temp_row][col1]->setMatched(true);
}
if(col1 != col2){
for (int temp_row = 0; temp_row < 8; ++temp_row) {
stones[temp_row][col2]->setMatched(true);
}
}
eliminateMatches();
return;
}
else if ((stones[row1][col1]->lineKiller == 1 && stones[row2][col2]->lineKiller == 2) ||
(stones[row1][col1]->lineKiller == 2 && stones[row2][col2]->lineKiller == 1)) {//横竖交换,消十字
// 消除所在行和所在列
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
stones[row2][temp_Col]->setMatched(true);
}
for (int temp_row = 0; temp_row < 8; ++temp_row) {
stones[temp_row][col2]->setMatched(true);
}
eliminateMatches();
return;
}
else if((stones[row1][col1]->isBombKiller && stones[row2][col2]->lineKiller == 1) ||
(stones[row1][col1]->lineKiller == 1 && stones[row2][col2]->isBombKiller)){//爆炸+横劈消三行
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
for (int temp_row = row2-1; temp_row <= row2+1 && temp_row < 8; ++temp_row) {
stones[temp_row][temp_Col]->setMatched(true);
}
}
eliminateMatches();
return;
}
else if((stones[row1][col1]->isBombKiller && stones[row2][col2]->lineKiller == 2) ||
(stones[row1][col1]->lineKiller == 2 && stones[row2][col2]->isBombKiller)){//爆炸+竖劈消三列
for (int temp_Col = col2-1; temp_Col <= col2+1 && temp_Col < 8; ++temp_Col) {
for (int temp_row = 0; temp_row < 8; ++temp_row) {
stones[temp_row][temp_Col]->setMatched(true);
}
}
eliminateMatches();
return;
}
else if(stones[row1][col1]->isBombKiller && stones[row2][col2]->isBombKiller){//两个bombKiller交换炸全屏
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
for (int temp_row = 0; temp_row < 8; ++temp_row) {
stones[temp_row][temp_Col]->setMatched(true);
}
}
eliminateMatches();
return;
}
else if (stones[row1][col1]->isKing && stones[row2][col2]->lineKiller == 1) {//king+横劈,全部与king同类型的棋子全变横劈
stones[row1][col1]->setMatched(true);
stones[row2][col2]->setMatched(true);
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
for (int temp_row = 0; temp_row < 8; ++temp_row) {
if (!stones[temp_row][temp_Col]->isFrozen && stones[temp_row][temp_Col]->getIndex() == stones[row1][col1]->getIndex()) {
stones[temp_row][temp_Col]->setLineKiller(1);
stones[temp_row][temp_Col]->setStyleSheetForRowKiller();
stones[temp_row][temp_Col]->setOriginalStyleSheet("background-color: yellow;");
stones[temp_row][temp_Col]->setKing(false);
stones[temp_row][temp_Col]->setBomb(false);
}
}
}
eliminateMatches();
return;
}
else if (stones[row1][col1]->lineKiller == 1 && stones[row2][col2]->isKing) {
stones[row1][col1]->setMatched(true);
stones[row2][col2]->setMatched(true);
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
for (int temp_row = 0; temp_row < 8; ++temp_row) {
if (!stones[temp_row][temp_Col]->isFrozen && stones[temp_row][temp_Col]->getIndex() == stones[row2][col2]->getIndex()) {
stones[temp_row][temp_Col]->setLineKiller(1);
stones[temp_row][temp_Col]->setStyleSheetForRowKiller();
stones[temp_row][temp_Col]->setOriginalStyleSheet("background-color: yellow;");
stones[temp_row][temp_Col]->setKing(false);
stones[temp_row][temp_Col]->setBomb(false);
}
}
}
eliminateMatches();
return;
}
else if (stones[row1][col1]->isKing && stones[row2][col2]->lineKiller == 2) {//king+竖劈,全部与king同类型的棋子全变竖劈
stones[row1][col1]->setMatched(true);
stones[row2][col2]->setMatched(true);
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
for (int temp_row = 0; temp_row < 8; ++temp_row) {
if (!stones[temp_row][temp_Col]->isFrozen && stones[temp_row][temp_Col]->getIndex() == stones[row1][col1]->getIndex()) {
stones[temp_row][temp_Col]->setLineKiller(2);
stones[temp_row][temp_Col]->setStyleSheetForColKiller();
stones[temp_row][temp_Col]->setOriginalStyleSheet("background-color: green;");
stones[temp_row][temp_Col]->setKing(false);
stones[temp_row][temp_Col]->setBomb(false);
}
}
}
eliminateMatches();
return;
}
else if (stones[row1][col1]->lineKiller == 2 && stones[row2][col2]->isKing) {
stones[row1][col1]->setMatched(true);
stones[row2][col2]->setMatched(true);
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
for (int temp_row = 0; temp_row < 8; ++temp_row) {
if (!stones[temp_row][temp_Col]->isFrozen && stones[temp_row][temp_Col]->getIndex() == stones[row2][col2]->getIndex()) {
stones[temp_row][temp_Col]->setLineKiller(2);
stones[temp_row][temp_Col]->setStyleSheetForColKiller();
stones[temp_row][temp_Col]->setOriginalStyleSheet("background-color: green;");
stones[temp_row][temp_Col]->setKing(false);
stones[temp_row][temp_Col]->setBomb(false);
}
}
}
eliminateMatches();
return;
}
else if (stones[row1][col1]->isKing && stones[row2][col2]->isBombKiller) {
stones[row1][col1]->setMatched(true);
stones[row2][col2]->setMatched(true);
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
for (int temp_row = 0; temp_row < 8; ++temp_row) {
if (!stones[temp_row][temp_Col]->isFrozen && stones[temp_row][temp_Col]->getIndex() == stones[row1][col1]->getIndex()) {
stones[temp_row][temp_Col]->setLineKiller(0);
stones[temp_row][temp_Col]->setKing(false);
stones[temp_row][temp_Col]->setBomb(true);
stones[temp_row][temp_Col]->setStyleSheetForBomb();
stones[temp_row][temp_Col]->setOriginalStyleSheet("background-color: pink;");
}
}
}
eliminateMatches();
return;
}
else if (stones[row1][col1]->isBombKiller && stones[row2][col2]->isKing) {
stones[row1][col1]->setMatched(true);
stones[row2][col2]->setMatched(true);
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
for (int temp_row = 0; temp_row < 8; ++temp_row) {
if (!stones[temp_row][temp_Col]->isFrozen && stones[temp_row][temp_Col]->getIndex() == stones[row2][col2]->getIndex()) {
stones[temp_row][temp_Col]->setLineKiller(0);
stones[temp_row][temp_Col]->setKing(false);
stones[temp_row][temp_Col]->setBomb(true);
stones[temp_row][temp_Col]->setStyleSheetForBomb();
stones[temp_row][temp_Col]->setOriginalStyleSheet("background-color: pink;");
}
}
}
eliminateMatches();
return;
}
else if(stones[row1][col]->isKing && stones[row2][col2]->isKing) {
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
for (int temp_row = 0; temp_row < 8; ++temp_row) {
stones[temp_row][temp_Col]->setMatched(true);
}
}
eliminateMatches();
return;
}
//Animation Shaked
QPoint originalPos = stones[row2][col2]->pos();
QPropertyAnimation *animationShake = new QPropertyAnimation(stones[row2][col2], "pos");
animationShake->setDuration(300);
QList<QPair<double, QVariant>> keyframes;
keyframes.append(qMakePair(0.0, QVariant::fromValue(originalPos + QPoint(8, 0)))); // 向右移动
keyframes.append(qMakePair(0.1, QVariant::fromValue(originalPos - QPoint(8, 0)))); // 向左移动
keyframes.append(qMakePair(0.2, QVariant::fromValue(originalPos + QPoint(8, 0)))); // 向右移动
keyframes.append(qMakePair(0.3, QVariant::fromValue(originalPos - QPoint(8, 0)))); // 向左移动
keyframes.append(qMakePair(0.4, QVariant::fromValue(originalPos))); // 恢复原位置
// 设置关键帧
animationShake->setKeyValues(keyframes);
animationShake->start(QAbstractAnimation::DeleteWhenStopped);
//Animation Shaked2
QPoint originalPos2 = stones[row1][col1]->pos();
QPropertyAnimation *animationShake2 = new QPropertyAnimation(stones[row1][col1], "pos");
animationShake2->setDuration(300);
QList<QPair<double, QVariant>> keyframes2;
keyframes2.append(qMakePair(0.0, QVariant::fromValue(originalPos2 + QPoint(8, 0)))); // 向右移动
keyframes2.append(qMakePair(0.1, QVariant::fromValue(originalPos2 - QPoint(8, 0)))); // 向左移动
keyframes2.append(qMakePair(0.2, QVariant::fromValue(originalPos2 + QPoint(8, 0)))); // 向右移动
keyframes2.append(qMakePair(0.3, QVariant::fromValue(originalPos2 - QPoint(8, 0)))); // 向左移动
keyframes2.append(qMakePair(0.4, QVariant::fromValue(originalPos2))); // 恢复原位置
// 设置关键帧
animationShake2->setKeyValues(keyframes2);
animationShake2->start(QAbstractAnimation::DeleteWhenStopped);
connect(animationShake, &QPropertyAnimation::finished, [=](){
QPropertyAnimation *reverseAnim1 = new QPropertyAnimation(stones[row1][col1], "pos");
QPropertyAnimation *reverseAnim2 = new QPropertyAnimation(stones[row2][col2], "pos");
reverseAnim1->setDuration(200);
reverseAnim2->setDuration(200);
// 计算回退的目标位置
reverseAnim1->setEndValue(targetPos1); // 原位置
reverseAnim2->setEndValue(targetPos2); // 原位置
reverseAnim1->start(QAbstractAnimation::DeleteWhenStopped);
reverseAnim2->start(QAbstractAnimation::DeleteWhenStopped);
// 动画完成后恢复交换
connect(reverseAnim1, &QPropertyAnimation::finished, [=]() {
// 换回去
std::swap(stones[row1][col1], stones[row2][col2]);
stones[row1][col1]->setrow(row1);
stones[row1][col1]->setcol(col1);
stones[row2][col2]->setrow(row2);
stones[row2][col2]->setcol(col2);
update();
});
});
}
change = false;
});
}
if(horizon){//横劈道具
curLabel->setStyle(0);
horizondelete(row);
horizon=false;
change=false;
}
if(vertical){//竖劈道具
curLabel->setStyle(1);
verticaldelete(col);
vertical=false;
change=false;
}
}
//判断
bool Game::checkFormatches(){
bool hasMatch = false;
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 6; ++col) { // 检查每行的前6列
if (stones[row][col]->getIndex() == stones[row][col + 1]->getIndex() &&
stones[row][col]->getIndex() == stones[row][col + 2]->getIndex()) {
hasMatch = true;
// 标记这些棋子为待消除
stones[row][col]->setMatched(true);
stones[row][col + 1]->setMatched(true);
stones[row][col + 2]->setMatched(true);
}
}
}
// 纵向检查
for (int col = 0; col < 8; ++col) {
for (int row = 0; row < 6; ++row) { // 检查每列的前6行
if (stones[row][col]->getIndex() == stones[row + 1][col]->getIndex() &&
stones[row][col]->getIndex() == stones[row + 2][col]->getIndex()) {
hasMatch = true;
// 标记这些棋子为待消除
stones[row][col]->setMatched(true);
stones[row + 1][col]->setMatched(true);
stones[row + 2][col]->setMatched(true);
}
}
}
for (int col = 0; col < 8; ++col) {
for (int row = 0; row < 8; ++row) {
// 更新匹配数
stones[row][col]->setColMatchNum(colCheckMatch(row, col));
stones[row][col]->setRowMatchNum(rowCheckMatch(row, col));
stones[row][col]->setMatchNum(checkMatch(stones[row][col]->rowMatchNum,stones[row][col]->colMatchNum));
// 更新样式状态
stones[row][col]->setOriginalStyleSheet(stones[row][col]->styleSheet());
}
}
return hasMatch;
}
void Game::eliminateMatches() {
for (int col = 0; col < 8; ++col) {
for (int row = 0; row < 8; ++row) {
// 更新匹配数
stones[row][col]->setColMatchNum(colCheckMatch(row, col));
stones[row][col]->setRowMatchNum(rowCheckMatch(row, col));
stones[row][col]->setMatchNum(checkMatch(stones[row][col]->rowMatchNum,stones[row][col]->colMatchNum));
// 更新样式状态
stones[row][col]->setOriginalStyleSheet(stones[row][col]->styleSheet());
}
}
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
if (stones[row][col] != nullptr && stones[row][col]->isMatched() && !stones[row][col]->isFrozen) {//未冰冻的棋子,直接消除即可
if(stones[row][col]->isSelectedAndEliminated){//由鼠标点击产生的消除
if(stones[row][col]->colMatchNum == 4 && stones[row][col]->rowMatchNum < 3){//四个纵的合成横劈
stones[row][col]->setLineKiller(1);
stones[row][col]->setMatched(false);
stones[row][col]->setStyleSheetForRowKiller();
}else if(stones[row][col]->rowMatchNum == 4){//四个横的合成竖劈
stones[row][col]->setLineKiller(2);
stones[row][col]->setMatched(false);
stones[row][col]->setStyleSheetForColKiller();
}
else if(stones[row][col]->colMatchNum >= 3 && stones[row][col]->rowMatchNum >= 3 && stones[row][col]->colMatchNum <= 4 && stones[row][col]->rowMatchNum <= 4) {
stones[row][col]->setBomb(true);
stones[row][col]->setMatched(false);
stones[row][col]->setStyleSheetForBomb();
}
else if(stones[row][col]->matchNum == 5){
stones[row][col]->setKing(true);
stones[row][col]->setMatched(false);
stones[row][col]->setStyleSheetForKing();
}
else{
eliminateStone(stones,stones[row][col],row,col);
}
}
else {//非鼠标直接消除 或 鼠标点击了普通三消 直接删除即可
eliminateStone(stones,stones[row][col],row,col);
}
}
else if (stones[row][col] != nullptr && stones[row][col]->isMatched() && stones[row][col]->isFrozen) { // 如果是冰冻棋子,清除匹配标记并恢复正常外观
eliminateStone(stones,stones[row][col],row,col);
}
}
}
if (hasStartedScoring) // 根据计分标记判断是否计分
{
// 根据消除的棋子个数计算得分,计分规则见calGainScore()
score += calGainScore(eliminatedCount,iceKilledNum); // 将本次得分累加到总积分中
// 更新冰块数量显示
if (gameMode == GameMode::ADVENTURE_MODE) {
eliminatedIceCount += iceKilledNum;// 将本次消除冰块数累加到总消除冰块数中
QString iceNumberText = QString("冰块 %1/%2").arg(eliminatedIceCount).arg(winIceCount);
ui->iceNumLabel->setText(iceNumberText);
if(eliminatedIceCount >= winIceCount){
ui->iceNumLabel->setStyleSheet("color:green;");
}
}
if (!progressDialog->isVisible()) {
QSoundEffect* soundEffect;
switch(eliminatedCount){
case 3:{
soundEffect = new QSoundEffect(this);
soundEffect->setSource(QUrl::fromLocalFile(":/music/eliminate/triple.wav"));
soundEffect->setLoopCount(1); // 只播放一次
soundEffect->setVolume(volume);
break;
}
case 4:{
soundEffect = new QSoundEffect(this);
soundEffect->setSource(QUrl::fromLocalFile(":/music/eliminate/quadruple.wav"));
soundEffect->setLoopCount(1); // 只播放一次
soundEffect->setVolume(volume);
break;
}
default:{
soundEffect = new QSoundEffect(this);
soundEffect->setSource(QUrl::fromLocalFile(":/music/eliminate/penta.wav"));
soundEffect->setLoopCount(1); // 只播放一次
soundEffect->setVolume(volume);
break;
}
}
soundEffect->play();//播放消除音效
}
eliminatedCount = 0; // 用于记录本次消除的棋子个数
iceKilledNum = 0; // 用于记录本次消除的冰块个数
}
// 更新积分显示
ui->lcdNumber->display(score);
/*if (!progressDialog->isVisible()) {
QSoundEffect* soundEffect;
std::cout<<eliminatedCount<<std::endl;
switch(eliminatedCount){
case 3:{
soundEffect = new QSoundEffect(this);
soundEffect->setSource(QUrl::fromLocalFile(":/music/eliminate/triple.wav"));
soundEffect->setLoopCount(1); // 只播放一次
soundEffect->setVolume(volume);
break;
}
case 4:{
soundEffect = new QSoundEffect(this);
soundEffect->setSource(QUrl::fromLocalFile(":/music/eliminate/quadruple.wav"));
soundEffect->setLoopCount(1); // 只播放一次
soundEffect->setVolume(volume);
break;
}
default:{
soundEffect = new QSoundEffect(this);
soundEffect->setSource(QUrl::fromLocalFile(":/music/eliminate/penta.wav"));
soundEffect->setLoopCount(1); // 只播放一次
soundEffect->setVolume(volume);
break;
}
}
soundEffect->play();//播放消除音效
}*/
dropStones();// 执行棋子下落逻辑,用于填补因消除产生的空位
resetMatchedFlags();// 重置所有棋子的匹配标记,为下一轮检测做准备
}
void Game::eliminateStone(std::vector<std::vector<StoneLabel*>>& stones, StoneLabel* stoneLabel, int row, int col){
if(stoneLabel == nullptr) { return; }
if(stoneLabel->isFrozen){//冰冻棋子
stoneLabel->setMatched(false);
stoneLabel->setFrozen(false);
stoneLabel->setStyleSheetForNormal();
iceKilledNum++;
}
else if(stoneLabel->lineKiller == 1){//如果是横劈
//使用掉lineKiller效果
stoneLabel->setLineKiller(0);
stoneLabel->setStyleSheetForNormal();
for (int temp_Col = 0; temp_Col < 8; ++temp_Col) {
eliminateStone(stones,stones[row][temp_Col],row,temp_Col);
}
}
else if(stoneLabel->lineKiller == 2){//如果是竖劈
//使用掉lineKiller效果
stoneLabel->setLineKiller(0);
stoneLabel->setStyleSheetForNormal();
// 执行竖劈逻辑,消除整列
for (int temp_row = 0; temp_row < 8; ++temp_row) {
eliminateStone(stones,stones[temp_row][col],temp_row,col);
}
}
else if(stoneLabel->isBombKiller){//如果是炸弹
stoneLabel->setBomb(false);
stoneLabel->setStyleSheetForNormal();
// 执行炸弹逻辑,消除5*5;
for (int temp_row = row-2; temp_row <= row+2; ++temp_row) {
for (int temp_col = col-2; temp_col <= col+2; ++temp_col) {
if(temp_row < 8 && temp_row > 0 && temp_col < 8 && temp_col > 0){
eliminateStone(stones,stones[temp_row][temp_col],temp_row,temp_col);
}
}
}
}
else if(stoneLabel->isKing){//如果是王棋子
stoneLabel->setKing(false);
stoneLabel->setStyleSheetForNormal();
// 执行王逻辑,消除场上所有同类型棋子;
for (int temp_row = 0; temp_row < 8; ++temp_row) {
for (int temp_col = 0; temp_col < 8; ++temp_col) {
if (stones[temp_row][temp_col] != nullptr && stones[temp_row][temp_col]->getIndex() == stoneLabel->getIndex() && temp_row != stoneLabel->getrow() && temp_col != stoneLabel->getcol()) {
eliminateStone(stones,stones[temp_row][temp_col],temp_row,temp_col);
}
}
}
eliminateStone(stones,stones[row][col],row,col);
}
else{//普通棋子
stoneLabel->setMatched(false);
delete stones[row][col];
stones[row][col] = nullptr;
eliminatedCount++;
}
}
//生成新子
//生成一个新子
void Game::generateNewStone(int row, int col){
// 初始化
StoneLabel* imgLabel = new StoneLabel(this);
imgLabel->setrow(row);
imgLabel->setcol(col);
imgLabel->resize(48, 48);
imgLabel->setIndex(genRandom());
std::string pixStr = ":/" + StoneLabel::stoneMode + std::to_string(imgLabel->getIndex()) + ".png";
imgLabel->setPixmap(QPixmap(QString::fromStdString(pixStr)).scaled(48, 48));
imgLabel->setpix(pixStr);
// 显示添加
// mainWidget->addWidget(imgLabel, row, col);
if(gameMode == Game::GameMode::ADVENTURE_MODE){//只有冒险模式才生成冰块
if (genRandom() < 2) {
imgLabel->setFrozen(true);
imgLabel->setStyleSheetForFrozen(); // 设置为白色填充样式,代表冰冻状态
} else {
imgLabel->setFrozen(false);
}
}else{
imgLabel->setFrozen(false);
}
// 逻辑添加
stones[row][col] = imgLabel;
}
void Game::onEliminateAgain(){
{
if(checkFormatches()){
isComboing = true;
eliminateMatches();
}else{
isComboing = false;
int row1=swapReturn[0],col1=swapReturn[1],row2=swapReturn[2],col2=swapReturn[3];
std::cout<<"row1:"<<row1<<",row2:"<<row2<<",col1:"<<col1<<",col2:"<<col2<<std::endl;
std::swap(stones[row1][col1], stones[row2][col2]);
stones[row1][col1]->setrow(row1);
stones[row1][col1]->setcol(col1);
stones[row2][col2]->setrow(row2);
stones[row2][col2]->setcol(col2);
if(gameMode == GameMode::ADVENTURE_MODE){//冒险模式
if (checkAdventureWin() && !isComboing) {//胜利
// 显示结束界面并提示闯关成功
end = new End(this,client);
connect(end, &End::nextButtonClicked, this, &Game::onNextButtonClicked);
end->showAdventureWinUI();
// 停止游戏相关的定时器等操作
resetGameState();
return;
}
}
emit initEndSignal();
}
}
}
void Game::initEnd(){
std::cout<<"initEnd"<<std::endl;
this->progressDialog->setValue(100);
this->progressDialog->hide();
emit startGameTimer();
}
//遍历空白位置,生成全部新子
void Game::creatstones(){
int sum=0;
int tmpRow=-1;
for(int row=7;row>=0;--row){// 行从下到上
for(int col=7;col>=0;--col){//列从下到上
if(stones[row][col]==nullptr && tmpRow!=row){
sum++;
tmpRow=row;
}
}
}
for(int row=7;row>=0;--row){// 行从下到上
for(int col=7;col>=0;--col){//列从下到上
if(stones[row][col]==nullptr){
generateNewStone(row,col);
int time=sum*200;
dropLabel(stones[row][col],col*48,(row-sum)*48,col*48,row*48,time);
}
}
}
update();
emit eliminateAgainSignal();
}
//棋子下落动画
//duration should set to x/v
void Game::dropLabel(StoneLabel* stoneLabel, int startX,int startY,int targetX, int targetY, int duration) {
if(stoneLabel == nullptr){
return;
}
this->initing=true;
duration=1000*(targetY-startY)/192;
QPropertyAnimation* animation = new QPropertyAnimation(stoneLabel, "pos");
animation->setStartValue(QPoint(startX,startY));// 起始位置NO
animation->setEndValue(QPoint(targetX, targetY)); // 目标位置
animation->setDuration(duration); // 持续时间
// 连接动画完成的信号
connect(animation, &QPropertyAnimation::finished, this, &Game::onDropAnimationFinished);
// 启动动画
animation->start(QAbstractAnimation::DeleteWhenStopped); // 动画完成后自动删除
}
//下落并生成新子
void Game::dropStones() {
animationsLeft = 0; // 重置动画计数器
bool drop=false;
// 让上面的棋子填补下方空位
for (int col = 0; col < 8; ++col) {
// 从底部开始检查每一列
for (int row = 7; row >= 0; --row) {
if (stones[row][col] == nullptr) { // 如果当前这个位置为空
// 查找上面第一个非空的位置
int emptyRow = row;//空位置
for (int checkRow = row - 1; checkRow >= 0; --checkRow) {
if (stones[checkRow][col] != nullptr) {
// 这个位置有棋子,交换
// 计算目标位置的坐标
int targetX = col * 48;
int targetY = emptyRow * 48;
stones[checkRow][col]->targetX=targetX;
stones[checkRow][col]->targetY=targetY;
dropLabel(stones[checkRow][col],col*48,checkRow*48,targetX,targetY,1000);//下落动画
drop=true;
stones[emptyRow][col] = stones[checkRow][col];//逻辑交换
stones[emptyRow][col]->setrow(emptyRow);
stones[emptyRow][col]->setcol(col);
stones[checkRow][col] = nullptr; // 清空原位置
// 更新动画计数器
animationsLeft++;
break;
}
}
}
}
}
// 若无子下落,遍历前三行,找到空位生成新子
if(!drop){
creatstones();
}
resetMatchedFlags();
}
void Game::resetMatchedFlags(){
for (int row = 0; row < 8; ++row) {
for (int col = 0; col < 8; ++col) {
if (stones[row][col] != nullptr) {
stones[row][col]->setMatched(false);
stones[row][col]->isSelectedAndEliminated = false; // 重置标记
}
}
}
}
// 设置游戏模式的函数实现
void Game::setGameMode(GameMode mode)
{
gameMode = mode;
}