This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pong.cpp
1142 lines (1016 loc) · 38.1 KB
/
Pong.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
/*
Copyright (c) 2020 Mitch Coyer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#ifndef _MSC_VER
#error "This example of Pong requires the MSVC toolchain to compile and run"
#else
#ifndef __cplusplus
#error "This example of Pong requires a C++ compiler"
#else
#define _WIN32_WINNT 0x0501
#define TIME std::chrono::time_point<std::chrono::high_resolution_clock>
#define NOW chrono::high_resolution_clock::now()
#define DURATION(startTime, endTime) std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
#define CONSOLE_WHITE 15
#define CONSOLE_RED FOREGROUND_RED | FOREGROUND_INTENSITY
#define CONSOLE_GREEN FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define CONSOLE_BLUE FOREGROUND_BLUE | FOREGROUND_INTENSITY
#define CONSOLE_YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY
#define CONSOLE_AQUA FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY
#define CONSOLE_MAGENTA FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY
#include <ctime>
#include <chrono>
#include <functional>
#include <iostream>
#include <windows.h>
#pragma comment(lib, "User32.lib")
using namespace std;
using namespace std::chrono;
class Game;
class Player;
class Ball;
class Shape;
class Shape {
public: // Typedefs
typedef std::function<void(Shape *)> shapeCallback;
public: // Constructor
Shape(int _width, int _height) : width(_width), height(_height) {}
virtual ~Shape() {}
public: // Accessors
void setVisibility(bool _visibility = true) {
visible = _visibility;
}
void setXPosition(float _xPosition, bool _fireEvents = true) {
if (_fireEvents && onBeforePositionEvent) onBeforePositionEvent(this);
x = _xPosition;
if (_fireEvents && onAfterPositionEvent) onAfterPositionEvent(this);
}
void setYPosition(float _yPosition, bool _fireEvents = true) {
if (_fireEvents && onBeforePositionEvent) onBeforePositionEvent(this);
y = _yPosition;
if (_fireEvents && onAfterPositionEvent) onAfterPositionEvent(this);
}
void setAbsPosition(int _xPosition, int _yPosition, bool _fireEvents = true) {
if (_fireEvents && onBeforePositionEvent) onBeforePositionEvent(this);
x = _xPosition;
position.X = _xPosition;
y = _yPosition;
position.Y = _yPosition;
if (_fireEvents && onAfterPositionEvent) onAfterPositionEvent(this);
}
void setXVelocity(float _vx, bool _fireEvents = true) {
if (_fireEvents && onBeforeVelocityEvent) onBeforeVelocityEvent(this);
vx = _vx;
if (_fireEvents && onAfterVelocityEvent) onAfterVelocityEvent(this);
}
void setYVelocity(float _vy, bool _fireEvents = true) {
if (_fireEvents && onBeforeVelocityEvent) onBeforeVelocityEvent(this);
vy = _vy;
if (_fireEvents && onAfterVelocityEvent) onAfterVelocityEvent(this);
}
void setVelocities(float _vx, float _vy, bool _fireEvents = true) {
if (_fireEvents && onBeforeVelocityEvent) onBeforeVelocityEvent(this);
vx = _vx;
vy = _vy;
if (_fireEvents && onAfterVelocityEvent) onAfterVelocityEvent(this);
}
public: // Events
void onBeforePositionChange(shapeCallback _callback) {
if (_callback) onBeforePositionEvent = _callback;
};
void onAfterPositionChange(shapeCallback _callback) {
if (_callback) onAfterPositionEvent = _callback;
};
void onBeforeVelocityChange(shapeCallback _callback) {
if (_callback) onBeforeVelocityEvent = _callback;
};
void onAfterVelocityChange(shapeCallback _callback) {
if (_callback) onAfterVelocityEvent = _callback;
};
public: // Draw Methods
virtual void draw(HANDLE *_console) = 0;
virtual void clear(HANDLE *_console) = 0;
protected: // Data
bool visible;
int height, width = 0; // The width and height of the object
float x, y = 0; // The coordinates of the player used for calculating position
float vx, vy = 0; // Velocity of the shape in space
COORD position; // the onscreen position of the middle of the player
shapeCallback onBeforePositionEvent = [](Shape *_this) {}; // Callback to call before the object has changed position
shapeCallback onAfterPositionEvent = [](Shape *_this) {}; // Callback to call after the object has changed position
shapeCallback onBeforeVelocityEvent = [](Shape *_this) {}; // Callback to call before the object has changed velocity
shapeCallback onAfterVelocityEvent = [](Shape *_this) {}; // Callback to call after the object has changed velocity
};
class Player : public Shape {
public: // Friends
friend Game;
public: // Constructor
Player() : Shape(1, 5) {}
~Player() {}
public: // Player Movement Handles
void moveUp() {
setYPosition(y - vy);
}
void moveDown() {
setYPosition(y + vy);
}
private: // Drawing Overrides
virtual void draw(HANDLE *_console) override {
if (visible) {
// Update Player Position from the x y values
position.Y = int(y);
COORD drawPosition = position;
drawPosition.Y -= 2;
// Adjust the draw position and colour to draw the player
SetConsoleTextAttribute(*_console, CONSOLE_AQUA);
for (int i = 0; i < height; i++) {
SetConsoleCursorPosition(*_console, drawPosition);
cout << 'I';
drawPosition.Y += 1;
}
SetConsoleTextAttribute(*_console, CONSOLE_WHITE);
}
}
virtual void clear(HANDLE *_console) override {
// Update Player Position
COORD drawPosition = position;
drawPosition.Y -= 2;
for (int i = 0; i < height; i++) {
SetConsoleCursorPosition(*_console, drawPosition);
cout << ' ';
drawPosition.Y += 1;
}
}
private: // Private Data
int score = 0; // The current score of the player
bool lostLastPoint = false; // Keep track of if the player lost their last point
};
class Ball : public Shape {
public: // Friends
friend Game;
public: // Constructors
Ball() : Shape(1, 1) {}
~Ball() {}
private: // Calculations
void calculatePosition() {
if (onBeforePositionEvent) onBeforePositionEvent(this);
x += vx;
y += vy;
if (onAfterPositionEvent) onAfterPositionEvent(this);
}
private: // Drawing Overrides
virtual void draw(HANDLE *_console) override {
// Update the position
position.X = int(x);
position.Y = int(y);
SetConsoleCursorPosition(*_console, position);
// Draw on console
SetConsoleTextAttribute(*_console, CONSOLE_GREEN);
cout << 'O';
SetConsoleTextAttribute(*_console, CONSOLE_WHITE);
}
virtual void clear(HANDLE *_console) override {
SetConsoleCursorPosition(*_console, position);
cout << ' ';
}
};
class Game {
public: // Enums
enum class GameMode {
NOT_STARTED = -1,
MULTIPLAYER = 0,
EASY = 1,
MEDIUM = 2,
HARD = 3,
IMPOSSIBLE = 4
};
enum class GameState {
NOT_STARTED = -1,
PAUSED = 0,
IN_PLAY = 1,
PLAYER_1_WINNER = 2,
PLAYER_2_WINNER = 3,
CPU_WINNER = 4
};
public: // Constructor
Game(int _width, int _height) : width(_width), height(_height) {
// Adjust the height if set to 0
if (!height)
height = 35;
if (!width)
width = 79;
// Set up the players and the ball
initGame();
}
private: // Game Initializer
void initGame() {
// Seed the random number generator
srand(unsigned int(time(NULL)));
// Get the output console object and set its size
setGameArea(height, width);
// Hid the cursor for the whole Game
hideCursor();
// Create generic functors for player movement
Shape::shapeCallback playerBeforeCallback = [=](Shape *_this) {
beforePlayerChangeCallback(_this);
};
Shape::shapeCallback playerAfterCallback = [=](Shape *_this) {
afterPlayerChangeCallback(_this);
};
// Init the players
player1.setYVelocity(1);
player1.onBeforePositionChange(playerBeforeCallback);
player1.onAfterPositionChange(playerAfterCallback);
player2.setYVelocity(1);
player2.onBeforePositionChange(playerBeforeCallback);
player2.onAfterPositionChange(playerAfterCallback);
cpu.onBeforePositionChange(playerBeforeCallback);
cpu.onAfterPositionChange(playerAfterCallback);
// Init the ball
ball.onBeforePositionChange([=](Shape *_this) {
beforeBallChangeCallback(_this);
});
ball.onAfterPositionChange([=](Shape *_this) {
afterBallChangeCallback(_this);
});
ball.onAfterVelocityChange([=](Shape *_this) {
afterBallVelocityCallback(_this);
});
}
private: // Player and ball callbacks
void beforePlayerChangeCallback(Shape *_this) {
_this->clear(&console);
}
void afterPlayerChangeCallback(Shape *_this) {
// Cast back up to Player object
Player *player = static_cast<Player *>(_this);
// Check if there are collisions with the wall
int playerHalfHeight = int(player->height / 2);
int playerTop = player->y - playerHalfHeight;
int playerBottom = player->y + playerHalfHeight;
if (playerTop < 3) {
player->setYPosition(3 + playerHalfHeight, false);
} else if (playerBottom > height - 1) {
player->setYPosition(height - 1 - playerHalfHeight, false);
}
// Draw Player
player->draw(&console);
}
void beforeBallChangeCallback(Shape *_this) {
_this->clear(&console);
}
void afterBallChangeCallback(Shape *_this) {
// Cast back up to Player object
Ball *c_ball = static_cast<Ball *>(_this);
// Make a reset play bool
bool playNeedsReset = false;
// Check if it will collide with a wall
if (c_ball->y < 3) {
if (c_ball->vy < 0) {
c_ball->setAbsPosition(c_ball->x, 3, false);
c_ball->setVelocities(c_ball->vx, -c_ball->vy);
}
} else if (c_ball->y > height - 1) {
if (c_ball->vy > 0) {
c_ball->setAbsPosition(c_ball->x, height - 1, false);
c_ball->setVelocities(c_ball->vx, -c_ball->vy);
}
}
// Check if it will collide with player 1
if (c_ball->x < 1) {
// Get position relative to player1
int ballRelPos = player1.position.Y - (int)c_ball->y;
int playerHalfHeight = (int)player1.height / 2;
if (ballRelPos <= playerHalfHeight && ballRelPos >= -playerHalfHeight) {
// Adjust Ball position
c_ball->setAbsPosition(1, c_ball->y, false);
// Invert ball x velocity
if (c_ball->vx < 0) {
float newXVelocity = randomFloat(minXSpeed, maxXSpeed);
float newYVelocity = c_ball->vy + ballRelPos / 4 + randomFloat(-0.5, 0.5);
c_ball->setVelocities(newXVelocity, newYVelocity);
Beep(300, 50);
}
// If in impossible mode add to score each hit
if (gameMode == GameMode::IMPOSSIBLE) {
player1.score += 1;
drawScore();
}
} else {
// add score to CPU or player 2
c_ball->setAbsPosition(0, c_ball->y, false);
player1.lostLastPoint = true;
getOpponent()->score += 1;
playNeedsReset = true;
}
}
// Check if it will collide with player 2 or the CPU
if (c_ball->x > width - 1) {
// Get the opponent
Player *opponent = getOpponent();
// Get position relative to opponent
int ballRelPos = (int)c_ball->y - opponent->position.Y;
int playerHalfHeight = (int)opponent->height / 2;
if (ballRelPos <= playerHalfHeight && ballRelPos >= -playerHalfHeight) {
// Adjust Ball position
c_ball->setAbsPosition(width - 2, c_ball->y, false);
// Invert ball x velocity
if (c_ball->vx > 0) {
float newXVelocity = randomFloat(-maxXSpeed, -minXSpeed);
float newYVelocity = c_ball->vy + ballRelPos / 4 + randomFloat(-0.5, 0.5);
c_ball->setVelocities(newXVelocity, newYVelocity);
Beep(300, 50);
}
} else {
// add score to CPU or player 2
c_ball->setAbsPosition(width - 1, c_ball->y, false);
opponent->lostLastPoint = true;
player1.score += 1;
playNeedsReset = true;
}
}
c_ball->draw(&console);
if (playNeedsReset) resetPlay();
}
void afterBallVelocityCallback(Shape *_this) {
// Cast back to ball
Ball *c_ball = static_cast<Ball *>(_this);
// Check if the velocities have breached their maximums or minimums
c_ball->setXVelocity((c_ball->vx > maxXSpeed) ? maxXSpeed : c_ball->vx, false);
c_ball->setXVelocity((c_ball->vx < -maxXSpeed) ? -maxXSpeed : c_ball->vx, false);
c_ball->setXVelocity((c_ball->vx > -minXSpeed && c_ball->vx < 0) ? -minXSpeed : c_ball->vx, false);
c_ball->setXVelocity((c_ball->vx < minXSpeed && c_ball->vx > 0) ? minXSpeed : c_ball->vx, false);
c_ball->setYVelocity((c_ball->vy > maxYSpeed) ? maxYSpeed : c_ball->vy, false);
c_ball->setYVelocity((c_ball->vy < -maxYSpeed) ? -maxYSpeed : c_ball->vy, false);
}
private: // Game Logic Methods
Player *getOpponent() {
Player *opponent;
if (gameMode == GameMode::MULTIPLAYER) {
opponent = &player2;
} else {
opponent = &cpu;
}
return opponent;
}
void checkInputs() {
if (isActiveWindow()) {
if (GetAsyncKeyState(0x57))
player1.moveUp();
if (GetAsyncKeyState(0x53))
player1.moveDown();
if (GetAsyncKeyState(VK_UP))
player2.moveUp();
if (GetAsyncKeyState(VK_DOWN))
player2.moveDown();
}
}
void calculateCpuPosition() {
// Shortcut the impossible mode
if (gameMode == GameMode::IMPOSSIBLE) {
cpu.setYPosition(ball.y);
}
// Only judge where the ball will be when if coming towards the CPU
else if (ball.vx > 0) {
// Get the difference between the ball and the center of the cpu
float deltaCpu = float(cpu.y - ball.y);
// Applied a weighted multiplier based on the difficulty
switch (gameMode) {
case GameMode::EASY:
cpu.vy -= deltaCpu / 10.0f;
cpu.vy *= 0.60f;
cpu.setYPosition(cpu.y + cpu.vy);
break;
case GameMode::MEDIUM:
cpu.vy -= deltaCpu / 10.0f;
cpu.vy *= 0.70f;
cpu.setYPosition(cpu.y + cpu.vy);
break;
case GameMode::HARD:
cpu.vy -= deltaCpu / 10.0f;
cpu.vy *= 0.80f;
cpu.setYPosition(cpu.y + cpu.vy);
break;
default:
break;
}
}
}
void checkScore() {
if (gameMode != GameMode::IMPOSSIBLE) {
Player *opponent = getOpponent();
if (player1.score >= 5) {
gameState = GameState::PLAYER_1_WINNER;
} else if (opponent->score >= 5) {
if (gameMode == GameMode::MULTIPLAYER)
gameState = GameState::PLAYER_2_WINNER;
else
gameState = GameState::CPU_WINNER;
}
}
}
void waitForStart() {
// Wait till the game mode is selected
if (isActiveWindow()) {
if (GetAsyncKeyState(VK_SPACE) & 0x8000) {
gameMode = GameMode::MULTIPLAYER;
} else if (GetAsyncKeyState(0x31) & 0x8000) {
gameMode = GameMode::EASY;
} else if (GetAsyncKeyState(0x32) & 0x8000) {
gameMode = GameMode::MEDIUM;
} else if (GetAsyncKeyState(0x33) & 0x8000) {
gameMode = GameMode::HARD;
} else if (GetAsyncKeyState(0x34) & 0x8000) {
gameMode = GameMode::IMPOSSIBLE;
}
}
// Show the correct players
switch (gameMode) {
case GameMode::NOT_STARTED:
player1.visible = false;
player2.visible = false;
cpu.visible = false;
break;
case GameMode::MULTIPLAYER:
player1.visible = true;
player2.visible = true;
cpu.visible = false;
break;
default:
player1.visible = true;
player2.visible = false;
cpu.visible = true;
break;
}
}
void waitForPlay() {
if (isActiveWindow()) {
if (GetAsyncKeyState(VK_SPACE) & 0x8000) {
// Clear the play area
clearPlayArea();
// Draw the players and ball back in
player1.draw(&console);
getOpponent()->draw(&console);
ball.draw(&console);
// Set the game state to inplay
gameState = GameState::IN_PLAY;
} else {
gameState = GameState::PAUSED;
}
}
}
public: // Game Progression Methods
bool runGame() {
// Draw the Title and setup game
resetGame();
drawTitleScreen();
// Play theme song
playThemeSong();
// Handle the front options menu
while (gameMode == GameMode::NOT_STARTED) {
// Exit the console if on the main screen
if (isActiveWindow() && GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
FreeConsole();
return false;
}
// Wait for user input for game mode
waitForStart();
Sleep(50);
}
// Show game mode when selected
drawGameModeScreen();
// Reset the game play
resetPlay();
// Loop through the main game functions until there is a result
while (gameState <= GameState::IN_PLAY) {
// Use the esc key to escape the game
if (isActiveWindow() && GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
Sleep(50);
return true;
}
// If window is not active or p button is pressed pause the game
if ((!isActiveWindow() || GetAsyncKeyState(0x50) & 0x8000) && gameState != GameState::PAUSED) {
gameState = GameState::PAUSED;
drawPauseScreen();
continue;
}
// Handle the start of paused play
if (gameState == GameState::PAUSED) {
waitForPlay();
Sleep(50);
}
// Run the game continuously
else if (gameState == GameState::IN_PLAY) {
// Check the keyboard inputs
checkInputs();
// Calculate the new CPU position
calculateCpuPosition();
// Calculate the new ball position
ball.calculatePosition();
// Check the score
checkScore();
// Delay for visuals
TIME loopEndTime = NOW;
auto ms = DURATION(loopStartTime, loopEndTime);
if (ms < 30)
Sleep(30 - ms);
loopStartTime = NOW;
}
}
// Show the winner
drawWinnerScreen();
playWinningSong();
Sleep(3000);
return true;
}
void resetPlay() {
// Get the opponent
Player *opponent = getOpponent();
// If the game mode was impossible reset player 1 score
if (gameMode == GameMode::IMPOSSIBLE && player1.score > 0) {
drawImpossibleModeScore();
Sleep(1000);
player1.score = 0;
}
// Add small delay to see ball before reset
Sleep(500);
// Reset the game to start conditions
player1.setAbsPosition(0, height / 2);
opponent->setAbsPosition(width - 1, height / 2);
ball.setAbsPosition(width / 2, height / 2);
// Serve the ball towards the winner
if (player1.lostLastPoint) {
ball.setVelocities(randomFloat(minXSpeed / 2, maxXSpeed / 2), randomFloat(-maxYSpeed / 3, maxYSpeed / 3));
player1.lostLastPoint = false;
} else if (opponent->lostLastPoint) {
ball.setVelocities(randomFloat(-maxXSpeed / 2, -minXSpeed / 2), randomFloat(-maxYSpeed / 3, maxYSpeed / 3));
opponent->lostLastPoint = false;
} else {
ball.setVelocities(randomFloat(-maxXSpeed / 2, maxXSpeed / 2), randomFloat(-maxYSpeed / 3, maxYSpeed / 3));
}
// Draw the start screen
drawGameStartScreen();
// Set the game state
gameState = GameState::PAUSED;
}
void resetGame() {
// Reset the scores
player1.score = 0;
player2.score = 0;
cpu.score = 0;
// Reset the states
gameMode = GameMode::NOT_STARTED;
gameState = GameState::NOT_STARTED;
}
private: // Game Draw Methods
void drawBorder(int borderColour = CONSOLE_WHITE) {
// Set the colour of the text
SetConsoleTextAttribute(console, borderColour);
// Draw the top border
setCursorPosition(0, 0);
drawOverWidth('-');
// Draw the score border
setCursorPosition(0, 2);
drawOverWidth('-');
// Draw the bottom
setCursorPosition(0, height);
drawOverWidth('-');
// Reset the colour
SetConsoleTextAttribute(console, CONSOLE_WHITE);
}
void drawScore() {
// Put the cursor at the top
setCursorPosition(0, 1);
// Get the opponent
Player * opponent = getOpponent();
const char *opponentName = (gameMode == GameMode::MULTIPLAYER) ? "P2" : "CPU";
// Draw the score board
if (gameMode == GameMode::NOT_STARTED) {
SetConsoleTextAttribute(console, CONSOLE_GREEN);
padToMiddle(" Waiting for Game Start \n");
SetConsoleTextAttribute(console, CONSOLE_WHITE);
} else if (gameMode != GameMode::IMPOSSIBLE) {
// Create the score strings
char p1Score[12];
char opponentScore[14];
sprintf(p1Score, "P1 Score: %d", player1.score);
sprintf(opponentScore, "%s Score: %d ", opponentName, opponent->score);
// Print P1 score
if (player1.score > opponent->score) {
SetConsoleTextAttribute(console, CONSOLE_GREEN);
} else if (player1.score < opponent->score) {
SetConsoleTextAttribute(console, CONSOLE_RED);
} else {
SetConsoleTextAttribute(console, CONSOLE_YELLOW);
}
padToWidth(p1Score, width / 2 - 12);
// Print the divider
SetConsoleTextAttribute(console, CONSOLE_WHITE);
cout << " | ";
// Print the opponent score
if (player1.score < opponent->score) {
SetConsoleTextAttribute(console, CONSOLE_GREEN);
} else if (player1.score > opponent->score) {
SetConsoleTextAttribute(console, CONSOLE_RED);
} else {
SetConsoleTextAttribute(console, CONSOLE_YELLOW);
}
cout << opponentScore;
// Reset the colour
SetConsoleTextAttribute(console, CONSOLE_WHITE);
} else if (gameMode == GameMode::IMPOSSIBLE) {
// Create the score strings
char p1Score[12];
sprintf(p1Score, " P1 Score: %d ", player1.score);
// Print P1 score
SetConsoleTextAttribute(console, CONSOLE_GREEN);
padToMiddle(p1Score);
SetConsoleTextAttribute(console, CONSOLE_WHITE);
// Reset the colour
SetConsoleTextAttribute(console, CONSOLE_WHITE);
}
}
void drawTitleScreen() {
// Draw the border and score
drawBorder();
drawScore();
clearPlayArea();
// Put the cursor in the right place
setCursorPosition(0, 9);
// Draw the welcome
SetConsoleTextAttribute(console, CONSOLE_BLUE);
padToMiddle(" Welcome to: \n");
// Draw the Game title
SetConsoleTextAttribute(console, CONSOLE_WHITE);
padToMiddle(" _______ _______ __ _ _______ __ \n");
padToMiddle("| || || | | || || | \n");
padToMiddle("| _ || _ || |_| || ___|| | \n");
padToMiddle("| |_| || | | || || | __ | | \n");
padToMiddle("| ___|| |_| || _ || || ||__| \n");
padToMiddle("| | | || | | || |_| | __ \n");
padToMiddle("|___| |_______||_| |__||_______||__| \n");
// Draw the instructions
setCursorPosition(0, 20);
padToMiddle("Hit SPACE for Multiplayer");
setCursorPosition(0, 22);
padToMiddle("or");
setCursorPosition(0, 24);
padToMiddle("Enter difficulty of Single Player:");
drawOverWidth(' ');
// Draw the multiplayer options
setCursorPosition(0, 26);
SetConsoleTextAttribute(console, CONSOLE_GREEN);
padToWidth("Easy : 1", 14);
SetConsoleTextAttribute(console, CONSOLE_WHITE);
cout << " | ";
SetConsoleTextAttribute(console, CONSOLE_YELLOW);
cout << "Medium : 2";
SetConsoleTextAttribute(console, CONSOLE_WHITE);
cout << " | ";
SetConsoleTextAttribute(console, CONSOLE_RED);
cout << "Hard : 3";
SetConsoleTextAttribute(console, CONSOLE_WHITE);
cout << " | ";
SetConsoleTextAttribute(console, CONSOLE_MAGENTA);
cout << "Survival : 4";
SetConsoleTextAttribute(console, CONSOLE_WHITE);
}
void drawGameModeScreen() {
// Reset the border
drawScore();
clearPlayArea();
// Draw the appropriate game mode in the right colour
switch (gameMode) {
case GameMode::MULTIPLAYER:
// Draw Text
drawBorder();
setCursorPosition(0, height / 2 - 3);
padToMiddle(" _______ __ __ _______ __ \n");
padToMiddle("| || | | || || | \n");
padToMiddle("| _ || |_| || _ || | \n");
padToMiddle("| |_| || || |_| || | \n");
padToMiddle("| ___|| || ___||__| \n");
padToMiddle("| | | | | | __ \n");
padToMiddle("|___| |___| |___| |__| \n");
// Play Song
Beep(247, 300);
Beep(330, 300);
Beep(330, 300);
Beep(370, 300);
Beep(555, 300);
break;
case GameMode::EASY:
// Draw Text
drawBorder(CONSOLE_GREEN);
setCursorPosition(0, height / 2 - 3);
SetConsoleTextAttribute(console, CONSOLE_GREEN);
padToMiddle(" _______ _______ _______ __ __ __ \n");
padToMiddle("| || _ || || | | || | \n");
padToMiddle("| ___|| |_| || _____|| |_| || | \n");
padToMiddle("| |___ | || |_____ | || | \n");
padToMiddle("| ___|| ||_____ ||_ _||__| \n");
padToMiddle("| |___ | _ | _____| | | | __ \n");
padToMiddle("|_______||__| |__||_______| |___| |__| \n");
SetConsoleTextAttribute(console, CONSOLE_WHITE);
// Play Song
Beep(494, 300);
Beep(440, 300);
Beep(392, 200);
Beep(440, 200);
Beep(494, 200);
Beep(440, 800);
break;
case GameMode::MEDIUM:
// Draw Text
drawBorder(CONSOLE_YELLOW);
setCursorPosition(0, height / 2 - 3);
SetConsoleTextAttribute(console, CONSOLE_YELLOW);
padToMiddle(" __ __ _______ ______ ___ __ __ __ __ __ \n");
padToMiddle("| |_| || || | | | | | | || |_| || | \n");
padToMiddle("| || ___|| _ || | | | | || || | \n");
padToMiddle("| || |___ | | | || | | |_| || || | \n");
padToMiddle("| || ___|| |_| || | | || ||__| \n");
padToMiddle("| ||_|| || |___ | || | | || ||_|| | __ \n");
padToMiddle("|_| |_||_______||______| |___| |_______||_| |_||__| \n");
SetConsoleTextAttribute(console, CONSOLE_WHITE);
// Play Song
Beep(440, 300);
Beep(494, 300);
Beep(440, 300);
Beep(392, 800);
break;
case GameMode::HARD:
// Draw Text
drawBorder(CONSOLE_RED);
setCursorPosition(0, height / 2 - 3);
SetConsoleTextAttribute(console, CONSOLE_RED);
padToMiddle(" __ __ _______ ______ ______ __ \n");
padToMiddle("| | | || _ || _ | | | | | \n");
padToMiddle("| |_| || |_| || | || | _ || | \n");
padToMiddle("| || || |_||_ | | | || | \n");
padToMiddle("| || || __ || |_| ||__| \n");
padToMiddle("| _ || _ || | | || | __ \n");
padToMiddle("|__| |__||__| |__||___| |_||______| |__|\n");
SetConsoleTextAttribute(console, CONSOLE_WHITE);
// Play Song
Beep(392, 800);
Beep(392, 300);
Beep(370, 300);
Beep(278, 600);
break;
case GameMode::IMPOSSIBLE:
// Draw Text
drawBorder(CONSOLE_MAGENTA);
setCursorPosition(0, height / 2 - 3);
SetConsoleTextAttribute(console, CONSOLE_MAGENTA);
padToMiddle(" ______ _______ _______ _______ __ __ __ \n");
padToMiddle("| | | || _ || || | | || | \n");
padToMiddle("| _ || ___|| |_| ||_ _|| |_| || | \n");
padToMiddle("| | | || |___ | | | | | || | \n");
padToMiddle("| |_| || ___|| | | | | ||__| \n");
padToMiddle("| || |___ | _ | | | | _ | __ \n");
padToMiddle("|______| |_______||__| |__| |___| |__| |__||__|\n");
SetConsoleTextAttribute(console, CONSOLE_WHITE);
// Play Song
Beep(494, 800);
Beep(440, 800);
Beep(392, 1600);
break;
}
}
void drawGameStartScreen() {
// Refresh the area
drawScore();
clearPlayArea();
// Draw the relevant players
player1.draw(&console);
getOpponent()->draw(&console);
ball.draw(&console);
// Draw the press to start button
drawPauseScreen();
}
void drawPauseScreen() {
// Show the game menu
setCursorPosition(0, 6);
padToMiddle("Press SPACE to start");
setCursorPosition(0, 7);
padToMiddle("Press P to pause");
setCursorPosition(0, 8);
padToMiddle("Press ESC to exit");
// Show how to win
setCursorPosition(0, 10);
SetConsoleTextAttribute(console, CONSOLE_GREEN);
if (gameMode != GameMode::IMPOSSIBLE) {
padToMiddle("First to 5 wins!");
} else {
padToMiddle("Try return the ball as many times as you can!");
}
SetConsoleTextAttribute(console, CONSOLE_WHITE);
// Show the player controls
setCursorPosition(0, 28);
padToMiddle("Player 1 Controls: W = up, S = down");
if (gameMode == GameMode::MULTIPLAYER) {
setCursorPosition(0, 29);
padToMiddle("Player 2 Controls: UP ARROW = up, DOWN ARROW = down");
}
}
void drawWinnerScreen() {
// Reset the screen
clearPlayArea();
// Put the cursor back in the middle
setCursorPosition(0, 11);
// Draw the Winner Sketch
switch (gameState) {
case GameState::PLAYER_1_WINNER:
padToWidth(" _ _ ___ __ _ __ _ _______ ______ ___ \n", width / 2 - 34);
padToWidth(" | | _ | || | | | | || | | || || _ | | | \n", width / 2 - 34);
padToWidth(" | || || || | | |_| || |_| || ___|| | || |___| \n", width / 2 - 34);
padToWidth(" | || | | || || |___ | |_||_ ___ \n", width / 2 - 34);
padToWidth(" | || | | _ || _ || ___|| __ || | \n", width / 2 - 34);
padToWidth(" | _ || | | | | || | | || |___ | | | ||___| \n", width / 2 - 34);
padToWidth(" |__| |__||___| |_| |__||_| |__||_______||___| |_| \n", width / 2 - 34);
SetConsoleTextAttribute(console, CONSOLE_GREEN);
padToWidth(" _______ ___ _______ __ __ _______ ______ ____ __ \n", width / 2 - 34);
padToWidth("| || | | _ || | | || || _ | | | | | \n", width / 2 - 34);
padToWidth("| _ || | | |_| || |_| || ___|| | || | | | | \n", width / 2 - 34);
padToWidth("| |_| || | | || || |___ | |_||_ | | | | \n", width / 2 - 34);
padToWidth("| ___|| |___ | ||_ _|| ___|| __ | | | |__| \n", width / 2 - 34);
padToWidth("| | | || _ | | | | |___ | | | | | | __ \n", width / 2 - 34);
padToWidth("|___| |_______||__| |__| |___| |_______||___| |_| |___| |__| \n", width / 2 - 34);
SetConsoleTextAttribute(console, CONSOLE_WHITE);
break;
case GameState::PLAYER_2_WINNER:
padToWidth(" _ _ ___ __ _ __ _ _______ ______ ___ \n", width / 2 - 34);
padToWidth(" | | _ | || | | | | || | | || || _ | | | \n", width / 2 - 34);
padToWidth(" | || || || | | |_| || |_| || ___|| | || |___| \n", width / 2 - 34);
padToWidth(" | || | | || || |___ | |_||_ ___ \n", width / 2 - 34);
padToWidth(" | || | | _ || _ || ___|| __ || | \n", width / 2 - 34);
padToWidth(" | _ || | | | | || | | || |___ | | | ||___| \n", width / 2 - 34);
padToWidth(" |__| |__||___| |_| |__||_| |__||_______||___| |_| \n", width / 2 - 34);
SetConsoleTextAttribute(console, CONSOLE_GREEN);
padToWidth(" _______ ___ _______ __ __ _______ ______ _______ __ \n", width / 2 - 34);
padToWidth("| || | | _ || | | || || _ | | || | \n", width / 2 - 34);
padToWidth("| _ || | | |_| || |_| || ___|| | || |____ || | \n", width / 2 - 34);
padToWidth("| |_| || | | || || |___ | |_||_ ____| || | \n", width / 2 - 34);
padToWidth("| ___|| |___ | ||_ _|| ___|| __ | | ______||__| \n", width / 2 - 34);
padToWidth("| | | || _ | | | | |___ | | | | | |_____ __ \n", width / 2 - 34);
padToWidth("|___| |_______||__| |__| |___| |_______||___| |_| |_______||__| \n", width / 2 - 34);
SetConsoleTextAttribute(console, CONSOLE_WHITE);
break;
case GameState::CPU_WINNER:
padToMiddle(" _ _ ___ __ _ __ _ _______ ______ ___ \n");
padToMiddle("| | _ | || | | | | || | | || || _ | | | \n");
padToMiddle("| || || || | | |_| || |_| || ___|| | || |___| \n");
padToMiddle("| || | | || || |___ | |_||_ ___ \n");
padToMiddle("| || | | _ || _ || ___|| __ || | \n");
padToMiddle("| _ || | | | | || | | || |___ | | | ||___| \n");
padToMiddle("|__| |__||___| |_| |__||_| |__||_______||___| |_| \n");
SetConsoleTextAttribute(console, CONSOLE_RED);
padToMiddle(" _______ _______ __ __ __ \n");
padToMiddle(" | || || | | || | \n");
padToMiddle(" | || _ || | | || | \n");
padToMiddle(" | || |_| || |_| || | \n");
padToMiddle(" | _|| ___|| ||__| \n");
padToMiddle(" | |_ | | | | __ \n");
padToMiddle(" |_______||___| |_______||__| \n");
SetConsoleTextAttribute(console, CONSOLE_WHITE);
break;
}
}