Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
166 lines (138 loc) · 4.73 KB
/
Copy pathmain.cpp
File metadata and controls
166 lines (138 loc) · 4.73 KB
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
#include "Ball.h"
#include "WindowManager.h"
#include <raylib.h>
#include <string>
bool hasPlayerScored(const Ball& ball, const Rectangle& scoreArea)
{
return CheckCollisionCircleRec(
ball.getPosition(), ball.getRadius(), scoreArea
);
}
void drawScoreBoard(
const int gameWindowWidth, std::string_view leftScoreBoardNum,
std::string_view rightScoreBoardNum
)
{
DrawText(" - ", gameWindowWidth / 2, 50, 94, WHITE);
DrawText(
leftScoreBoardNum.data(), gameWindowWidth / 2 - 200, 50, 94, WHITE
);
DrawText(
rightScoreBoardNum.data(), gameWindowWidth / 2 + 200, 50, 94, WHITE
);
}
void drawPlayers(
Rectangle leftPlayer, Color leftPlayerColor, Rectangle rightPlayer,
Color rightPlayerColor
)
{
DrawRectangleRec(leftPlayer, leftPlayerColor);
DrawRectangleRec(rightPlayer, rightPlayerColor);
}
int main(void)
{
WindowManager windowManager {};
int leftPlayerScore {};
std::string leftScoreBoardNum = std::to_string(leftPlayerScore);
int rightPlayerScore {};
std::string rightScoreBoardNum = std::to_string(rightPlayerScore);
// PLAYERS
constexpr int playersHeight {200};
constexpr int playersWidth {30};
// left player
constexpr int playerLeftOffset {100};
Rectangle leftPlayer {};
leftPlayer.height = playersHeight;
leftPlayer.width = playersWidth;
leftPlayer.x = playerLeftOffset;
leftPlayer.y = windowManager.getGameWindowCenter().y;
// right player
const float playerRightOffset {
static_cast<float>(windowManager.getGameWindowWidth()) - 130.0f
};
Rectangle rightPlayer;
rightPlayer.height = playersHeight;
rightPlayer.width = playersWidth;
rightPlayer.x = playerRightOffset;
rightPlayer.y = windowManager.getGameWindowCenter().y;
Vector2 randomAngledVel {Vector2Scale(
Vector2Normalize(
{static_cast<float>(rand()) / static_cast<float>(-RAND_MAX),
static_cast<float>(rand()) / static_cast<float>(RAND_MAX)}
),
2000.0f
)};
Ball theBall {};
theBall.setPosition(windowManager.getGameWindowCenter());
theBall.setColor(WHITE);
theBall.setRadius(20.0f);
theBall.setVelocity(randomAngledVel);
// Score Boundries
Rectangle leftScoreBoundry;
leftScoreBoundry.height
= static_cast<float>(windowManager.getGameWindowHeight());
leftScoreBoundry.width = 2.0f;
leftScoreBoundry.x = 0;
leftScoreBoundry.y = 0;
Rectangle rightScoreBoundry;
rightScoreBoundry.height
= static_cast<float>(windowManager.getGameWindowHeight());
rightScoreBoundry.width = 2.0f;
rightScoreBoundry.x
= static_cast<float>(windowManager.getGameWindowWidth()) - 2.0f;
rightScoreBoundry.y = 0;
// GameLoop updating each frame
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(BLACK);
// Score board top
leftScoreBoardNum = std::to_string(leftPlayerScore);
rightScoreBoardNum = std::to_string(rightPlayerScore);
// Drawing
drawScoreBoard(
windowManager.getGameWindowWidth(), leftScoreBoardNum,
rightScoreBoardNum
);
drawPlayers(leftPlayer, BLUE, rightPlayer, RED);
theBall.draw();
// Ball Movement
theBall.move();
// Right Player AI
float rightPlayerCenter {rightPlayer.height / 2};
rightPlayer.y = theBall.getPosition().y - rightPlayerCenter;
// Left Player AI
// float leftPlayerCenter{leftPlayer.height / 2};
// leftPlayer.y = theBall.pos.y - leftPlayerCenter + 61.0f;
// Ball Relection
theBall.reflectOnPlayer(leftPlayer);
theBall.reflectOnPlayer(rightPlayer);
// [[maybe_unused]]
// float leftPlayerBottomShoulderHitPoint {theBall.pos.y - 210.0f};
//
// [[maybe_unused]]
// float leftPlayerTopShoulderHitPoint {theBall.pos.y + 10.0f};
theBall.reflectOnWall(
static_cast<float>(windowManager.getGameWindowHeight())
);
// Player movement
float playerMovementSpeed {1500 * GetFrameTime()};
if (IsKeyDown(KEY_W)) {
leftPlayer.y -= playerMovementSpeed;
}
if (IsKeyDown(KEY_S)) {
leftPlayer.y += playerMovementSpeed;
}
// Scoring Mechanics
if (hasPlayerScored(theBall, rightScoreBoundry)) {
leftPlayerScore += 1;
// restart the game
theBall.setPosition(windowManager.getGameWindowCenter());
}
if (hasPlayerScored(theBall, leftScoreBoundry)) {
rightPlayerScore += 1;
// restart the game
theBall.setPosition(windowManager.getGameWindowCenter());
}
EndDrawing();
}
}