-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
1309 lines (1044 loc) · 25.2 KB
/
mainwindow.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 "mainwindow.h"
#include <QLabel>
#include <QButtonGroup>
#include <QApplication>
#include <QGridLayout>
#include <QTextEdit>
#include <iostream>
#include <QString>
#include <QTextStream>
#include <QTimer>
#include <sstream>
#include <QGraphicsPixmapItem>
#include <fstream>
using namespace std;
/* This slot is connected to the quit button and terminate the program */
void MainWindow:: quit()
{
QApplication::quit();
}
/* This slot is connected to the directions button on the start screen and updates the startscene to show the directions page */
void MainWindow:: directions()
{
// checking to see if name has been entered
namestring = name->toPlainText();
if(namestring == 0 || (namestring == "Please Enter a Valid Name") == true)
{
name->setText("Please Enter a Valid Name");
}
else
{
//creates directions screen
start->hide();
name->hide();
namelabel->hide();
prelogo->hide();
QPixmap* directionsPic = new QPixmap("PNGpics/directions.png");
QGraphicsPixmapItem* directions = new QGraphicsPixmapItem(*directionsPic);
startscene->addItem(directions);
directions->show();
view->setFixedSize(1020,800);
QPushButton* starter = new QPushButton("COMMENCE THE FUN");
startscene->addWidget(starter);
starter->move(400,700);
connect(starter, SIGNAL(clicked()), this, SLOT(startGame()));
}
}
/* This slot is connected to the menu button and pauses the game and allows for a resetGame(), quit(), or unpauseGame() */
void MainWindow:: menuFunc()
{
timer->stop();
QMessageBox menuBox;
menuBox.setText("Choose an option:");
menuBox.setWindowTitle("Menu");
QPushButton* unpause = menuBox.addButton("Resume", QMessageBox::ActionRole);
QPushButton* restartButton = menuBox.addButton("Restart", QMessageBox::ActionRole);
QPushButton* quitButton = menuBox.addButton("Quit", QMessageBox::ActionRole);
connect(quitButton, SIGNAL(clicked()), this, SLOT(quit()));
connect(restartButton, SIGNAL(clicked()), this, SLOT(resetGame()));
connect(unpause, SIGNAL(clicked()), this, SLOT(unpauseGame()));
menuBox.exec();
}
/* This slot is connected to the resume button and starts the timer from the menu*/
void MainWindow :: unpauseGame()
{
timer->start();
}
/* default destructor */
MainWindow:: ~MainWindow()
{
}
/* Constructor is responsible for creating the opening screen and has connections to the directions page */
MainWindow::MainWindow()
{
startscene = new QGraphicsScene();
mainPlayer = new Mario(700, 300);
view = new GraphicsView(startscene, mainPlayer, NULL);
view->setFixedSize(800, 800 );
QPixmap* logoPic = new QPixmap("PNGpics/prelogo.png");
prelogo = new QGraphicsPixmapItem(*logoPic);
startscene->addItem(prelogo);
prelogo->setPos(100,100);
prelogo->show();
// creating button and text box to enter name
start = new QPushButton("Next");
startscene->addWidget(start);
start->show();
start->move(200,430);
name = new QTextEdit(" ");
namelabel = new QLabel("Enter Your Name:");
name->setFixedHeight(30);
startscene->addWidget(name);
startscene->addWidget(namelabel);
name->show();
name->move(120,400);
namelabel->show();
namelabel->move(190,375);
restart = true;
mushOpen = false;
coinCounter = 0;
//initializing shells that wont show until later
greenShell2 = NULL;
greenShell3 = NULL;
connect(start, SIGNAL(clicked()), this, SLOT(directions()));
}
/* Is called when the player collides with the coin, re-randomizes the position of the coin */
void MainWindow:: resetCoin()
{
int cRandXR = rand() %870;
int cRandYR = rand()%350 + 400;
int x = mainPlayer->pixmap->pos().x();
int y = mainPlayer->pixmap->pos().y();
// putting distance between coin and player
if(cRandXR < x+5000 && cRandXR > x-5000 && cRandYR < y+5000 && cRandYR > y-5000)
{
if(cRandXR > x)
{
cRandXR += 300;
}
else if(cRandXR < x)
{
cRandXR -= 300;
}
if(cRandYR > y)
{
cRandYR += 200;
}
else if(cRandYR < y)
{
cRandYR -= 200;
}
if(cRandXR > 870)
{
cRandXR -= 700;
}
else if(cRandXR < -20)
{
cRandXR += 700;
}
if(cRandYR > 500)
{
cRandYR -= 500;
}
else if(cRandYR < 0)
{
cRandYR += 500;
}
}
// resetting counter for blue shell path
counter2 = 0;
//setting new coin and blueshell positions
coin-> setX(cRandXR);
coin->setY(cRandYR);
coin->setPos(cRandXR, cRandYR);
blueShell->setPos(cRandXR - 80,cRandYR - 90);
blueShell->setX(cRandXR - 80);
blueShell->setY(cRandYR - 90);
blueShell->setvX(1);
blueShell->setvY(1);
timer->start();
}
/* This slot is connected to the reset button but is also called in levelUp() and when a shell collision is made.
This re-randomizes the position of the shells and coin and resets all variables if restart is true */
void MainWindow :: resetGame()
{
timer->stop();
// restart is true when the retart button is pushed, not when a life is lost
if(restart == true)
{
//resetting all back to original
lives = 3;
score = 0;
counter3 = 0;
coinCounter = 0;
level = 1;
// setting player position
mainPlayer->pixmap->setPos(700,300);
mainPlayer->setX(700);
mainPlayer->setY(300);
mainPlayer->setPic(4);
// setting shell original velocities
redShell1 -> setDVelX(1.1);
redShell1 -> setDVelY(1.1);
greenShell1 -> setDVelX(3);
greenShell1 -> setDVelY(3);
// only delete it mush is on screen or else it gives error
if(mushOpen == true)
{
gamescene->removeItem(greenMush);
greenMush->setCheck(true);
greenMush->setPos(-9999, -9999);
mushOpen = false;
}
// removing big level green shells from list
if(greenShell3)
{
itemsList.pop_back();
gamescene->removeItem(greenShell3);
greenShell3 = NULL;
}
if(greenShell2)
{
itemsList.pop_back();
gamescene->removeItem(greenShell2);
greenShell2 = NULL;
}
}
// will be changed if the function calling needs restart to be false
restart = true;
//finding radnom numbers for all items
int gsRandXR = rand()%500;
int gsRandYR = rand()%300;
int cRandXR = rand() %870;
int cRandYR = rand()%350 + 400;
int gRandXR = rand() %900;
int gRandYR = rand()%550;
int gsRandXR2 = rand() %500;
int gsRandYR2 = rand()%300;
int gsRandXR3 = rand() %500;
int gsRandYR3 = rand()%300;
int rRandXR = rand() %500;
int rRandYR = rand()%300;
// finsing x and y for the mario
int x = mainPlayer->pixmap->pos().x();
int y = mainPlayer->pixmap->pos().y();
// putting distance between green shells and player
if(gsRandXR < x+500 && gsRandXR > x-500 && gsRandYR < y+500 && gsRandYR > y-500)
{
if(gsRandXR > x)
{
gsRandXR += 100;
}
else if(gsRandXR < x)
{
gsRandXR -= 100;
}
if(gsRandYR > y)
{
gsRandYR += 100;
}
else if(gsRandYR < y)
{
gsRandYR -= 100;
}
if(gsRandXR > 870)
{
gsRandXR -= 500;
}
else if(gsRandXR < -20)
{
gsRandXR += 500;
}
if(gsRandYR > 500)
{
gsRandYR -= 500;
}
else if(gsRandYR < 0)
{
gsRandYR += 500;
}
}
// distance for the 2nd green shell if exists
if(greenShell2 && gsRandXR2 < x+500 && gsRandXR2 > x-500 && gsRandYR2 < y+500 && gsRandYR2 > y-500)
{
if(gsRandXR2 > x)
{
gsRandXR2 += 100;
}
else if(gsRandXR2 < x)
{
gsRandXR2 -= 100;
}
if(gsRandYR2 > y)
{
gsRandYR2 += 100;
}
else if(gsRandYR2 < y)
{
gsRandYR2 -= 100;
}
if(gsRandXR2 > 870)
{
gsRandXR2 -= 500;
}
else if(gsRandXR2 < -20)
{
gsRandXR2 += 500;
}
if(gsRandYR2 > 500)
{
gsRandYR2 -= 500;
}
else if(gsRandYR2 < 0)
{
gsRandYR2 += 500;
}
}
// distance for the third green shell if exists
if(greenShell3 && gsRandXR3 < x+500 && gsRandXR3 > x-500 && gsRandYR3 < y+500 && gsRandYR3 > y-500)
{
if(gsRandXR3 > x)
{
gsRandXR3 += 100;
}
else if(gsRandXR3 < x)
{
gsRandXR3 -= 100;
}
if(gsRandYR3 > y)
{
gsRandYR3 += 100;
}
else if(gsRandYR3 < y)
{
gsRandYR3 -= 100;
}
if(gsRandXR3 > 870)
{
gsRandXR3 -= 500;
}
else if(gsRandXR3 < -20)
{
gsRandXR3 += 500;
}
if(gsRandYR3 > 500)
{
gsRandYR3 -= 500;
}
else if(gsRandYR3 < 0)
{
gsRandYR3 += 500;
}
}
// distance for the red shell
if(rRandXR < x+700 && rRandXR > x-700 && rRandYR < y+700 && rRandYR > y-700)
{
if(rRandXR > x)
{
cRandXR += 100;
}
else if(rRandXR < x)
{
rRandXR -= 100;
}
if(rRandYR > y)
{
rRandYR += 100;
}
else if(rRandYR < y)
{
rRandYR -= 100;
}
if(rRandXR > 870)
{
rRandXR -= 500;
}
else if(rRandXR < -20)
{
rRandXR += 500;
}
if(rRandYR > 500)
{
rRandYR -= 500;
}
else if(rRandYR < 0)
{
rRandYR += 500;
}
}
// distance between coin and player
if(cRandXR < x+5000 && cRandXR > x-5000 && cRandYR < y+5000 && cRandYR > y-5000)
{
if(cRandXR > x)
{
cRandXR += 300;
}
else if(cRandXR < x)
{
cRandXR -= 300;
}
if(cRandYR > y)
{
cRandYR += 200;
}
else if(cRandYR < y)
{
cRandYR -= 200;
}
if(cRandXR > 870)
{
cRandXR -= 700;
}
else if(cRandXR < -20)
{
cRandXR += 700;
}
if(cRandYR > 500)
{
cRandYR -= 500;
}
else if(cRandYR < 0)
{
cRandYR += 500;
}
}
// putting distance bewteen mush and player
if(gRandXR < x+500 && gRandXR > x-500 && gRandYR < y+500 && gRandYR > y-500)
{
if(gRandXR > x)
{
gRandXR += 100;
}
else if(gRandXR < x)
{
gRandXR -= 100;
}
if(gRandYR > y)
{
gRandYR += 100;
}
else if(gRandYR < y)
{
gRandYR -= 100;
}
if(gRandXR > 870)
{
gRandXR -= 500;
}
else if(gRandXR < -20)
{
gRandXR += 500;
}
if(gRandYR > 500)
{
gRandYR -= 500;
}
else if(gRandYR < 0)
{
gRandYR += 500;
}
}
counter= 0;
counter2 = 0;
// setting all positions and own variable to reflect rand numbers
greenShell1->setPos(gsRandXR, gsRandYR);
greenShell1->setX(gsRandXR);
greenShell1->setY(gsRandYR);
if(greenShell2)
{
greenShell2->setPos(gsRandXR2, gsRandYR2);
greenShell2->setX(gsRandXR2);
greenShell2->setY(gsRandYR2);
}
if(greenShell3)
{
greenShell2->setPos(gsRandXR3, gsRandYR3);
greenShell2->setX(gsRandXR3);
greenShell2->setY(gsRandYR3);
}
redShell1->setPos(rRandXR,rRandYR);
redShell1->setX(rRandXR);
redShell1->setY(rRandYR);
coin->setPos(cRandXR, cRandYR);
coin->setX(cRandXR);
coin->setY(cRandYR);
blueShell->setPos(cRandXR - 80,cRandYR - 90);
blueShell->setX(cRandXR - 80);
blueShell->setY(cRandYR - 90);
blueShell->setvX(1);
blueShell->setvY(1);
timer->start();
}
/* This slot is connected to the start button and allocates memory for the game view and scene
and creates the items and adds them to the scene as well as sets all variable and counters to origin */
void MainWindow::startGame()
{
// graphics scene and view created to show game sections
view->setFixedSize(1200, 900 );
gamescene= new QGraphicsScene();
gameview = new QGraphicsView(gamescene);
scene = new QGraphicsScene();
view->setScene(scene);
scene->clearFocus();
view->setFocusPolicy(Qt :: StrongFocus);
view->setFocus();
gameview->setFixedSize(1000, 700 );
gamescene->setSceneRect(0,0,950,650);
//setting the background of the game
backgroundPic1 = new QPixmap("PNGpics/background1.png");
backgroundPic2 = new QPixmap("PNGpics/background2.png");
backgroundPic3 = new QPixmap("PNGpics/background3.png");
backgroundPic4 = new QPixmap("PNGpics/background4.png");
QBrush* backgroundBrush1 = new QBrush(*backgroundPic1);
gamescene->setBackgroundBrush(*backgroundBrush1);
QGridLayout* layout = new QGridLayout();
view->setLayout(layout);
layout->addWidget(gameview, 2, 0, 10,5);
// set texts to start
lives = 3;
level = 1;
score = 0;
QPixmap* logoPic = new QPixmap("PNGpics/logo.png");
QLabel* logo = new QLabel();
logo->setPixmap(*logoPic);
layout->addWidget(logo, 0, 1 , Qt::AlignHCenter);
// menu button
menu = new QPushButton("Menu // Pause");
layout->addWidget(menu, 6, 10 , 1, 3, Qt::AlignHCenter);
//cheat button
QPushButton* cheat = new QPushButton("Cheat // Level Up");
layout->addWidget(cheat, 7, 10 , 1, 3, Qt::AlignHCenter);
QString name2("Name: ");
name2.append(namestring);
QLabel* name1 = new QLabel(name2);
layout->addWidget(name1, 2, 10, 1, 3 , Qt::AlignHCenter);
//setting original score
stringstream ss;
ss<< score;
string str = ss.str();
const char* cstr = str.c_str();
QString score2("Score: ");
score2.append(cstr);
score1 = new QLabel(score2);
layout->addWidget(score1, 4, 10 , 1 , 3, Qt::AlignHCenter);
//setting original lives
stringstream ss1;
ss1<< lives;
string str1 = ss1.str();
const char* cstr1 = str1.c_str();
QString lives2("Lives: ");
lives2.append(cstr1);
lives1 = new QLabel(lives2);
layout->addWidget(lives1, 5, 10 , 1 , 3, Qt::AlignHCenter);
//setting original level
stringstream ss2;
ss2<< level;
string str2 = ss2.str();
const char* cstr2 = str2.c_str();
QString level2("Level: ");
level2.append(cstr2);
level1 = new QLabel(level2);
layout->addWidget(level1, 3, 10 , 1 , 3, Qt::AlignHCenter);
// all pixmaps are created here
greenShellPic = new QPixmap("PNGpics/greenshell.png");
redShellPic = new QPixmap("PNGpics/redshell.png");
coinPic = new QPixmap("PNGpics/coin.png");
blueShellPic = new QPixmap("PNGpics/blueshell.png");
greenMushPic = new QPixmap("PNGpics/greenmushroom.png");
counter = 0;
counter2 = 0;
counter3 = 0;
gamescene->clear();
// finding original random locations
int gsRandX = rand()%900;
int gsRandY = rand()%600;
int cRandX = rand() %900;
int cRandY = rand()%600;
int gRandX = rand() %900;
int gRandY = rand()%600;
// putting distance between shell and player
while(gsRandX < 900 && gsRandX > 500 && gsRandY < 500 && gsRandY > 50)
{
gsRandX = rand()%900;
gsRandY = rand()%600;
}
// putting distance between coin and player
while(cRandX < 900 && cRandX > 500 && cRandY < 500 && cRandY > 50)
{
cRandX = rand()%900;
cRandY = rand()%600;
}
// putting distance bewteen mush and player
while(gRandX < 900 && gRandX > 500 && gRandY < 500 && gRandY > 50)
{
gRandX = rand()%900;
gRandY = rand()%600;
}
// items created
redShell1 = new RedShell(redShellPic, 80, 400);
greenShell1 = new GreenShell(greenShellPic, gsRandX, gsRandY);
coin = new Coin(coinPic, cRandX, cRandY);
blueShell = new BlueShell(blueShellPic,cRandX - 80,cRandY - 90);
greenMush = new GreenMush(greenMushPic,-9999 , -9999);
// adding items to collisions list
itemsList.push_back(redShell1);
itemsList.push_back(greenShell1);
itemsList.push_back(coin);
itemsList.push_back(blueShell);
itemsList.push_back(greenMush);
// adding items to scene
gamescene->addItem(mainPlayer->pixmap);
gamescene->addItem(redShell1);
gamescene->addItem(greenShell1);
gamescene->addItem(coin);
gamescene->addItem(blueShell);
timer = new QTimer(this);
timer->setInterval(50);
timer->start();
connect(cheat, SIGNAL(clicked()), this, SLOT(levelUp()));
connect(menu, SIGNAL(clicked()), this, SLOT(menuFunc()));
connect(timer, SIGNAL(timeout()), this, SLOT(handleTimer()));
}
/* This slot is connected to timer timeout() signal and calls the move() function for every item.
It handles the collisions of the items with the layer and calls the appropriate action functions. */
void MainWindow:: handleTimer()
{
gameview->setFocusPolicy(Qt:: NoFocus);
// creating string for score
stringstream ss;
ss<< score;
string str = ss.str();
const char* cstr = str.c_str();
QString score2("Score: ");
score2.append(cstr);
score1->setText(score2);
// creating string for lives
stringstream ss1;
ss1<< lives;
string str1 = ss1.str();
const char* cstr1 = str1.c_str();
QString lives2("Lives: ");
lives2.append(cstr1);
lives1->setText(lives2);
// creating string for level
stringstream ss2;
ss2<< level;
string str2 = ss2.str();
const char* cstr2 = str2.c_str();
QString level2("Level: ");
level2.append(cstr2);
level1->setText(level2);
counter++;
counter2++;
counter3++;
// coin bouncing cycle
if(counter == 40)
{
counter = 0;
}
// blueshell cycle
if(counter2 == 600)
{
counter2 = 0;
}
// greenmush cycle
if(counter3% 200 == 0)
{
if(greenMush->getCheck() == true && mushOpen == false)
{
int gRandX = rand() %900;
int gRandY = rand()%600;
greenMush->setCheck(true);
mushOpen = true;
greenMush->setPos(gRandX, gRandY);
gamescene->addItem(greenMush);
}
}
// calling all items to move
redShell1->move(mainPlayer->pixmap->pos());
greenShell1->move();
if(greenShell2)
{
greenShell2->move();
}
if(greenShell3)
{
greenShell3->move();
}
blueShell->setCounter(counter2);
blueShell->move();
coin->setCounter(counter);
coin->move();
// iterating through list to find colisions
list<Thing*>:: iterator it = itemsList.begin();
for(it = itemsList.begin(); it!= itemsList.end(); ++it)
{
if(mainPlayer->pixmap->collidesWithItem(*it))
{
int obj = (*it)->executePower();
{
// shells collison
if(obj == 1)
{
timer->stop();
// dies if no more lives available
if(lives == 1)
{
die();
break;
}
// takes away life else
else
{
minusLife();
break;
}
}
// blue shell collision. kills player
if(obj == 2)
{
timer->stop();
die();
break;
}
// coin colision, score updated, coin moved. Levels up if coin counter is 3
if(obj == 3)
{
timer->stop();
coinCounter++;
score += (100 * level);
resetCoin();
if(coinCounter % 3 == 0)
{
score += 500;
levelUp();
break;
}
break;
}
// mushroom collision adds life, removes from scene // only entered if the mushroom has not alread been deleted fset
if(obj == 4 && greenMush->getCheck() == true)
{
timer->stop();
gamescene->removeItem(greenMush);
greenMush->setCheck(true);
greenMush->setPos(-9999, -9999);
lives++;
mushOpen = false;
timer->start();
break;
}
}
}
}
}
/* Is called when there are no more lives left or the player collides with the blueshell.
Ends the game with a QMessgae box showing the score and an option to restart or quit */
void MainWindow:: die()
{
--lives;
// starting string to show in message box
stringstream ss1;
ss1<< lives;
string str1 = ss1.str();
const char* cstr1 = str1.c_str();
QString lives2("Lives: ");
lives2.append(cstr1);
lives1->setText(lives2);
string hsList[10];
ifstream text("highscores.txt");
if(text)
{
for(int i=0; i<10; i++)
{
string name1;
text >> name1;
int score1;
text >> score1;
stringstream total;
total << name1 << " " << score1;
string final = total.str();
hsList[i] = final;
}
text.close();
}
else
{
for(int i=0; i<10; i++)
{
stringstream num;
string name3 = "NONE";
num << name3 << " " << 0;
string final;
final = num.str();
hsList[i] = final;
}
}
for(int i=0; i<10; i++)
{
string snew = hsList[i];
stringstream ss(snew);
string name;
ss >> name;
int scoreR;
ss >> scoreR;
if(score > scoreR)
{
string cName;
cName = namestring.toStdString();
for(int j=9; j>=i+1; j--)
{
hsList[j] = hsList[j-1];
}
string name = cName;
stringstream total;
total << cName << " " << score;
string final = total.str();
hsList[i] = final;
break;