-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_ui.cpp
1162 lines (1048 loc) · 41.5 KB
/
game_ui.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_ui.h"
#include "ui_game_ui.h"
#include"chooselevel_ui.h"
#include"deleteqgraphicsitemthread.h"
#include<iostream>
#include<QPixmap>
#include<QGraphicsScene>
#include<QList>
#include<QPixmap>
#include<QSize>
#include"linkgame.h"
#include"set_ui.h"
#include"rankinglist.h"
using namespace std;
extern Set_UI *set_ui;
Game_UI::Game_UI(QWidget *parent) :
MyMainWindow(parent),
ui(new Ui::Game_UI)
{
ui->setupUi(this);
Scence=new QGraphicsScene(this);
Scence->setSceneRect(0,0,this->width(),this->height());
ui->game_UI_graphicsView->setScene(Scence);
ui->score_Label->setStyleSheet("color:white");
ui->label3->setVisible(false);
//ui->label->setStyleSheet("color:white");
this->setMinimumSize(45,45);
setWindowTitle("LinkGame");
QPixmap pixmap1(":/image/button_icon/game_ui/restart.png");
ui->beginButton->resize(pixmap1.size());
ui->beginButton->setIcon(pixmap1);
ui->beginButton->setIconSize(pixmap1.size());
ui->beginButton->setMask(pixmap1.mask());
ui->beginButton->setStyleSheet("QToolButton{border:0px;}");
QPixmap pixmap2(":/image/button_icon/game_ui/pause.png");
ui->pauseButton->resize(pixmap2.size());
ui->pauseButton->setIcon(pixmap2);
ui->pauseButton->setIconSize(pixmap2.size());
ui->pauseButton->setMask(pixmap2.mask());
ui->pauseButton->setStyleSheet("QToolButton{border:0px;}");
QPixmap pixmap3(":/image/button_icon/game_ui/auto_solve_problem.png");
ui->autoSolveProblemButton->resize(pixmap3.size());
ui->autoSolveProblemButton->setIcon(pixmap3);
ui->autoSolveProblemButton->setIconSize(pixmap3.size());
ui->autoSolveProblemButton->setMask(pixmap3.mask());
ui->autoSolveProblemButton->setStyleSheet("QToolButton{border:0px;}");
QPixmap pixmap4(":/image/button_icon/game_ui/remind3.png");
ui->tipButton->resize(pixmap4.size());
ui->tipButton->setIcon(pixmap4);
ui->tipButton->setIconSize(pixmap4.size());
ui->tipButton->setMask(pixmap4.mask());
ui->tipButton->setStyleSheet("QToolButton{border:0px;}");
QPixmap pixmap5(":/image/button_icon/game_ui/reset.png");
ui->resetButton->resize(pixmap5.size());
ui->resetButton->setIcon(pixmap5);
ui->resetButton->setIconSize(pixmap5.size());
ui->resetButton->setMask(pixmap5.mask());
ui->resetButton->setStyleSheet("QToolButton{border:0px;}");
QPixmap pixmap6(":/image/button_icon/return2.png");
ui->returnButton->resize(pixmap6.size());
ui->returnButton->setIcon(pixmap6);
ui->returnButton->setIconSize(pixmap6.size());
ui->returnButton->setMask(pixmap6.mask());
ui->returnButton->setStyleSheet("QToolButton{border:0px;}");
QPixmap pixmap7(":/image/button_icon/game_ui/prepare.png");
ui->prepareButton->resize(pixmap7.size());
ui->prepareButton->setIcon(pixmap7);
ui->prepareButton->setIconSize(pixmap7.size());
ui->prepareButton->setMask(pixmap7.mask());
ui->prepareButton->setStyleSheet("QToolButton{border:0px;}");
QPixmap pixmap8(":/image/button_icon/linkgame_ui/list.png");
ui->rankingListButton->resize(pixmap8.size());
ui->rankingListButton->setIcon(pixmap8);
ui->rankingListButton->setIconSize(pixmap8.size());
ui->rankingListButton->setMask(pixmap8.mask());
ui->rankingListButton->setStyleSheet("QToolButton{border:0px;}");
QPixmap pixmap9(":/image/button_icon/game_ui/playback.png");
ui->rePlayToolButton->resize(pixmap9.size());
ui->rePlayToolButton->setIcon(pixmap9);
ui->rePlayToolButton->setIconSize(pixmap9.size());
ui->rePlayToolButton->setMask(pixmap9.mask());
ui->rePlayToolButton->setStyleSheet("QToolButton{border:0px;}");
if(!set_ui->isTwoPeople){
ui->rankingListButton->hide();
}
ui->rePlayToolButton->hide();
ui->win_lose_gameover_Label->hide();
ui->name_Label->setText(set_ui->name);
connect(set_ui, SIGNAL(signal_startPK(int**)),this, SLOT(slot_startPK(int**)));
connect(set_ui, SIGNAL(signal_ChangeHasPrepared(bool)),this, SLOT(slot_ChangeHasPrepared(bool)));
}
Game_UI::~Game_UI()
{
delete ui;
}
void Game_UI::closeEvent(QCloseEvent *event){
if(set_ui->hasLogin){
if(set_ui->isTwoPeople&&set_ui->ispking){
set_ui->tcpsocket->write("MIDFIELDEXIT");
}
set_ui->tcpsocket->disconnectFromHost();
}
exit(0);
}
void Game_UI::freeGameMap(int** gameMap){
for(int i=0;i<rowSize;i++){
free(gameMap[i]);
gameMap[i]=nullptr;
}
}
bool Game_UI::hasSollution(int** gameMap){
int** copyGameMap=(int**) malloc(rowSize*sizeof(int*));
for(int i=0;i<rowSize;i++){
copyGameMap[i]=(int*)malloc(columnSize*sizeof(int));
for(int j=0;j<columnSize;j++){
copyGameMap[i][j]=gameMap[i][j];
}
}
while(!allCleared(copyGameMap)){
bool hasSollution=false;
for(int i=1;i<=rowSize-2;i++){
for(int j=1;j<=columnSize-2;j++){
if(copyGameMap[i][j]!=0){
for (int m=1;m<=rowSize-2;m++) {
for(int n=1;n<=columnSize-2;n++){
if(copyGameMap[m][n]!=0&©GameMap[i][j]==copyGameMap[m][n]){
QList<Vertex> *list=new QList<Vertex>();
int turnNum = map.canLink_2(copyGameMap,i,j,m,n,*list);
if(turnNum!=-1){
hasSollution=true;
copyGameMap[i][j]=0;
copyGameMap[m][n]=0;
}
}
}
}
}
}
}
if(!hasSollution) {
return false;
// cout<<"无解 hasSollution(int** gameMap)"<<endl;
break;
}
}
return true;
/////////////////////////////////////////////////释放copyGameMap内存
}
void Game_UI::resetMap(){
LinkList<Vertex*> *remainVertexList=new LinkList<Vertex*>;
LinkList<int> *remainPictrueList=new LinkList<int>;
for(int i=1;i<rowSize-1;i++){
for(int j=1;j<columnSize;j++){
if(gameMap[i][j]){
Vertex *vertex=new Vertex();
vertex->first=i;
vertex->second=j;
remainVertexList->tailInsert(vertex);
remainPictrueList->tailInsert(gameMap[i][j]);
}
}
}
srand(time(NULL));
while (remainVertexList->getLen()) {
int randNum=rand()%remainVertexList->getLen();
Vertex *tempVertex;
remainVertexList->del(randNum,tempVertex);
int picIndex=0;
remainPictrueList->del(0,picIndex);
gameMap[tempVertex->first][tempVertex->second]=picIndex;
gameButtonMap[tempVertex->first][tempVertex->second]->setStyleSheet(QString("QPushButton{border-image: url(:/image/button_icon/%1/%2.png)}").arg(currentModel[set_ui->currentModelNum]).arg(picIndex).toLatin1().data());
}
while(!hasSollution(gameMap)){
resetMap();
}
remainVertexList->~LinkList();
remainPictrueList->~LinkList();
}
void Game_UI::changeAutoState(){
if(!isPause){
if(isAutoSolve){
isAutoSolve=false;
//ui->autoSolveProblemButton->setText(QString::fromLocal8Bit("自动解题"));
QPixmap pixmap1(":/image/button_icon/game_ui/auto_solve_problem.png");
ui->autoSolveProblemButton->resize(pixmap1.size());
ui->autoSolveProblemButton->setIcon(pixmap1);
ui->autoSolveProblemButton->setIconSize(pixmap1.size());
ui->autoSolveProblemButton->setMask(pixmap1.mask());
ui->autoSolveProblemButton->setStyleSheet("QToolButton{border:0px;}");
}else{
isAutoSolve=true;
//ui->autoSolveProblemButton->setText(QString::fromLocal8Bit("手动解题"));
QPixmap pixmap1(":/image/button_icon/game_ui/manual_problem_solving.png");
ui->autoSolveProblemButton->resize(pixmap1.size());
ui->autoSolveProblemButton->setIcon(pixmap1);
ui->autoSolveProblemButton->setIconSize(pixmap1.size());
ui->autoSolveProblemButton->setMask(pixmap1.mask());
ui->autoSolveProblemButton->setStyleSheet("QToolButton{border:0px;}");
}
}
}
void Game_UI::setLevel(int level){
this->level=level;
numOfPic =2+level*6;
}
int Game_UI::getLevel(){
return this->level;
}
void Game_UI::drawLine(int x1,int y1,int x2,int y2)
{
QPoint pointa(x1+edgeOfButton/2,y1+edgeOfButton/2); //点A
QPoint pointb(x2+edgeOfButton/2,y2+edgeOfButton/2); //点B
QPen pen;
pen.setColor("white");
pen.setWidth(4);
QLine line(pointa,pointb);
///////////////////////////////////
// cout<<"坐标:"<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<" "<<endl;
////////////////////////////////////
Scence->addLine(line,pen);
ui->game_UI_graphicsView->update();
DeleteQGraphicsItemThread *thread=new DeleteQGraphicsItemThread(x1+edgeOfButton/2,y1+edgeOfButton/2);
thread->start();
connect(thread, SIGNAL(deleteItem(int,int)),this, SLOT(on_deleteThread(int,int)));
}
void Game_UI::drawPathLine_exe(int index_x1,int index_y1,int index_x2,int index_y2,QList<Vertex> *list){
// cout<<"drawPathLine"<<endl;
int x1,y1,x2,y2;
MyButton *button1=gameButtonMap[index_x1][index_y1];
MyButton *button2=gameButtonMap[index_x2][index_y2];
MyButton *button3;
if(list->length() == 0)
{
drawLine(button1->x(),button1->y(),button2->x(),button2->y());
}
else if(list->length() == 1)
{
button3=gameButtonMap[list->at(0).first][list->at(0).second];
drawLine(button1->x(),button1->y(),button3->x(),button3->y());
drawLine(button3->x(),button3->y(),button2->x(),button2->y());
}
else if(list->length() == 2)
{
x1=list->at(0).second*edgeOfButton+start_x;
y1=list->at(0).first*edgeOfButton+start_y;
x2=list->at(1).second*edgeOfButton+start_x;
y2=list->at(1).first*edgeOfButton+start_y;
drawLine(button1->x(),button1->y(),x1,y1);
drawLine(x1,y1,x2,y2);
drawLine(x2,y2,button2->x(),button2->y());
}
qApp->processEvents();
//判断是否全部消除(游戏通关)
// if(allCleared(gameMap))
// {
// //gameOver();
// cout<<"全部删除!!!"<<endl;
// }
}
void Game_UI::hideButton_exe(int index_x1,int index_y1,int index_x2,int index_y2){
gameButtonMap[index_x1][index_y1]->hide();
gameButtonMap[index_x2][index_y2]->hide();
Erasure_Score();
//判断是否全部消除(游戏通关)
if(allCleared(gameMap))
{
gameOver();
// cout<<"全部删除!!!"<<endl;
}
}
void Game_UI::Erasure_Score()
{
score++;
erasure_Interval = Erasure_Time.restart();
if(erasure_Interval<=1500){
continuous_Erasure++;
score+=continuous_Erasure;
}else{
continuous_Erasure = 0;
}
ui->score_Label->setText(QString::fromLocal8Bit("得分:")+QString::number(score));
}
void Game_UI::initButtonImage()
{
for(int i=1;i<rowSize-1;i++){
for(int j=1;j<columnSize-1;j++){
gameButtonMap[i][j]->setStyleSheet(QString("QPushButton{border-image: url(:/image/button_icon/%1/%2.png)}").arg(currentModel[set_ui->currentModelNum]).arg(gameMap[i][j]).toLatin1().data());
}
}
}
void Game_UI::on_deleteThread(int x,int y){
QList<QGraphicsItem *> item_list_p = Scence->items(QRectF(x, y, 1, 1), Qt::IntersectsItemShape);
//删除元素
for(int i=0; i<item_list_p.size(); i++){
Scence->removeItem(item_list_p[i]); //从scene移除
delete item_list_p[i]; //释放内存
}
}
//bool Game_UI::autoEliminateBlock(int** gameMap_0,bool showProgress,int index_x1,int index_y1,int index_x2,int index_y2){
// cout<<"showProgress "<<showProgress<<" "<<index_x1<<" "<<index_y1<<" "<<index_x2<<" "<<index_y2<<endl;
// if(gameMap_0[index_x1][index_y1] == gameMap_0[index_x2][index_y2])
// {
// QList<Vertex> list;
// int turnNum = map.canLink_2(gameMap_0,index_x1,index_y1,index_x2,index_y2,list);
// if(turnNum!=-1)//判断能否连接消除
// {
// //判断是否全部消除(游戏通关)
// if(allCleared(gameMap_0))
// {
// ////////////////////////////////待添加结束界面
// gameOver();
// cout<<"全部删除!!!"<<endl;
// ////////////////////////////////
// }
// return true;
// }
// else
// {
// //voiceplayer->Play_Voice(1);//播放按钮音效
// return false;
// }
// voiceplayer->Stop_Voice();
// }
// else
// {
// //voiceplayer->Play_Voice(1);//播放按钮音效
// return false;
// }
//}
/*
*判定僵局
*/
bool Game_UI::isDeadlock(int** gameMap_0){
for(int i=1;i<=rowSize-2;i++){
for(int j=1;j<=columnSize-2;j++){
if(gameMap_0[i][j]!=0){
for (int m=1;m<=rowSize-2;m++) {
for(int n=1;n<=columnSize-2;n++){
if(gameMap_0[m][n]!=0&&gameMap_0[i][j]==gameMap_0[m][n]){
QList<Vertex> list;
int turnNum = map.canLink_2(gameMap_0,i,j,m,n,list);
if(turnNum!=-1){
return false;
}
}
}
}
}
}
}
return true;
}
void Game_UI::tip(int** gameMap_0){
while(!allCleared(gameMap_0)) {
bool hasSollution=false;
for(int i=1;i<=rowSize-2;i++){
for(int j=1;j<=columnSize-2;j++){
if(gameMap_0[i][j]!=0){
for (int m=1;m<=rowSize-2;m++) {
for(int n=1;n<=columnSize-2;n++){
if(gameMap_0[m][n]!=0){
if(gameMap_0[i][j] == gameMap_0[m][n])
{
QList<Vertex> list;
int turnNum = map.canLink_2(gameMap_0,i,j,m,n,list);
if(turnNum!=-1)//判断能否连接消除
{
//画线
drawPathLine_exe(i,j,m,n,&list);
// int x1,y1,x2,y2;
// MyButton *button1=gameButtonMap[i][j];
// MyButton *button2=gameButtonMap[m][n];
// MyButton *button3;
// if(turnNum == 0)
// {
// drawLine(button1->x(),button1->y(),button2->x(),button2->y());
// }
// else if(turnNum == 1)
// {
// button3=gameButtonMap[list.at(0).first][list.at(0).second];
// drawLine(button1->x(),button1->y(),button3->x(),button3->y());
// drawLine(button3->x(),button3->y(),button2->x(),button2->y());
// }
// else if(turnNum == 2)
// {
// x1=list.at(0).second*edgeOfButton+start_x;
// y1=list.at(0).first*edgeOfButton+start_y;
// x2=list.at(1).second*edgeOfButton+start_x;
// y2=list.at(1).first*edgeOfButton+start_y;
// drawLine(button1->x(),button1->y(),x1,y1);
// drawLine(x1,y1,x2,y2);
// drawLine(x2,y2,button2->x(),button2->y());
// }
// qApp->processEvents();
hasSollution=true;
goto label;
}
}
}
}
}
}
}
}
if(!hasSollution) {
break;
}
}
label:;
}
//游戏进度条
void Game_UI::gameTimerEvent(){
//进度条计时效果
if(ui->timeBar->value() == 0)
{
//游戏结束
gameOver();
}
else
{
ui->timeBar->setValue(ui->timeBar->value() - 1);
if(ui->gametime_label->text()!="0")
ui->gametime_label->setText(QString::number(ui->timeBar->value() - 1));
else
ui->timeBar->setValue(0);
}
}
bool Game_UI::allCleared(int** gameMap)
{
for(int i=1;i<rowSize-1;i++){
for(int j=1;j<columnSize-1;j++){
if(gameMap[i][j] != 0)
return false;
}
}
return true;
}
void Game_UI::gameOver()
{
if(!set_ui->isTwoPeople){
ui->rePlayToolButton->show();
}
setAllButtonVisible(false);
if(!set_ui->ispking){
QPixmap myPix(":/image/button_icon/game_over_pink.png");
ui->win_lose_gameover_Label->setPixmap(myPix);
ui->win_lose_gameover_Label->setScaledContents(true);
ui->win_lose_gameover_Label->show();
//显示gameover图片////////////////////////////////////////////////////////////////////////////////
}
if(set_ui->hasLogin){
QString endMsg="END:"+set_ui->mail;
set_ui->tcpsocket->write(endMsg.toUtf8().data());
}
//倒计时停止
if(!set_ui->ispking) gameTimer->stop();
//游戏时限内结束游戏,奖励加分2*level*剩余秒数
score+=ui->gametime_label->text().toInt()*2*level;
ui->score_Label->setText(QString::fromLocal8Bit("得分:")+QString::number(score));
// //提示框
// QMessageBox::information(this, "Game Over!", tr("<span style='color: blue; font-size: 24px;'> Your score:%1").arg(QString::number(score)));
// ui->score_Label->setText(QString::fromLocal8Bit("得分:")+QString::number(score));
//重新开始
//if(!set_ui->ispking) on_beginButton_clicked();
///////////////////强制提示框,Yes,Retry,Cancel 分别 对应无影响,重新开始,返回主界面
// QMessageBox msgBox;
// msgBox.setText(tr("<span style='color: blue; font-size: 24px;'> Game Over!</span/p>"));
// msgBox.setInformativeText(tr("Do you want to continue?"));
// msgBox.setStandardButtons(QMessageBox::Retry
// | QMessageBox::Yes
// | QMessageBox::No);
// msgBox.setDefaultButton(QMessageBox::Save);
// // msgBox.setIconPixmap(QPixmap(":/image/button_icon/fruit/2.png"));
// msgBox.setIconPixmap(QPixmap(":/image/button_icon/fruit/2.png").scaled(QSize(60,40), Qt::KeepAspectRatio));
// int ret = msgBox.exec();
// switch (ret) {
// case QMessageBox::Retry:
// qDebug() << "Retry";
// on_beginButton_clicked();
// break;
// case QMessageBox::Yes:
// qDebug() << "Yes";
// break;
// case QMessageBox::No:
// qDebug() << "No";
// LinkGame *linkGame_ui=new LinkGame;
// linkGame_ui->show();
// delete this;
// break;
// }
}
void Game_UI::on_returnButton_clicked()
{
if(isAutoSolve)
{
autoProblemSolveThread->stop();
Sleep(500);
}
freeGameMap(gameMap);
if(rePlay_vertex_queue->length()!=0){
rePlay_vertex_queue->~QQueue();
}
if(set_ui->isTwoPeople){
if(set_ui->ispking){
set_ui->tcpsocket->write("MIDFIELDEXIT");
}
LinkGame *linkgame=new LinkGame;
linkgame->show();
}else{
ChooseLevel_UI *chooseLevel_ui=new ChooseLevel_UI;
chooseLevel_ui->show();
}
//此处可能呀清空游戏数据
delete this;
}
void Game_UI::on_beginButton_clicked()
{
ui->rePlayToolButton->hide();
tipTimes=3;
// ui->label->setText(QString::fromLocal8Bit("%1/3").arg(QString::number(tipTimes)));
QPixmap pixmap4(QString(":/image/button_icon/game_ui/remind%1.png").arg(QString::number(tipTimes)));
ui->tipButton->resize(pixmap4.size());
ui->tipButton->setIcon(pixmap4);
ui->tipButton->setIconSize(pixmap4.size());
ui->tipButton->setMask(pixmap4.mask());
ui->tipButton->setStyleSheet("QToolButton{border:0px;}");
freeGameMap(gameMap);
if(isRePlay){
isRePlay=false;
rePlayTimer->stop();
}
if(rePlay_vertex_queue->length()!=0){
rePlay_vertex_queue->clear();
}
if(isAutoSolve){
autoProblemSolveThread->stop();
}
ui->label3->setVisible(false);
ui->win_lose_gameover_Label->setVisible(false);
// ui->pauseButton->setText(QString::fromLocal8Bit("暂停"));
QPixmap pixmap1(":/image/button_icon/game_ui/pause.png");
ui->pauseButton->resize(pixmap1.size());
ui->pauseButton->setIcon(pixmap1);
ui->pauseButton->setIconSize(pixmap1.size());
ui->pauseButton->setMask(pixmap1.mask());
ui->pauseButton->setStyleSheet("QToolButton{border:0px;}");
ui->timeBar->setValue(60);
ui->gametime_label->setText(QString::number(59));
gameTimer->stop();
gameTimer=new QTimer;
gameTimer->start(1000);
connect(gameTimer, SIGNAL(timeout()), this, SLOT(gameTimerEvent()));
gameMap=map.creatMap(rowSize,columnSize,level,numOfPic);
rePlayGameMap=(int**) malloc(rowSize*sizeof(int*));
for(int i=0;i<rowSize;i++){
rePlayGameMap[i]=(int*)malloc(columnSize*sizeof(int));
for(int j=0;j<columnSize;j++){
rePlayGameMap[i][j]=gameMap[i][j];
}
}
for(int i=1;i<rowSize-1;i++){
for(int j=1;j<columnSize-1;j++){
//gameButtonMap[i][j]->setText(QString::number(gameMap[i][j]));
gameButtonMap[i][j]->show();
}
}
initButtonImage();
score=0;
continuous_Erasure=0;
erasure_Interval=0;
Scence->clear();
ui->score_Label->setText(QString::fromLocal8Bit("得分:")+QString::number(score));
ui->score_Label->setStyleSheet("color:white");
}
void Game_UI::createGameMap(){
// qDebug()<<"creatGameMap";
// 进度条
ui->gametime_label->setText("59");
ui->gametime_label->setStyleSheet("color:white");
ui->score_Label->setStyleSheet("color:white");
ui->timeBar->setTextVisible(false);
ui->timeBar->setMaximum(60);
ui->timeBar->setMinimum(0);
ui->timeBar->setValue(60);
// 游戏计时器
gameTimer = new QTimer;
connect(gameTimer, SIGNAL(timeout()), this, SLOT(gameTimerEvent()));
gameTimer->start(1000);
ui->prepareButton->hide();
MyButton *button=new MyButton;
gameButtonMap=(MyButton***) malloc(rowSize*sizeof(MyButton**));
for(int i=0;i<rowSize;i++){
gameButtonMap[i]=(MyButton**)malloc(columnSize*sizeof(MyButton*));
for(int j=0;j<columnSize;j++){
gameButtonMap[i][j]=button;
}
}
gameMap=map.creatMap(rowSize,columnSize,level,numOfPic);
rePlayGameMap=(int**) malloc(rowSize*sizeof(int*));
for(int i=0;i<rowSize;i++){
rePlayGameMap[i]=(int*)malloc(columnSize*sizeof(int));
for(int j=0;j<columnSize;j++){
rePlayGameMap[i][j]=gameMap[i][j];
}
}
for(int i=1;i<rowSize-1;i++){
for(int j=1;j<columnSize-1;j++){
/////////////////
// cout<<gameMap[i][j]<<"\t";
///////////////////////
MyButton *myButton=new MyButton;
myButton->setParent(this);
myButton->setCoordinate(i,j);
myButton->setGeometry(start_x+j*edgeOfButton,start_y+i*edgeOfButton,edgeOfButton,edgeOfButton);
myButton->show();
//连接信号槽,传递button的数组下标
connect(myButton, SIGNAL(clicked(int,int)),this, SLOT(slot_myButton_clicked(int,int)));
//myButton->setText(QString::number(gameMap[i][j]));
gameButtonMap[i][j]=myButton;
}
/////////////////
// cout<<endl;
//////////////////
}
initButtonImage();
}
void Game_UI::createEmptyGameMap(){
// 进度条
ui->gametime_label->setText("59");
ui->gametime_label->setStyleSheet("color:white");
ui->score_Label->setStyleSheet("color:white");
ui->timeBar->setTextVisible(false);
ui->timeBar->setMaximum(60);
ui->timeBar->setMinimum(0);
ui->timeBar->setValue(60);
ui->beginButton->hide();
ui->autoSolveProblemButton->hide();
ui->pauseButton->hide();
ui->resetButton->hide();
MyButton *button=new MyButton;
gameButtonMap=(MyButton***) malloc(rowSize*sizeof(MyButton**));
for(int i=0;i<rowSize;i++){
gameButtonMap[i]=(MyButton**)malloc(columnSize*sizeof(MyButton*));
for(int j=0;j<columnSize;j++){
gameButtonMap[i][j]=button;
}
}
gameMap=map.creatMap(rowSize,columnSize,level,numOfPic);
for(int i=1;i<rowSize-1;i++){
for(int j=1;j<columnSize-1;j++){
MyButton *myButton=new MyButton;
myButton->setParent(this);
myButton->setCoordinate(i,j);
myButton->setGeometry(start_x+j*edgeOfButton,start_y+i*edgeOfButton,edgeOfButton,edgeOfButton);
//myButton->show();
//连接信号槽,传递button的数组下标
connect(myButton, SIGNAL(clicked(int,int)),this, SLOT(slot_myButton_clicked(int,int)));
//myButton->setText(QString::number(gameMap[i][j]));
gameButtonMap[i][j]=myButton;
}
}
}
void Game_UI::setAllButtonVisible(bool visible){
for(int i=1;i<rowSize-1;i++){
for(int j=1;j<columnSize-1;j++){
if(gameMap[i][j]!=0){
if(visible){
gameButtonMap[i][j]->show();
}else{
gameButtonMap[i][j]->hide();
}
}
}
}
}
void Game_UI::slot_myButton_clicked(int row,int column){
if(!isRePlay){
Vertex *rePlayVertex=new Vertex;
rePlayVertex->first=row;
rePlayVertex->second=column;
rePlay_vertex_queue->enqueue(rePlayVertex);
}
voiceplayer=new VoicePlayer;
if(count == 1){
if(gameMap[row][column] == gameMap[vertex1.first][vertex1.second])
{
QList<Vertex> list;
int turnNum = map.canLink_2(gameMap,row,column,vertex1.first,vertex1.second,list);
if(turnNum!=-1)//判断能否连接消除
{
count--;
voiceplayer->Play_Voice(2);//播放消除音效
gameMap[row][column]=0;
gameMap[vertex1.first][vertex1.second]=0;
gameButtonMap[row][column]->hide();
gameButtonMap[vertex1.first][vertex1.second]->hide();
Erasure_Score();
// for(int i=0;i<list.length();i++)//**************************************************
// {
//// cout<<i<<"hhhhhhhhhh"<<list.at(i).first<<" "<<list.at(i).second<<endl;
// }//*******************************************************************************
int x1,y1,x2,y2;
MyButton *button1=gameButtonMap[row][column];
MyButton *button2=gameButtonMap[vertex1.first][vertex1.second];
MyButton *button3;
if(turnNum == 0)
{
drawLine(button1->x(),button1->y(),button2->x(),button2->y());
}
else if(turnNum == 1)
{
button3=gameButtonMap[list.at(0).first][list.at(0).second];
drawLine(button1->x(),button1->y(),button3->x(),button3->y());
drawLine(button3->x(),button3->y(),button2->x(),button2->y());
}
else if(turnNum == 2)
{
x1=list.at(0).second*edgeOfButton+start_x;
y1=list.at(0).first*edgeOfButton+start_y;
x2=list.at(1).second*edgeOfButton+start_x;
y2=list.at(1).first*edgeOfButton+start_y;
drawLine(button1->x(),button1->y(),x1,y1);
drawLine(x1,y1,x2,y2);
drawLine(x2,y2,button2->x(),button2->y());
}
//判断是否全部消除(游戏通关)
if(allCleared(gameMap))
{
////////////////////////////////待添加结束界面
gameOver();
// cout<<"全部删除!!!"<<endl;
////////////////////////////////
}
// int** copyGameMap=(int**) malloc(rowSize*sizeof(int*));
// for(int i=0;i<rowSize;i++){
// copyGameMap[i]=(int*)malloc(columnSize*sizeof(int));
// for(int j=0;j<columnSize;j++){
// copyGameMap[i][j]=gameMap[i][j];
// }
// }
if(isDeadlock(gameMap)&&!allCleared(gameMap)){
resetMap();
// cout<<"陷入僵局"<<endl;
}
}
else
{
voiceplayer->Play_Voice(1);//播放按钮音效
vertex1.first = row;
vertex1.second = column;
}
list.~QList();
}
else
{
voiceplayer->Play_Voice(1);//播放按钮音效
vertex1.first = row;
vertex1.second = column;
}
}
else if(count ==0){
voiceplayer->Play_Voice(1);//播放按钮音效
vertex1.first = row;
vertex1.second = column;
count++;
}
//voiceplayer->Stop_Voice();
voiceplayer->~VoicePlayer();
// delete voiceplayer;
// voiceplayer=nullptr;
}
//暂停继续按钮
void Game_UI::on_pauseButton_clicked()
{
if(!isPause)
{
if(isAutoSolve){
autoProblemSolveThread->stop();
}
setAllButtonVisible(false);
isPause=true;
gameTimer->stop();
// ui->pauseButton->setText(QString::fromLocal8Bit("继续"));
QPixmap pixmap1(":/image/button_icon/game_ui/continue.png");
ui->pauseButton->resize(pixmap1.size());
ui->pauseButton->setIcon(pixmap1);
ui->pauseButton->setIconSize(pixmap1.size());
ui->pauseButton->setMask(pixmap1.mask());
ui->pauseButton->setStyleSheet("QToolButton{border:0px;}");
}
else if(isPause)
{
if(isAutoSolve){
autoProblemSolveThread->start();
}
setAllButtonVisible(true);
isPause=false;
gameTimer->start(1000);
// ui->pauseButton->setText(QString::fromLocal8Bit("暂停"));
QPixmap pixmap1(":/image/button_icon/game_ui/pause.png");
ui->pauseButton->resize(pixmap1.size());
ui->pauseButton->setIcon(pixmap1);
ui->pauseButton->setIconSize(pixmap1.size());
ui->pauseButton->setMask(pixmap1.mask());
ui->pauseButton->setStyleSheet("QToolButton{border:0px;}");
}
ui->label3->setVisible(isPause); //设置遮挡画布(图片)的可见性
if(isAutoSolve){
ui->label3->setVisible(false); //设置遮挡画布(图片)的可见性
setAllButtonVisible(true);
}
}
void Game_UI::on_autoSolveProblemButton_clicked()
{
if(isAutoSolve){
autoProblemSolveThread->stop();
// ui->autoSolveProblemButton->setText(QString::fromLocal8Bit("自动解题"));
QPixmap pixmap1(":/image/button_icon/game_ui/auto_solve_problem.png");
ui->autoSolveProblemButton->resize(pixmap1.size());
ui->autoSolveProblemButton->setIcon(pixmap1);
ui->autoSolveProblemButton->setIconSize(pixmap1.size());
ui->autoSolveProblemButton->setMask(pixmap1.mask());
ui->autoSolveProblemButton->setStyleSheet("QToolButton{border:0px;}");
}else{
isAutoSolve=true;
autoProblemSolveThread=new AutoProblemSolveThread(gameMap,rowSize,columnSize,true,250);
connect(autoProblemSolveThread, SIGNAL(drawPathLine(int,int,int,int,QList<Vertex>*)),this, SLOT(drawPathLine_exe(int,int,int,int,QList<Vertex>*)));
connect(autoProblemSolveThread, SIGNAL(hideButton(int,int,int,int)),this, SLOT(hideButton_exe(int,int,int,int)));
connect(autoProblemSolveThread, SIGNAL(autoSolveFinish()),this, SLOT(changeAutoState()));
autoProblemSolveThread->start();
// ui->autoSolveProblemButton->setText(QString::fromLocal8Bit("手动解题"));
QPixmap pixmap1(":/image/button_icon/game_ui/manual_problem_solving.png");
ui->autoSolveProblemButton->resize(pixmap1.size());
ui->autoSolveProblemButton->setIcon(pixmap1);
ui->autoSolveProblemButton->setIconSize(pixmap1.size());
ui->autoSolveProblemButton->setMask(pixmap1.mask());
ui->autoSolveProblemButton->setStyleSheet("QToolButton{border:0px;}");
}
}
void Game_UI::on_tipButton_clicked()
{
if(set_ui->isTwoPeople&&!set_ui->ispking)
return;
if(tipTimes>0){
tipTimes--;
//ui->label->setText(QString::fromLocal8Bit("%1/3").arg(QString::number(tipTimes)));
QPixmap pixmap4(QString(":/image/button_icon/game_ui/remind%1.png").arg(QString::number(tipTimes)));
ui->tipButton->resize(pixmap4.size());
ui->tipButton->setIcon(pixmap4);
ui->tipButton->setIconSize(pixmap4.size());
ui->tipButton->setMask(pixmap4.mask());
ui->tipButton->setStyleSheet("QToolButton{border:0px;}");
// if(isDeadlock(gameMap)&&!allCleared(gameMap)){
// cout<<"僵局"<<endl;
// }
tip(gameMap);
if(score<5){
score=0;
}else{
score-=5;
}
ui->score_Label->setText(QString::fromLocal8Bit("得分:")+QString::number(score));
}
}
void Game_UI::on_resetButton_clicked()
{
resetMap();
if(score<5){
score=0;
}else{
score-=5;
}
ui->score_Label->setText(QString::fromLocal8Bit("得分:")+QString::number(score));
}
void Game_UI::on_prepareButton_clicked()
{
ui->win_lose_gameover_Label->hide();
if(!isPrepared){
QString pkMsg="PK:"+set_ui->mail;
set_ui->tcpsocket->write(pkMsg.toUtf8().data());
// ui->prepareButton->setText(QString::fromLocal8Bit("取消准备"));
QPixmap pixmap1(":/image/button_icon/game_ui/cancel_prepare.png");
ui->prepareButton->resize(pixmap1.size());
ui->prepareButton->setIcon(pixmap1);
ui->prepareButton->setIconSize(pixmap1.size());
ui->prepareButton->setMask(pixmap1.mask());
ui->prepareButton->setStyleSheet("QToolButton{border:0px;}");
isPrepared=true;
}else{
set_ui->tcpsocket->write("CANCALPK");
// ui->prepareButton->setText(QString::fromLocal8Bit("准备"));
QPixmap pixmap2(":/image/button_icon/game_ui/prepare.png");
ui->prepareButton->resize(pixmap2.size());
ui->prepareButton->setIcon(pixmap2);
ui->prepareButton->setIconSize(pixmap2.size());
ui->prepareButton->setMask(pixmap2.mask());
ui->prepareButton->setStyleSheet("QToolButton{border:0px;}");
isPrepared=false;
}
}