-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
1079 lines (916 loc) · 28.7 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
/*!\file Game.cpp
*
* \brief Contians the Snake Game Logic.
*
* \author Chad Martin
*
* This class contains the logic that makes up the Snake Game.
*/
#include "Game.h"
#include <QPainter>
#include <QApplication>
/*! \brief Class Constructor - initializes the data members to default values
* and creates dynamically allocated memory.
* Precondition: Game object has been created.
* Postcondition: Intro screen 1 is displayed.*/
Game::Game(QWidget *parent)
: QWidget(parent)
{
// creates dynamically allocated memory
snake = NULL;
food = new Food();
specialFood = new SpecialFood();
foodWinner = new FoodWinner();
//initializes the data members to default values
speedDelay = 1000;
points = 0;
pointMulti = 1;
introPrompt1 = TRUE;
introPrompt2 = FALSE;
gameStarted = FALSE;
gameOver = FALSE;
gameWon = FALSE;
gameEnded = FALSE;
bSOD = FALSE;
paused = FALSE;
changedDir = FALSE;
lastDir = ' ';
growCount = 0;
spawnWinnerCount = 0;
deathSpawn = FALSE;
winSpawn = FALSE;
deathCounter = 0;
winCounter = 0;
doOneTime = TRUE;
fixCount = FALSE;
switchFood = 0;
}
/*! \brief Class Destructor - Deletes dynamically allocated objects (specialFood, foodWinner, food, and snake).*/
Game::~Game()
{
// Deletes dynamically allocated memory
delete specialFood;
delete foodWinner;
delete food;
delete snake;
}
/*! \brief Paints the necessary object on the screen.
* Precondition: a QPaintEvent is called.
* Postcondition: Necessary objects are painted on the screen.
* \param *event QT event pointer*/
void Game::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
//always paints iphone on screen
painter.drawImage(iphone.getRect(), iphone.getImage());
if (gameOver) // If gameOver is true
{
gameOverScreen(painter); //paint game over screen
}
else if (gameWon) // If gameWon is true
{
victoryScreen(painter); //paint victory screen
}
else if(bSOD) // If bSOD is true, Paint Blue Screen of Death
{
//loads Blue Screen of Death image
blueScreenImage.load("blueScreenDeath.png");
blueScreenRect = blueScreenImage.rect();
blueScreenRect.moveTo(28,150);
//Paints Blue Screen of Death image
painter.drawImage(blueScreenRect, blueScreenImage);
//Paints Points on status bar of iphone
getPoints(painter);
//resets last direction to nothing
lastDir = ' ';
}
else if(introPrompt1) // If introPrompt1 is true
{
introScreen(painter); //paint intro screen 1 screen
}
else if(introPrompt2) // If introPrompt2 is true
{
introScreen(painter); //paint intro screen 2 screen
}
else // if has game Started
{
snake->drawSnake(painter); // paint Snake on screen
painter.drawImage(food->getRect(), food->getImage()); // paint Food on screen
if(winSpawn) // If winSpawn is true
{
painter.drawImage(foodWinner->getRect(), foodWinner->getImage()); // paint foodWinner on screen
winCounter++; // increament winCounter
if(winCounter%100==0) //if winCounter%100==0, then set up variables to stop painting foodWinner
{
deathSpawn = FALSE;
winSpawn = FALSE;
doOneTime = TRUE;
spawnWinnerCount = 0;
}
}
else if(deathSpawn) // If deathSpawn is true
{
painter.drawImage(specialFood->getRect(), specialFood->getImage()); // paint specialFood on screen
deathCounter++; // increament deathCounter
spawnWinnerCount++; // increament spawnWinnerCount
//if deathCounter%100==0, then set up variables to stop painting specialFood.
//if spawnWinnerCount%1000==0, then set up variables to start painting foodWinner.
if(deathCounter%100==0 || spawnWinnerCount%1000==0)
{
deathSpawn = FALSE;
doOneTime = TRUE;
}
}
//Paints Points on status bar of iphone
getPoints(painter);
}
}
/*! \brief Occurs when ever a timer event is called.
* Precondition: a QTimerEvent is called.
* Postcondition: If game is started and the direction has not changed,
* then the Snake is moved and collsion detection is run (checkCollision is called).
* Next, changedDir is set to false and QPaintEvent (repaint) is called.
* \param *event QT event pointer*/
void Game::timerEvent(QTimerEvent *event)
{
if(gameStarted && !changedDir) // If game is started and the direction has not changed
{
snake->autoMove(wrapAround); // moves snake one block in direction it has been going
checkCollision(); // checks if Snake collided with anything
}
changedDir = FALSE; // sets changedDir to false
repaint(); // calls a paint event
}
/*! \brief Carries out the required actions for the specific key.
* Precondition: a QKeyEvent is called. (a key is pressed)
* Postcondition: Actions for specified key are carried out.
* \param *event QT event pointer*/
void Game::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_Left: // Left arrow was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
else if(!introPrompt1 && !introPrompt2)
{
if(paused)
{pauseGame();} // unpause game
if(lastDir != 'L' && gameStarted)
{
snake->goLeft(wrapAround); // Move snake left one block
checkCollision(); // Check if snake collided with anything
changedDir = TRUE; // Set changedDir to true
lastDir = 'L'; // Set lastDir to 'L'
repaint(); // calls a paint event
}
}
}
break;
case Qt::Key_Right: // Right arrow was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
else if(!introPrompt1 && !introPrompt2)
{
if(paused)
{pauseGame();} // unpause game
if(lastDir != 'R' && gameStarted)
{
snake->goRight(wrapAround); // Move snake right one block
checkCollision(); // Check if snake collided with anything
changedDir = TRUE; // Set changedDir to true
lastDir = 'R'; // Set lastDir to 'R'
repaint(); // calls a paint event
}
}
}
break;
case Qt::Key_Up: // Up arrow was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
else if(!introPrompt1 && !introPrompt2)
{
if(paused)
{pauseGame();} // unpause game
if(lastDir != 'U' && gameStarted)
{
snake->goUp(wrapAround); // Move snake Up one block
checkCollision(); // Check if snake collided with anything
changedDir = TRUE; // Set changedDir to true
lastDir = 'U'; // Set lastDir to 'U'
repaint(); // calls a paint event
}
}
}
break;
case Qt::Key_Down: // Down arrow was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
else if(!introPrompt1 && !introPrompt2)
{
if(paused)
{pauseGame();} // unpause game
if(lastDir != 'D' && gameStarted)
{
snake->goDown(wrapAround); // Move snake down one block
checkCollision(); // Check if snake collided with anything
changedDir = TRUE; // Set changedDir to true
lastDir = 'D'; // Set lastDir to 'D'
repaint(); // calls a paint event
}
}
}
break;
case Qt::Key_P: // 'P' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
else
{
pauseGame(); // pause/unpause game
}
}
break;
case Qt::Key_R: // 'R' was pressed
{
if(introPrompt2) //If intro prompt 2 screen is displayed, go back to intro screen 1
{
//sets up variables for intro prompt 1
introPrompt1 =TRUE;
introPrompt2 =FALSE;
repaint(); // calls a paint event
}
else if(!introPrompt1)
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
/* Reloving UnPause/Resume/Restart ISSUES */
if(paused)
{
pauseGame(); //unpause game
}
stopGame(); // set up variables to end game and dispaly game over screen
//sets up variables for intro prompt 1
introPrompt1 =TRUE;
gameOver = FALSE;
gameWon = FALSE;
gameEnded = FALSE;
repaint(); // calls a paint event
}
}
break;
// Move the apple
case Qt::Key_M: // 'M' was pressed
case Qt::Key_A: // 'A' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
/* Reloving Paused ISSUES */
else if(paused)
{
// do nothing
}
else if(gameStarted) //If game has started move the apple and switch food images
{
// Sets up variable to switch food images
switchFood++;// increments Apple logo switcher
switchFood %= 2; // if Apple logo switch equals 2, set it back to zero
// Move the apple and making sure the apple is not on top oof any other object
do{
if(winSpawn)
food->moveFood(foodWinner,switchFood);
else if(deathSpawn)
food->moveFood(specialFood,switchFood);
else
food->moveFood(switchFood);
}while(snake->checkBadFood(food));
}
}
break;
// Grow the snake, but don't increase points
case Qt::Key_G: // 'G' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
/* Reloving Paused ISSUES */
else if(paused)
{
// do nothing
}
else if(gameStarted && lastDir != ' ')
{
// grow snake, but don't increase points
snake->grow();
growCount++;
// If needed, Fix the growCount
if(fixCount == TRUE)
{
growCount--;
fixCount = FALSE;
}
}
}
break;
// set up variable to paint specialFood and move specialFood (the Windows 8 logo)
case Qt::Key_W: // 'W' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
/* Reloving Paused ISSUES */
else if(paused)
{
// do nothing
}
else if(gameStarted)
{
// set up variable to paint specialFood
deathSpawn = TRUE;
doOneTime = FALSE;
// move specialFood making sure it is not on top oof any other object
do{
specialFood->moveFood(food);
}while(snake->checkBadFood(specialFood));
}
}
break;
// set up variables to dispaly blue screen of death
case Qt::Key_8: // '8' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
/* Reloving Paused ISSUES */
else if(paused)
{
// do nothing
}
else if(gameStarted)
{
blueScreenDeath(); // set up variables to dispaly blue screen of death
}
}
break;
// set up variable to paint foodWinner (the Linux logo)
case Qt::Key_L: // 'L' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
/* Reloving Paused ISSUES */
else if(paused)
{
// do nothing
}
else if(gameStarted)
{
// set up variable to paint foodWinner (the Linux logo)
spawnWinnerCount = 1000;
doOneTime = TRUE;
}
}
break;
// set up variables to dispaly victory screen
case Qt::Key_V: // 'V' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
/* Reloving Paused ISSUES */
else if(paused)
{
// do nothing
}
else if(gameStarted)
{
victory(); // set up variables to dispaly victory screen
}
}
break;
// During intro screen 2, this enables wrapAround and starts the game
case Qt::Key_Y: // 'Y' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
if(introPrompt2)
{
wrapAround = TRUE; // enables wrapAround
// starts the game
introPrompt2 = FALSE;
startGame();
}
}
break;
// During intro screen 2, this disables wrapAround, doubles the points multiplier, and starts the game
case Qt::Key_N: // 'N' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
if(introPrompt2)
{
wrapAround = FALSE;// disables wrapAround
// starts the game
introPrompt2 = FALSE;
pointMulti = pointMulti*2; // doubles the points multiplier
startGame();
}
}
break;
// During intro screen 1, this sets up speed delay and points multiplier,
// and then it will set up the variable for intro screen 2
case Qt::Key_1: // '1' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
if(introPrompt1)
{
// disables intro screen 1
introPrompt1 = FALSE;
// sets up speed delay and points multiplier
pointMulti = 1;
speedDelay = 200;
// set up the variable for intro screen 2
introPrompt2 = TRUE;
repaint(); // calls a paint event
}
}
break;
// During intro screen 1, this sets up speed delay and points multiplier,
// and then it will set up the variable for intro screen 2
case Qt::Key_2: // '2' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
if(introPrompt1)
{
// disables intro screen 1
introPrompt1 = FALSE;
// sets up speed delay and points multiplier
pointMulti = 2;
speedDelay = 169;
// set up the variable for intro screen 2
introPrompt2 = TRUE;
repaint(); // calls a paint event
}
}
break;
// During intro screen 1, this sets up speed delay and points multiplier,
// and then it will set up the variable for intro screen 2
case Qt::Key_3: // '3' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
if(introPrompt1)
{
// disables intro screen 1
introPrompt1 = FALSE;
// sets up speed delay and points multiplier
pointMulti = 3;
speedDelay = 138;
// set up the variable for intro screen 2
introPrompt2 = TRUE;
repaint(); // calls a paint event
}
}
break;
// During intro screen 1, this sets up speed delay and points multiplier,
// and then it will set up the variable for intro screen 2
case Qt::Key_4: // '4' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
if(introPrompt1)
{
// disables intro screen 1
introPrompt1 = FALSE;
// sets up speed delay and points multiplier
pointMulti = 4;
speedDelay = 107;
// set up the variable for intro screen 2
introPrompt2 = TRUE;
repaint(); // calls a paint event
}
}
break;
// During intro screen 1, this sets up speed delay and points multiplier,
// and then it will set up the variable for intro screen 2
case Qt::Key_5: // '5' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
if(introPrompt1)
{
// disables intro screen 1
introPrompt1 = FALSE;
// sets up speed delay and points multiplier
pointMulti = 5;
speedDelay = 75;
// set up the variable for intro screen 2
introPrompt2 = TRUE;
repaint(); // calls a paint event
}
}
break;
// During intro screen 1, this sets up speed delay and points multiplier,
// and then it will set up the variable for intro screen 2
case Qt::Key_9: // '9' was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
if(introPrompt1)
{
// disables intro screen 1
introPrompt1 = FALSE;
// sets up speed delay and points multiplier
pointMulti = 1000;
speedDelay = 0;
// set up the variable for intro screen 2
introPrompt2 = TRUE;
repaint(); // calls a paint event
}
}
break;
// If Victory or Game Over screen, sets up the variables for intro screen 1.
// If BSOD, then sets up variables to end game and dispaly game over screen.
case Qt::Key_Space: // The Space Bar was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
else if(!introPrompt1 && !introPrompt2 && gameEnded)
{
// sets up the variables for intro screen 1
introPrompt1 =TRUE;
gameOver = FALSE;
gameWon = FALSE;
gameEnded = FALSE;
repaint(); // calls a paint event
}
}
break;
// Exits App
case Qt::Key_Escape: // The Esc key was pressed
{
qApp->exit();
}
break;
// During BSOD, sets up variables to end game and dispaly game over screen
default: // Any other Key was pressed
{
if(bSOD)
{
bSOD = FALSE; // set up variable to disable blue screen of death
stopGame(); // set up variables to end game and dispaly game over screen
}
}
QWidget::keyPressEvent(event);
}
}
/*! \brief If game is not started, resets data members to default values,
* deletes Snake, creates a new Snake to default length and position, and moves food.
* Precondition: Game is not started and a 'Y' or 'N' was pressed during intro Screen 2.
* Postcondition: Game is started and Snake is centered on screen. */
void Game::startGame()
{
if (!gameStarted) // If game hasn't started
{
if(snake)
{delete snake;} // If snake created, deletes Snake
snake = new Snake(); // creates a new Snake to default length and position
// Resets data members to default values
points = 0;
growCount = 0;
timerId = startTimer(speedDelay);
gameStarted = TRUE;
gameOver = FALSE;
gameWon = FALSE;
gameEnded = FALSE;
bSOD = FALSE;
deathSpawn = FALSE;
deathCounter = 0;
doOneTime = TRUE;
spawnWinnerCount = 0;
winSpawn = FALSE;
winCounter = 0;
paused = FALSE;
fixCount = FALSE;
switchFood = 0;
/* Prevents Red Apple logo from spawning twice in a row at the start*/
// move food (Apple logos) to new location and make sure it is not on top of anything
do{
food->moveFood(switchFood);
}while(snake->checkBadFood(food));
}
}
/*! \brief Pauses and Unpauses a started game.
* Precondition: Game is started and 'P' is pressed, or Game is paused and 'P' or a directional key is pressed.
* Postcondition: If game is started, the game is paused. If game is paused, game is unpaused.*/
void Game::pauseGame()
{
if(gameStarted) //if Game is started
{
if (paused) // if paused, resume game
{
timerId = startTimer(speedDelay); // starts new timer
paused = FALSE;
lastDir = ' '; //resets last direction to nothing
}
else // if game is not paused, pause game
{
paused = TRUE;
killTimer(timerId); // kills current timer
}
}
}
/*! \brief Stops the game and the timer, sets up variables to paint the gameOverScreen, and calls a paint event.
* Precondition: Snake makes an illegal collision or any key is pressed during the Blue Screen of Death.
* Postcondition: The paint event will display the gameOverScreen.*/
void Game::stopGame()
{
killTimer(timerId); // kills current timer
// Sets up variables to paint the gameOverScreen
gameOver = TRUE;
gameEnded = TRUE;
gameStarted = FALSE;
repaint(); // calls a paint event
}
/*! \brief Stops the game and the timer, sets up variables to paint the victoryScreen, and calls a paint event.
* Precondition: If snake collides with foodWinner, game is started and 'V' is pressed, or growCount == 315.
* Postcondition: The paint event will display the victoryScreen.*/
void Game::victory()
{
killTimer(timerId); // kills current timer
// Sets up variables to paint the victoryScreen
gameWon = TRUE;
gameEnded = TRUE;
gameStarted = FALSE;
repaint(); // calls a paint event
}
/*! \brief Stops the timer, sets up variables to paint the Blue Screen of Death, sets points to zero,
* and calls a paint event.
* Precondition: If snake collides with specialFood, or game is started and '8' is pressed.
* Postcondition: Points are set to zero and the paint event will display the Blue Screen of Death.*/
void Game::blueScreenDeath()
{
killTimer(timerId); // kills current timer
// Sets up variables to paint the Blue Screen of Death
bSOD = TRUE;
points = 0;
repaint(); // calls a paint event
}
/*! \brief The screen that is displayed when a Game Over occurs.
* Precondition: Paint event is called and gameOver is true.
* Postcondition: gameOverScreen is displayed.
* \param QT Qpainter object reference. */
void Game::gameOverScreen(QPainter &painter)
{
QFont font("Courier", 15, QFont::DemiBold);
QFontMetrics fm(font);
int textWidth = fm.width("Game Over");
painter.setFont(font);
int h = height();
int w = width();
painter.translate(QPoint(w/2, h/2));
painter.drawText(-textWidth/2, -16, "Game Over");
int textWidth2 = fm.width("Total Points: " + QString::number(points));
painter.drawText(-textWidth2/2, 0, "Total Points: " + QString::number(points));
int textWidth3 = fm.width("Press the Space Bar to Restart!");
painter.drawText(-textWidth3/2, 16, "Press the Space Bar to Restart!");
lastDir = ' '; // resets last direction to nothing
}
/*! \brief The screen that is displayed when a Victory occurs.
* Precondition: Paint event is called and gameWon is true.
* Postcondition: victoryScreen is displayed.
* \param QT Qpainter object reference. */
void Game::victoryScreen(QPainter &painter)
{
victoryScreenImage.load("victoryScreen.png");
victoryScreenRect = victoryScreenImage.rect();
victoryScreenRect.moveTo(28,150);
painter.drawImage(victoryScreenRect, victoryScreenImage);
QFont font("Courier", 15, QFont::DemiBold);
QFontMetrics fm(font);
int textWidth = fm.width("Victory!!!");
QPen pen;
pen.setColor(Qt::white);
painter.setPen(pen);
painter.setFont(font);
int w = width();
painter.translate(QPoint(w/2, 150+64+32));
painter.drawText(-textWidth/2, -32, "Victory!!!");
int textWidth1 = fm.width("You have installed Linux!!!" );
painter.drawText(-textWidth1/2, -16, "You have installed Linux!!!");
int textWidth2 = fm.width("Total Points: " + QString::number(points));
painter.drawText(-textWidth2/2, 0, "Total Points: " + QString::number(points));
int textWidth3 = fm.width("Press the Space Bar to Restart!");
painter.drawText(-textWidth3/2, 16, "Press the Space Bar to Restart!");
lastDir = ' '; //resets last direction to nothing
}
/*! \brief Paints points on the status bar on the iphone.
* Precondition: Paint event is called and either gameStarted or BSOD is true.
* Postcondition: Points are displayed on status bar of iphone.
* \param QT Qpainter object reference. */
void Game::getPoints(QPainter &painter)
{
QFont font("Courier", 15, QFont::DemiBold);
QFontMetrics fm(font);
int textWidth = fm.width("Points: " + QString::number(points));
painter.setFont(font);
int w = width();
painter.translate(QPoint(w/2, 145));
painter.drawText(-textWidth/2, 0, "Points: " + QString::number(points));
}
/*! \brief Paints the intro screens on the screen of the iphone.
* Precondition: Paint event is called and either introPrompt1 or introPrompt2 is true.
* Postcondition: Either intro screen 1 or intro screen 2 is displayed.
* \param QT Qpainter object reference. */
void Game::introScreen(QPainter &painter)
{
//Paints intro screen 1 on the screen of the iphone.
if(introPrompt1)
{
QFont font("Courier", 14, QFont::DemiBold);
QFontMetrics fm(font);
int textWidth = fm.width("Let's Play Snake!!!");
painter.setFont(font);
int h = height();
int w = width();
painter.translate(QPoint(w/2, h/2));
painter.drawText(-textWidth/2, -16, "Let's Play Snake!!!");
int textWidth2 = fm.width("Select the Speed.");
painter.drawText(-textWidth2/2, 0, "Select the Speed!");
int textWidth3 = fm.width("Enter a number from 1 to 5.");
painter.drawText(-textWidth3/2, 16, "Enter a number from 1 to 5:");
int textWidth4 = fm.width("1 (Slowest) and 5 (Fastest)");
painter.drawText(-textWidth4/2, 32, "1 (Slowest) and 5 (Fastest)");
lastDir = ' '; //resets last direction to nothing
}
//Paints intro screen 2 on the screen of the iphone.
else if(introPrompt2)
{
QFont font("Courier", 14, QFont::DemiBold);
QFontMetrics fm(font);
int textWidth = fm.width("Let's Play Snake!!!");
painter.setFont(font);
int h = height();
int w = width();
painter.translate(QPoint(w/2, h/2));
painter.drawText(-textWidth/2, -16, "Let's Play Snake!!!");
int textWidth2 = fm.width("Enable Wrap Around???");
painter.drawText(-textWidth2/2, 0, "Enable Wrap Around???");
int textWidth3 = fm.width("Press 'Y' for Yes and 'N' for No.");
painter.drawText(-textWidth3/2, 16, "Press 'Y' for Yes and 'N' for No.");
lastDir = ' '; //resets last direction to nothing
}
}
/*! \brief Carries out actions if snake collided with something.
* Precondition: The Snake is moved by a directional key press or a timer event.
* Postcondition: Specific action carried out according to what the snake collided with.*/
void Game::checkCollision()
{
/* Prevents game from crashing at when the snake is bigger than 318 SnakeBlocks */
// If snake takes up 98% of screen, then VICTORY!!!
if(growCount == 315)
{victory();}
if(winSpawn)
{
//If Snake collides with foodWinner (Linux logo), end game and set up victory screen
if ((foodWinner->getRect()).intersects(snake->getRect()))
{
victory();
}
}
else if(deathSpawn)
{
//If Snake collides with specialFood (Windows 8 logo), kill timer and set up the Blue screen of death
if ((specialFood->getRect()).intersects(snake->getRect()))
{
blueScreenDeath();
}
}