-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgamewidget.cpp
1560 lines (1399 loc) · 53.5 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
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 "gamewidget.h"
#include "ui_gamewidget.h"
GameWidget::GameWidget(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::GameWidget)
{
ui->setupUi(this);
}
void GameWidget::setupScene(int i){
gameOver=false;
is_paused=false;
exitMagic=true;
score=0;
trans=0;
QSound* wel=new QSound(":/music/effect/Welcome.wav");
wel->play();
scoreTextLbl=new QLabel(this);
scoreTextLbl->setGeometry(360,130,150,50);
scoreTextLbl->setAlignment(Qt::AlignCenter);
scoreTextLbl->setFont(QFont("Euclid",40,QFont::Normal));
scoreTextLbl->setStyleSheet("QLabel{color:white;}");
scoreTextLbl->setVisible(true);
//禁用最大化按钮、设置窗口大小固定
this->setWindowFlags(windowFlags()& ~Qt::WindowMaximizeButtonHint);
this->setFixedSize(this->width(),this->height());
//全屏
QWidget::showFullScreen();
//循环播放背景音乐
sound=new QSoundEffect(this);
sound->setSource(QUrl::fromLocalFile(QCoreApplication::applicationDirPath()+"/music/background/music-2.wav"));
sound->setLoopCount(QSoundEffect::Infinite);
sound->play();
//窗口基本设置
setWindowFlag(Qt::Window); //设置为独立窗口
setWindowTitle("Bejeweled");
setWindowIcon(QIcon("://picture/app_icon.ico"));
//设置窗口背景黑色
QPalette palette(this->palette());
palette.setColor(QPalette::Background, Qt::black);
this->setPalette(palette);
//背景图片 棋盘框 分数框
setAdaptedImg(":/picture/backdrop00.jpg",ui->oriBkLbl);
setAdaptedImg(":/picture/frame.png",ui->borderLbl);
setAdaptedImg(":/picture/scorepod.png",ui->scoreLbl);
//设置变红的四周提示灯标签
redBorder=new QLabel(this);
redBorder->setGeometry(610, 2, 1055, 1073);
setAdaptedImg(":/picture/frame_red.png",redBorder);
redBorder->setAttribute(Qt::WA_TransparentForMouseEvents);
//辅助label,不用管
ui->menuLbl->setVisible(false);
ui->hintLbl->setVisible(false);
ui->pauseLbl->setVisible(false);
ui->reSetBtn->setVisible(false);
menuButton = new HoverButton(this);
menuButton->setGeometry(ui->menuLbl->geometry());
menuButton->setImage(":/picture/3balls/ball1.png",nullptr,ui->menuLbl->width(),ui->menuLbl->height(),ui->menuLbl);
hintButton = new HoverButton(this);
hintButton->setGeometry(ui->hintLbl->geometry());
hintButton->setImage(":/picture/3balls/ball2.png",nullptr,ui->hintLbl->width(),ui->hintLbl->height(),ui->hintLbl);
pauseButton = new HoverButton(this);
pauseButton->setGeometry(ui->pauseLbl->geometry());
pauseButton->setImage(":/picture/3balls/ball3.png",nullptr,ui->pauseLbl->width(),ui->pauseLbl->height(),ui->pauseLbl);
reSetButton = new HoverButton(this);
reSetButton->setGeometry(ui->reSetBtn->geometry());
reSetButton->setImage(":/picture/Settingpage/dialogbutton.png",nullptr,ui->reSetBtn->width(),ui->reSetBtn->height(),ui->reSetBtn);
//语言切换
if(i==1){
menuButton->showContent("MENU",20);
menuButton->show();
hintButton->showContent("HINT",30);
hintButton->show();
pauseButton->showContent("PAUSE",20);
pauseButton->show();
reSetButton->showContent("RESET",15);
reSetButton->show();
}
if(i==0){
menuButton->showContent("菜单",20);
menuButton->show();
hintButton->showContent("提示",30);
hintButton->show();
pauseButton->showContent("暂停",20);
pauseButton->show();
reSetButton->showContent("重置",15);
reSetButton->show();
}
//设置鼠标-普通
setCursor(QCursor(QPixmap("://picture/mouse1.png")));
//进度条
int totalTime=8000;
progressBar = new MyProBar(this);
progressBar->setRange(0,totalTime);
progressBar->setValue(totalTime);
progressBar->setTextVisible(false);
progressBar->show();
//动画
//棋盘
QPropertyAnimation* anim1 = new QPropertyAnimation(ui->borderLbl, "geometry");
anim1->setDuration(500);
anim1->setStartValue(QRect(610+1055, 2, 1055, 1073));
anim1->setEndValue(QRect(610, 2, 1055, 1073));
anim1->setEasingCurve(QEasingCurve::OutQuad);
//分数版
QPropertyAnimation* anim2 = new QPropertyAnimation(ui->scoreLbl, "geometry");
anim2->setDuration(500);
anim2->setStartValue(QRect(270, 0, 327, 178));
anim2->setEndValue(QRect(270, 80, 327, 178));
anim2->setEasingCurve(QEasingCurve::InQuad);
//进度条
QPropertyAnimation* anim3 = new QPropertyAnimation(progressBar,"geometry");
anim3->setDuration(500);
anim3->setStartValue(QRect(653+1055,1010,982,47));
anim3->setEndValue(QRect(653,1010,982,47));
anim3->setEasingCurve(QEasingCurve::OutQuad);
//菜单栏
QPropertyAnimation* anim4 = new QPropertyAnimation(menuButton,"geometry");
anim4->setDuration(500);
anim4->setStartValue(QRect(menuButton->x(),menuButton->y()+1000,menuButton->width(),menuButton->height()));
anim4->setEndValue(QRect(menuButton->x(),menuButton->y(),menuButton->width(),menuButton->height()));
anim4->setEasingCurve((QEasingCurve::OutQuad));
QPropertyAnimation* anim5 = new QPropertyAnimation(hintButton,"geometry");
anim5->setDuration(500);
anim5->setStartValue(QRect(hintButton->x(),hintButton->y()+1000,hintButton->width(),hintButton->height()));
anim5->setEndValue(QRect(hintButton->x(),hintButton->y(),hintButton->width(),hintButton->height()));
anim5->setEasingCurve((QEasingCurve::OutQuad));
QPropertyAnimation* anim6 = new QPropertyAnimation(pauseButton,"geometry");
anim6->setDuration(500);
anim6->setStartValue(QRect(pauseButton->x(),pauseButton->y()+1000,pauseButton->width(),pauseButton->height()));
anim6->setEndValue(QRect(pauseButton->x(),pauseButton->y(),pauseButton->width(),pauseButton->height()));
anim6->setEasingCurve((QEasingCurve::OutQuad));
QPropertyAnimation* anim7 = new QPropertyAnimation(reSetButton,"geometry");
anim7->setDuration(500);
anim7->setStartValue(QRect(reSetButton->x(),reSetButton->y()+1000,reSetButton->width(),reSetButton->height()));
anim7->setEndValue(QRect(reSetButton->x(),reSetButton->y(),reSetButton->width(),reSetButton->height()));
anim6->setEasingCurve((QEasingCurve::OutQuad));
QParallelAnimationGroup *group = new QParallelAnimationGroup;
group->addAnimation(anim1);
group->addAnimation(anim2);
group->addAnimation(anim3);
group->addAnimation(anim4);
group->addAnimation(anim5);
group->addAnimation(anim6);
group->addAnimation(anim7);
group->start(QAbstractAnimation::DeleteWhenStopped);
Sleep(600);
startGame();
//开始记时
progressTimer = new QTimer(this);
progressTimer->setInterval(15);
progressTimer->start();
redBorderTimer=new QTimer(this);
redBorderTimer->setInterval(500);
timeoutTimer=new QTimer(this);
timeoutTimer->setInterval(30);
//timeoutTimer->start();
connect(redBorderTimer, &QTimer::timeout, [=](){
if(redBordershow==0){
redBorder->show();
redBordershow=1;
}else{
redBorder->hide();
redBordershow=0;
}
});
//设置超时标签
outLabel=new QLabel(this);
connect(timeoutTimer, &QTimer::timeout, [=](){
if(trans<=1)
trans=trans+0.01;
else {
is_acting=false;
return;
}
outLabel->setGeometry(837,388,600,300);
outLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
QPixmap input(":/picture/time_out.png");
QImage image(input.size(), QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
QPainter p(&image);
p.setOpacity(trans);
p.drawPixmap(0, 0, input);
p.end();
QPixmap output = QPixmap::fromImage(image);
outLabel->setPixmap(output);
outLabel->show();
outLabel->setParent(this);
});
connect(progressTimer, &QTimer::timeout, [=](){
if(!is_paused){
if(progressBar->value() == 0){
gameOver=true;
forbidAll(true);
//allFallOut();
//计时结束
if(!timeoutTimer->isActive()){
timeoutTimer->start();
if(effect)
delete effect;
effect=new QSound(":/music/effect/Time_Up.wav");
effect->play();
//client->update(score);
}
if(redBorderTimer->isActive()){
redBorderTimer->stop();
redBorder->show();
redBordershow=1;
}
}
else{
progressBar->setValue(progressBar->value()-1);
if(progressBar->value()/static_cast<double>(totalTime)<=0.25){
if(!redBorderTimer->isActive()){
redBorderTimer->start();
}
}
}
}
});
connect(reSetButton,&HoverButton::clicked,[=]{
if(is_acting||gameOver)
return;
reSetBoard();
});
connect(menuButton, &HoverButton::clicked, [=](){
if(is_acting)
return;
client->update(score);
sound->stop();
this->hide();
showStartPage();
if(timeoutTimer)
delete timeoutTimer;
if(outLabel)
delete outLabel;
if(redBorderTimer)
delete redBorderTimer;
if(redBorder)
delete redBorder;
if(reSetButton)
delete reSetButton;
if(menuButton)
delete menuButton;
if(hintButton)
delete hintButton;
if(pauseBKLbl)
delete pauseBKLbl;
if(pauseBKgif){
pauseBKgif->stop();
delete pauseBKgif;
}
if(pauseTXLbl)
delete pauseTXLbl;
if(pauseButton)
delete pauseButton;
if(progressTimer)
delete progressTimer;
if(progressBar)
delete progressBar;
if(selectedLbl)
delete selectedLbl;
if(scoreTextLbl)
delete scoreTextLbl;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
delete gems[i][j];
}
}
delete boardWidget;
}) ;
connect(hintButton,&HoverButton::clicked,[=](){
if(gameOver)
return;
if(!is_acting&&hintArrowTimes>=6){
hintArrowTimes=0;
Point p=tipsdetect();
QString msg=QTime::currentTime().toString()+" ("+QString::number(p.x)+","+QString::number(p.y)+")";
QLabel *hintLabel=new QLabel(this);
hintLabel->setGeometry(665+p.x*118+39,44+p.y*118+118,40,60);
hintLabel->show();
setAdaptedImg(":/picture/arrow.png",hintLabel);
QPropertyAnimation* anim = new QPropertyAnimation(hintLabel,"geometry");
anim->setDuration(300);
anim->setStartValue(QRect(hintLabel->x(),hintLabel->y()+50,hintLabel->width(),hintLabel->height()));
anim->setEndValue(QRect(hintLabel->x(),hintLabel->y(),hintLabel->width(),hintLabel->height()));
anim->setEasingCurve(QEasingCurve::OutQuad);
anim->start();
QPropertyAnimation* danim = new QPropertyAnimation(hintLabel,"geometry");
connect(anim,&QPropertyAnimation::finished,[=]{
danim->setDuration(300);
danim->setEndValue(QRect(hintLabel->x(),hintLabel->y()+50,hintLabel->width(),hintLabel->height()));
danim->setStartValue(QRect(hintLabel->x(),hintLabel->y(),hintLabel->width(),hintLabel->height()));
danim->setEasingCurve(QEasingCurve::InQuad);
danim->start();
});
connect(danim,&QPropertyAnimation::finished,[=]{
hintArrowTimes=hintArrowTimes+1;
if(hintArrowTimes>=6){
if(anim)
delete anim;
if(danim)
delete danim;
if(hintLabel)
delete hintLabel;
}else{
anim->start();
}
});
}
});
connect(pauseButton,&HoverButton::clicked,[=]{
if(gameOver)
return;
if(!is_acting){
if(!is_paused)
{
if(i==1){
pauseButton->label.setText("CONTINUE");
}else{
pauseButton->label.setText("继续");
}
forbidAll(true);
pauseBKLbl = new QLabel(boardWidget);
pauseBKLbl->setGeometry(0,0,952, 952);
pauseBKgif = new QMovie("://picture/starsBK.gif",QByteArray(),boardWidget);
pauseBKgif->setScaledSize(QSize(952,952));
pauseBKLbl->setMovie(pauseBKgif);
pauseBKLbl->show();
pauseBKgif->start();
pauseTXLbl = new QLabel(boardWidget);
pauseTXLbl->setGeometry(250,boardWidget->height()/2,440,30);
pauseTXLbl->setText("The Game Has Been Paused.");
pauseTXLbl->setAlignment(Qt::AlignCenter);
pauseTXLbl->setFont(QFont("BoDoni MT",25, QFont::Normal));
pauseTXLbl->setStyleSheet("QLabel{color:white;}");
pauseTXLbl->setVisible(true);
is_paused=true;
}
else if(is_paused){
if(i==1){
pauseButton->label.setText("PAUSE");
}else{
pauseButton->label.setText("暂停");
}
pauseBKLbl->hide();
if(pauseBKgif){
pauseBKgif->stop();
}
if(pauseBKLbl){
}
if(pauseTXLbl)
pauseTXLbl->setText("");
forbidAll(false);
is_paused=false;
}
}
});
}
void GameWidget::reSetBoard(){
allFallOut();
QRandomGenerator::global()->fillRange(gemType[0], 64);
//掉落动画
QParallelAnimationGroup *group=new QParallelAnimationGroup;
for(int j = 7; j >=0; --j){
for(int i = 0; i <8 ; ++i){
gemType[i][j] = gemType[i][j] % static_cast<unsigned int>(DIFFICULITY) + 1;
gems[i][j] = new Gem(static_cast<int>(gemType[i][j]), 118, i, j , boardWidget);
gems[i][j]->setAttribute(Qt::WA_TransparentForMouseEvents, true);
group->addAnimation(startfallAnimation(gems[i][j],j+1));
connect(gems[i][j], &Gem::mouseClickedGem, this, &GameWidget::act);
}
}
group->start();
eliminateTimes=0;
connect(group, &QParallelAnimationGroup::finished, [=] {
forbidAll(false);
is_acting=false;
int s = updateBombList();
score+=s;
if(s!=0){
Sleep(100);
forbidAll(true);//禁用
is_acting=true;
eliminateBoard();
exitMagic=false;
eliminateTimes++;
}else{
eliminateTimes=0;
}
playSound(eliminateTimes);
delete group;
});
return;
}
//将path的图片放置到label上,自适应label大小
void GameWidget::setAdaptedImg(QString path,QLabel *label)
{
QImage image(path);
QSize size=label->size();
QImage image2=image.scaled(size,Qt::IgnoreAspectRatio);//重新调整图像大小以适应label
label->setPixmap(QPixmap::fromImage(image2));//显示
}
void GameWidget::keyPressEvent(QKeyEvent *ev)
{
//Esc退出全屏
if(ev->key() == Qt::Key_Escape)
{
QWidget::showNormal();
return;
}
//F11全屏
if(ev->key() == Qt::Key_F11)
{
QWidget::showFullScreen();
return;
}
QWidget::keyPressEvent(ev);
}
void GameWidget::Sleep(int msec)
{
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
void GameWidget::forbidAll(bool forbid){//true forbit ,false release
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(gems[i][j])
gems[i][j]->setAttribute(Qt::WA_TransparentForMouseEvents, forbid);
}
}
}
//关于宝石的随机数生成
int GameWidget::randomGem(){
return QRandomGenerator::global()->bounded(1, DIFFICULITY+1);
}
void GameWidget::playSound(int type){
QString src=":/music/effect/effect";
switch(type){
case 0:
return;
case 1:
src.append("1.wav");
break;
case 2:
src.append("2.wav");
break;
case 3:
src.append("3.wav");
break;
case 4:
src.append("4.wav");
break;
default:
src.append("5.wav");
break;
}
if(effect)
delete effect;
effect=new QSound(src);
effect->play();
}
void GameWidget::startGame(){
boardWidget = new QWidget(this);
boardWidget->show();
boardWidget->setGeometry(665, 44, 952, 952);
QRandomGenerator::global()->fillRange(gemType[0], 64);
//掉落动画
QParallelAnimationGroup *group=new QParallelAnimationGroup;
for(int j = 7; j >=0; --j){
for(int i = 0; i <8 ; ++i){
gemType[i][j] = gemType[i][j] % static_cast<unsigned int>(DIFFICULITY) + 1;
gems[i][j] = new Gem(static_cast<int>(gemType[i][j]), 118, i, j , boardWidget);
gems[i][j]->setAttribute(Qt::WA_TransparentForMouseEvents, true);
group->addAnimation(startfallAnimation(gems[i][j],j+1));
connect(gems[i][j], &Gem::mouseClickedGem, this, &GameWidget::act);
}
}
group->start();
eliminateTimes=0;
connect(group, &QParallelAnimationGroup::finished, [=] {
scoreTextLbl->setText("0");
connect(this, &GameWidget::eliminateFinished, [=] {
if(gameOver)
return;
Point p=tipsdetect();
if(p.x==-1&&p.y==-1){
Sleep(200);
reSetBoard();
return;
}
QSound *hit=new QSound(":/music/effect/hit.wav");
hit->play();
scoreTextLbl->setText(QString::number(score));
forbidAll(false);
is_acting=false;
int s = updateBombList();
score+=s;
if(s!=0){
Sleep(100);
forbidAll(true);//禁用
is_acting=true;
eliminateBoard();
eliminateTimes++;
exitMagic=false;
}else{
eliminateTimes=0;
}
playSound(eliminateTimes);
});
forbidAll(false);
is_acting=false;
int s = updateBombList();
score+=s;
if(s!=0){
Sleep(100);
forbidAll(true);//禁用
is_acting=true;
eliminateBoard();
exitMagic=false;
eliminateTimes++;
}else{
eliminateTimes=0;
}
playSound(eliminateTimes);
delete group;
});
connect(this,&GameWidget::finishCount,this,&GameWidget::finishAct);
}
void GameWidget::allFallOut(){
is_acting=true;
for(int j = 7; j >=0; --j){
QParallelAnimationGroup* gruop = new QParallelAnimationGroup;
for(int i = 0; i <8 ; ++i){
QPropertyAnimation* anim = new QPropertyAnimation(gems[i][j],"geometry",boardWidget);
anim->setDuration(700);
anim->setStartValue(gems[i][j]->geometry());
anim->setEndValue(QRect(gems[i][j]->geometry().x(),gems[i][j]->geometry().y()+1000,LEN,LEN));
anim->setEasingCurve(QEasingCurve::InQuad);
gruop->addAnimation(anim);
}
gruop->start(QAbstractAnimation::DeleteWhenStopped);
Sleep(90);
}
Sleep(600);
// for(int j = 7; j >=0; --j){
// for(int i = 0; i <8 ; ++i){
// gems[i][j]->bomb();
// }
// }
is_acting=false;
}
void GameWidget::finishAct(){
if(FTime==2){
//当前页面宝石掉落
magicFall();
//随机生成新宝石并掉落
magicFill();
FTime=0;
}
}
QPropertyAnimation* GameWidget::startfallAnimation(Gem *gem, int h){
//每一行的掉落动画
QPropertyAnimation* animation = new QPropertyAnimation(gem, "geometry", boardWidget);
animation->setDuration(static_cast<int>((sqrt((8-h)*300+1050)*20)));//时间由高度决定
animation->setStartValue(QRect(gem->oriX, gem->oriY-118*h-(8-h)*80, LEN, LEN));//高度由行数决定
animation->setEndValue(QRect(gem->oriX, gem->oriY, LEN, LEN));
animation->setEasingCurve(QEasingCurve::Linear);
return animation;
}
void GameWidget::magicCollect(int coType,int toX,int toY){
std::vector<Gem*> list;
QParallelAnimationGroup* group = new QParallelAnimationGroup;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(gemType[i][j]==static_cast<unsigned int>(coType)){
list.push_back(gems[i][j]);//同类型加入list
}
}
}
for(Gem* gem:list){
QPropertyAnimation* anim = new QPropertyAnimation(gem,"geometry",boardWidget);
anim->setDuration(500);
anim->setStartValue(QRect(gem->geometry()));
anim->setEndValue(QRect(gems[toX][toY]->geometry()));
anim->setEasingCurve(QEasingCurve::OutQuad);
group->addAnimation(anim);
}
list.push_back(gems[toX][toY]);
group->start();
connect(group,&QParallelAnimationGroup::finished,[=]{
delete group;
for(int i = 0; i < 8; ++i)
for(int j = 0; j < 8; ++j)
fallHeight[i][j]=0;
//计算当前页面宝石要掉落的高度
for(unsigned int i=0;i<(list.size());i++){
gemType[list[i]->x][list[i]->y] = 100;
fallHeight[list[i]->x][list[i]->y] = -1;
for(int k = list[i]->y - 1; k >= 0; --k)
if(fallHeight[list[i]->x][k] != -1)
fallHeight[list[i]->x][k]++;
}
for(Gem* gem:list){
gem->bomb();
}
Sleep(200);
fall();
fill();
});
}
void GameWidget::act(Gem* gem){
hintArrowTimes=6;
int len = 118;
int gemX = gem->x;
int gemY = gem->y;
//如果当前没有宝石被选中,则让点击的宝石选中
if(selectedX==-1 && selectedY==-1){
selectedX = gemX;
selectedY = gemY;
//加选框
selectedLbl = new QLabel(boardWidget);
QImage image("://picture/GameWidget/selected.png");
selectedLbl->setPixmap(QPixmap::fromImage(image));
selectedLbl->setGeometry(len*selectedX, len*selectedY, len, len);
selectedLbl->setAttribute(Qt::WA_TransparentForMouseEvents);
selectedLbl->show();
//让选中宝石旋转
if(gems[selectedX][selectedY]->type!=0){
makeSpin(selectedX,selectedY);
}
}
//如果有宝石选中,并点击了邻居宝石,则让宝石交换
else if( ( (selectedX==gemX)&&(abs(selectedY-gemY)==1) )
|| ( (selectedY==gemY)&&(abs(selectedX-gemX)==1) ) ){
int SX = selectedX;
int SY = selectedY;
selectedX=-1;
selectedY=-1;
if(gems[SX][SY]->type!=0)
makeStopSpin(SX,SY);
else{
makeSpin(SX,SY);
}
//去选框
selectedLbl->clear();
gems[gemX][gemY]->setAttribute(Qt::WA_TransparentForMouseEvents, true);
gems[SX][SY]->setAttribute(Qt::WA_TransparentForMouseEvents, true);
//让选中宝石与(gemX,gemY)交换
swap(SX,SY,gemX,gemY);
Sleep(300);
//在对象矩阵中交换
std::swap(gems[gemX][gemY],gems[SX][SY]);
std::swap(gems[gemX][gemY]->x,gems[SX][SY]->x);
std::swap(gems[gemX][gemY]->y,gems[SX][SY]->y);
std::swap(gemType[gemX][gemY],gemType[SX][SY]);
if(gems[SX][SY]->type==0){
magicCollect(gems[gemX][gemY]->type,SX,SY);
}
else if(gems[gemX][gemY]->type==0){
magicCollect(gems[SX][SY]->type,gemX,gemY);
}
else{
int currentScore = updateBombList();//将这次的分数返回,如果是0就回退
score+=currentScore;
if(currentScore == 0) {
if(effect)
delete effect;
effect=new QSound(":/music/effect/bad.wav");
effect->play();
std::swap(gemX,SX);
std::swap(gemY,SY);
swap(SX,SY,gemX,gemY);
Sleep(500);
//在对象矩阵中交换
std::swap(gems[gemX][gemY],gems[SX][SY]);
std::swap(gems[gemX][gemY]->x,gems[SX][SY]->x);
std::swap(gems[gemX][gemY]->y,gems[SX][SY]->y);
std::swap(gemType[gemX][gemY],gemType[SX][SY]);
is_acting=false;
gems[gemX][gemY]->setAttribute(Qt::WA_TransparentForMouseEvents, false);
gems[SX][SY]->setAttribute(Qt::WA_TransparentForMouseEvents, false);
}
else{
forbidAll(true);//禁用
is_acting=true;
gems[gemX][gemY]->setAttribute(Qt::WA_TransparentForMouseEvents, false);
gems[SX][SY]->setAttribute(Qt::WA_TransparentForMouseEvents, false);
eliminateTimes=1;
playSound(eliminateTimes);
if(exitMagic){
eliminateBoard();
}else{
bombList.clear();
int magicType1 = getBombsToMakeMagic(SX,SY,bombsToMakeMagic1,1);
generateMagic(SX,SY,magicType1,1);
int magicType2 = getBombsToMakeMagic(gemX,gemY,bombsToMakeMagic2,2);
generateMagic(gemX,gemY,magicType2,2);
}
}
exitMagic=false;
}
}
//如果点击了自己
else if( (selectedX==gemX)&&(selectedY==gemY) ){
//去选框
selectedLbl->clear();
//静止
if(gems[selectedX][selectedY]->type!=0)
makeStopSpin(selectedX,selectedY);
selectedX=-1;
selectedY=-1;
}else{
//去选框
selectedLbl->clear();
//静止
if(gems[selectedX][selectedY]->type!=0)
makeStopSpin(selectedX,selectedY);
selectedX = gemX;
selectedY = gemY;
//加选框
selectedLbl = new QLabel(boardWidget);
QImage image("://picture/GameWidget/selected.png");
selectedLbl->setPixmap(QPixmap::fromImage(image));
selectedLbl->setGeometry(len*selectedX, len*selectedY, len, len);
selectedLbl->setAttribute(Qt::WA_TransparentForMouseEvents);
selectedLbl->show();
//让选中宝石旋转
makeSpin(selectedX,selectedY);
}
}
//交换按钮位置
void GameWidget::swap(int SX,int SY,int gemX,int gemY){
//处理宝石交换
int xVal1 = gems[SX][SY]->x*118;int yVal1 = gems[SX][SY]->y*118;
int width1 = gems[SX][SY]->width();int height1 = gems[SX][SY]->height();
int xVal2 = gems[gemX][gemY]->x*118;int yVal2 = gems[gemX][gemY]->y*118;
int width2 = gems[gemX][gemY]->width();int height2 = gems[gemX][gemY]->height();
//宝石
QParallelAnimationGroup* group = new QParallelAnimationGroup;
QPropertyAnimation *anim1 = new QPropertyAnimation(gems[SX][SY],"geometry",boardWidget);
anim1->setDuration(300);
anim1->setStartValue(QRect(xVal1,yVal1,width1,height1));
anim1->setEndValue(QRect(xVal2,yVal2,width2,height2));
anim1->setEasingCurve(QEasingCurve::Linear);
//宝石动图
QPropertyAnimation *anim11 = new QPropertyAnimation(gems[SX][SY]->spinLabel,"geometry",boardWidget);
anim11->setDuration(300);
anim11->setStartValue(QRect(xVal1,yVal1,width1,height1));
anim11->setEndValue(QRect(xVal2,yVal2,width2,height2));
anim11->setEasingCurve(QEasingCurve::Linear);
//被交换宝石
QPropertyAnimation *anim2 = new QPropertyAnimation(gems[gemX][gemY],"geometry",boardWidget);
anim2->setDuration(300);
anim2->setStartValue(QRect(xVal2,yVal2,width2,height2));
anim2->setEndValue(QRect(xVal1,yVal1,width1,height1));
anim2->setEasingCurve(QEasingCurve::Linear);
group->addAnimation(anim1);
group->addAnimation(anim11);
group->addAnimation(anim2);
group->start(QAbstractAnimation::DeleteWhenStopped);
}
void GameWidget::fallAnimation(Gem *gem, int h){
gem->setAttribute(Qt::WA_TransparentForMouseEvents, true); //屏蔽鼠标
QPropertyAnimation* animation = new QPropertyAnimation(gem, "geometry", this);
animation->setDuration(static_cast<int>(sqrt(h*118*900)));
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();
connect(animation,&QPropertyAnimation::finished,[=]{
fallCount++;
gem->setAttribute(Qt::WA_TransparentForMouseEvents, false); //取消屏蔽
delete animation;
});
}
void GameWidget::fillfallAnimation(Gem *gem, int h){
gem->setAttribute(Qt::WA_TransparentForMouseEvents, true); //屏蔽鼠标
QPropertyAnimation* animation = new QPropertyAnimation(gem, "geometry", this);
animation->setDuration(static_cast<int>(sqrt(h*118*900)));
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();
connect(animation, &QPropertyAnimation::finished, [=] {
fallCount++;
if(fallCount==fallNum){
is_acting=false;
eliminateFinished();//发射信号
}
gem->setAttribute(Qt::WA_TransparentForMouseEvents, false); //取消屏蔽
delete animation;
});
}
void GameWidget::fall(){
for(int i = 0; i < 8; ++i)
for(int j = 7; j >= 0; --j){
if(fallHeight[i][j] != -1 && fallHeight[i][j] != 0 && gemType[i][j] != 100){
gemType[i][j + fallHeight[i][j]] = gemType[i][j];
gems[i][j]->setY(gems[i][j]->y + fallHeight[i][j]);
gems[i][j + fallHeight[i][j]] = gems[i][j];
gemType[i][j] = 100;
fallAnimation(gems[i][j], fallHeight[i][j]);
}
}
}
void GameWidget::magicFall(){
for(int i = 0; i < 8; ++i)
for(int j = 7; j >= 0; --j){
if(tHeight[i][j] != -1 && tHeight[i][j] != 0 && gemType[i][j] != 100){
gemType[i][j + tHeight[i][j]] = gemType[i][j];
gems[i][j]->setY(gems[i][j]->y + tHeight[i][j]);
gems[i][j + tHeight[i][j]] = gems[i][j];
gemType[i][j] = 100;
fallAnimation(gems[i][j], tHeight[i][j]);
}
}
}
void GameWidget::magicFill(){
QTimer::singleShot(100, this, [=](){
int lack[8] = {0};
for(int i = 0; i < 8; ++i){
for(int j = 0; j < 8; ++j)
if(tHeight[i][j] == -1)
lack[i]++;
else if(tHeight[i][j] != 0){
lack[i] += tHeight[i][j];
break;
}
}
for(int i = 0; i < 8; ++i)
for(int j = 0; j < lack[i]; ++j){
gems[i][lack[i]-j-1] = new Gem(randomGem(), LEN, i, lack[i]-j-1, boardWidget, -lack[i]);
gems[i][lack[i]-j-1]->setGeometry(LEN*i, LEN*(-j-1), LEN, LEN);
gemType[i][lack[i]-j-1] = static_cast<unsigned int>(gems[i][lack[i]-j-1]->type);
connect(gems[i][lack[i]-j-1], &Gem::mouseClickedGem, this, &GameWidget::act);
}
for(int i = 0; i < 8; ++i)
for(int j = 7; j >= 0; --j){
if(tHeight[i][j] != -1 && tHeight[i][j] != 0 && gemType[i][j] != 100){
fallNum++;
}
}
for(int i = 0; i < 8; ++i)
fallNum+=lack[i];
for(int i = 0; i < 8; ++i)
for(int j = 0; j < lack[i]; ++j){
fillfallAnimation(gems[i][lack[i]-j-1], lack[i]);
}
});
}
void GameWidget::fill(){
QTimer::singleShot(100, this, [=](){
int lack[8] = {0};
for(int i = 0; i < 8; ++i){
for(int j = 0; j < 8; ++j)
if(fallHeight[i][j] == -1)//这一列全消的情况
lack[i]++;
else if(fallHeight[i][j] != 0){
lack[i] += fallHeight[i][j];//这一列不是全消的情况,直接利用fallHeight即可
break;
}
}
for(int i = 0; i < 8; ++i)
for(int j = 0; j < lack[i]; ++j){
gems[i][lack[i]-j-1] = new Gem(randomGem(), LEN, i, lack[i]-j-1, boardWidget, -lack[i]);
gems[i][lack[i]-j-1]->setGeometry(LEN*i, LEN*(-j-1), LEN, LEN);
gemType[i][lack[i]-j-1] = static_cast<unsigned int>(gems[i][lack[i]-j-1]->type);
connect(gems[i][lack[i]-j-1], &Gem::mouseClickedGem, this, &GameWidget::act);
}
for(int i = 0; i < 8; ++i)
for(int j = 7; j >= 0; --j){
if(fallHeight[i][j] != -1 && fallHeight[i][j] != 0 && gemType[i][j] != 100){
fallNum++;
}
}
for(int i = 0; i < 8; ++i)
fallNum+=lack[i];
for(int i = 0; i < 8; ++i)
for(int j = 0; j < lack[i]; ++j){
fillfallAnimation(gems[i][lack[i]-j-1], lack[i]);
}
});
}
void GameWidget::generateMagic(int cX,int cY,int type,int time){
std::vector<Gem*> tempList;//动画用
if(time==1){
if(type==-1)