-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
261 lines (205 loc) · 6.47 KB
/
main.c
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
#include <math.h>
#include <raylib.h>
typedef struct Ball {
float cx, cy;
float radius;
Color color;
int sx, sy;
} Ball;
typedef struct Paddle {
float x, y;
float w, h;
Color color;
int s;
} Paddle;
void drawMidLine();
void drawScores();
void makeBall(Ball *b, float cx, float cy, float r, Color c);
void drawBall(Ball *b);
void updateBall(Ball *b);
void resetBall(Ball *b);
void makePaddle(Paddle *p, float x, float y, float w, float h, Color c);
void drawPaddle(Paddle *p);
void updatePaddle(Paddle *p);
void updateAI(Paddle *p, Ball *b);
Rectangle _paddle2vec4(Paddle *p);
Vector2 _ball2vec2(Ball *b);
void _bounds_check(float *y, float *h);
void _updateScore(Ball *b, Paddle *p, Paddle *ai);
void _checkBallPaddleCollision(Ball *b, Paddle *p, Paddle *ai);
int _getHalfScreenWidth();
int _getHalfScreenHeight();
int _getQuarterScreenWidth();
int _getQuarterScreenHeight();
int _getPaddleWidth();
int _getPaddleHeight();
int _getHalfPaddleWidth();
int _getHalfPaddleHeight();
int _getBallRadius();
int _getPadding();
int _getFontSize();
int player_score = 0;
int ai_score = 0;
int main(void) {
const int windowWidth = 800;
const int windowHeight = 450;
const char *windowName = "Pong";
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(windowWidth, windowHeight, windowName);
SetTargetFPS(60);
Ball ball = {0};
Paddle player = {0};
Paddle ai = {0};
makeBall(&ball, _getHalfScreenWidth(), _getHalfScreenHeight(),
_getBallRadius(), RED);
makePaddle(&player, GetScreenWidth() - _getPaddleWidth() - _getPadding(),
_getHalfScreenHeight() - _getHalfPaddleHeight(),
_getPaddleWidth(), _getPaddleHeight(), RED);
makePaddle(&ai, _getPadding(),
_getHalfScreenHeight() - _getHalfPaddleHeight(),
_getPaddleWidth(), _getPaddleHeight(), RED);
#define BACKGROUND \
CLITERAL(Color) { 24, 24, 24, 255 }
while (!WindowShouldClose()) {
_checkBallPaddleCollision(&ball, &player, &ai);
_updateScore(&ball, &player, &ai);
updateBall(&ball);
updatePaddle(&player);
updateAI(&ai, &ball);
BeginDrawing();
ClearBackground(BACKGROUND);
drawMidLine();
drawBall(&ball);
drawPaddle(&player);
drawPaddle(&ai);
drawScores();
EndDrawing();
}
CloseWindow();
return 0;
}
void drawMidLine() {
DrawLine(_getHalfScreenWidth(), 0, _getHalfScreenWidth(), GetScreenHeight(),
WHITE);
}
int _getFontPadding() { return ceilf(GetScreenHeight() * 0.04444f); }
void drawScores() {
DrawText(TextFormat("%i", ai_score),
_getQuarterScreenWidth() - _getFontPadding(), _getFontPadding(),
_getFontSize(), WHITE);
DrawText(TextFormat("%i", player_score),
3 * _getQuarterScreenWidth() - _getFontPadding(),
_getFontPadding(), _getFontSize(), WHITE);
}
void makeBall(Ball *b, float cx, float cy, float r, Color c) {
b->cx = cx;
b->cy = cy;
b->radius = r;
b->color = c;
b->sx = 4;
b->sy = 4;
}
void drawBall(Ball *b) {
DrawCircle((int)ceilf(b->cx), (int)ceilf(b->cy), b->radius, b->color);
}
void updateBall(Ball *b) {
b->cx += b->sx;
b->cy += b->sy;
// Change direction upon colliding with the walls
if (b->cy + b->radius >= GetScreenHeight() || b->cy - b->radius <= 0) {
b->sy *= -1;
}
}
void resetBall(Ball *b) {
b->cx = _getHalfScreenWidth();
b->cy = _getHalfScreenHeight();
int speed_choice[2] = {-1, 1};
b->sx *= speed_choice[GetRandomValue(0, 1)];
b->sy *= speed_choice[GetRandomValue(0, 1)];
}
void makePaddle(Paddle *p, float x, float y, float w, float h, Color c) {
p->x = x;
p->y = y;
p->w = w;
p->h = h;
p->color = c;
p->s = 4;
}
void drawPaddle(Paddle *p) { DrawRectangle(p->x, p->y, p->w, p->h, p->color); }
void updatePaddle(Paddle *p) {
if (IsWindowResized()) {
p->x = GetScreenWidth() - _getPaddleWidth() - _getPadding();
}
if (IsKeyDown(KEY_UP)) {
p->y -= p->s;
}
if (IsKeyDown(KEY_DOWN)) {
p->y += p->s;
}
_bounds_check(&p->y, &p->h);
}
void resetPaddle(Paddle *player, Paddle *ai) {
ai->x = _getPadding();
ai->y = _getHalfScreenHeight() - _getHalfPaddleHeight();
player->x = GetScreenWidth() - _getHalfPaddleWidth() - _getPadding();
player->y = _getHalfScreenHeight() - _getHalfPaddleHeight();
}
void updateAI(Paddle *p, Ball *b) {
if (IsWindowResized()) {
p->x = _getPadding();
}
if (p->y + _getHalfPaddleHeight() > b->cy) {
p->y -= p->s;
}
if (p->y + _getHalfPaddleHeight() <= b->cy) {
p->y += p->s;
}
_bounds_check(&p->y, &p->h);
}
void _bounds_check(float *y, float *h) {
// Bounds check
if (*y <= 0) {
*y = 0;
}
if (*y + *h >= GetScreenHeight()) {
*y = GetScreenHeight() - *h;
}
}
Rectangle _paddle2vec4(Paddle *p) {
return (Rectangle) {p->x, p->y, p->w, p->h};
}
Vector2 _ball2vec2(Ball *b) { return (Vector2) {b->cx, b->cy}; }
void _updateScore(Ball *b, Paddle *p, Paddle *ai) {
if (b->cx + b->radius >= GetScreenWidth()) {
ai_score++;
resetBall(b);
resetPaddle(p, ai);
return;
}
if (b->cx - b->radius <= 0) {
player_score++;
resetBall(b);
resetPaddle(p, ai);
return;
}
}
void _checkBallPaddleCollision(Ball *b, Paddle *p, Paddle *ai) {
// Check if the Ball has collided with the paddle
if (CheckCollisionCircleRec(_ball2vec2(b), b->radius, _paddle2vec4(p))) {
b->sx *= -1;
}
if (CheckCollisionCircleRec(_ball2vec2(b), b->radius, _paddle2vec4(ai))) {
b->sx *= -1;
}
}
int _getHalfScreenWidth() { return GetScreenWidth() * 0.5f; }
int _getHalfScreenHeight() { return GetScreenHeight() * 0.5f; }
int _getQuarterScreenWidth() { return GetScreenWidth() * 0.25f; }
int _getQuarterScreenHeight() { return GetScreenHeight() * 0.25f; }
int _getPaddleWidth() { return ceilf(GetScreenWidth() * 0.0125f); }
int _getPaddleHeight() { return ceilf(GetScreenHeight() * 0.26666666f); }
int _getHalfPaddleWidth() { return ceilf(_getPaddleWidth() * 0.5f); }
int _getHalfPaddleHeight() { return ceilf(_getPaddleHeight() * 0.5f); }
int _getBallRadius() { return ceilf(GetScreenWidth() * 0.0095f); }
int _getPadding() { return ceilf(GetScreenWidth() * 0.0125f); }
int _getFontSize() { return ceilf(GetScreenWidth() * 0.038f); }