-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
394 lines (346 loc) · 9.06 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
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
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#include "definitions.h"
#include "structs.h"
// essentials
bool started = false; // is game started or not
Ball ball;
Player player1;
Player player2;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
FILE *file;
// Score Bar
SDL_Rect score = {
.x = (WIDTH / 2) - (120 / 2),
.y = HEIGHT - 40,
.w = 120,
.h = 20,
};
// to color the bottom score bar
bool player1Score = false;
bool player2Score = false;
// ----functions' prototypes----
bool Initialize(void);
void Update(float);
void End(void);
Ball CreateBall(int size);
void UpdateBall(Ball *ball, float elapsed);
void RenderBall(const Ball *);
Player MakePlayer(void);
void UpdatePlayers(float elapsed);
void RenderPlayers(void);
void UpdateScore(int player, int points);
void RenderLines(float yPos);
// -----------------------------
int main(int argc, char *argv[])
{
srand((unsigned int)time(NULL));
// at end of game
atexit(End);
if (!Initialize())
{
exit(1);
}
bool quit = false;
SDL_Event event;
// to get the number of milliseconds passed since the game started
Uint32 lastTick = SDL_GetTicks();
while (!quit)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
quit = true;
}
}
// to animate rects
Uint32 curTick = SDL_GetTicks();
Uint32 diff = curTick - lastTick;
float elapsed = diff / 1000.0f;
Update(elapsed); // x time elapsed
lastTick = curTick;
}
SDL_Quit();
return 0;
}
bool Initialize(void)
{
// Initializes the timer, audio, video, joystick,
// haptic, gamecontroller and events subsystems
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
printf("Error initializing SDL: %s\n", SDL_GetError());
return false;
}
// creates a window
window = SDL_CreateWindow("Table Tennis -- Rally", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
// window created but it does not stay
if (!window)
{
return false;
}
// creates a renderer
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!window)
{
return false;
}
file = fopen("ScoreBoard.txt", "w");
if (file == NULL)
{
printf("Error initializing file ScoreBoard.txt\n");
return false;
}
// creates rects
ball = CreateBall(BALLSIZE);
player1 = MakePlayer();
player2 = MakePlayer();
return true;
}
void Update(float elapsed)
{
// to color the screen
SDL_SetRenderDrawColor(renderer, 55, 255, 1, 255);
SDL_RenderClear(renderer);
UpdateBall(&ball, elapsed);
RenderBall(&ball);
UpdatePlayers(elapsed);
RenderPlayers();
for (int i = 500; i >= 0; i -= 30)
RenderLines(i);
SDL_RenderDrawLine(renderer, 0, HEIGHT - 60, WIDTH, HEIGHT - 60);
// score bar rendering
if (player1Score)
{
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
}
else if (player2Score)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
}
else
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
}
SDL_RenderFillRect(renderer, &score);
SDL_RenderPresent(renderer);
}
void End(void)
{
// this func deallocates memory
if (renderer)
{
SDL_DestroyRenderer(renderer);
}
if (window)
{
SDL_DestroyWindow(window);
}
if (player1.score > player2.score)
{
fprintf(file, "You Lost 😶 --- 😢👎");
}
else if (player1.score < player2.score)
{
fprintf(file, "You Won 🏆 --- 🥳🎉");
}
else
{
fprintf(file, "Match Tied --- 😕👔");
}
fprintf(file, "\n\n----- Final Score 💯 -----\n");
fprintf(file, "--> Bot 🤖: %d\n", player1.score);
fprintf(file, "--> You 👦: %d\n", player2.score);
fclose(file);
SDL_Quit();
}
bool HeadOrTails(void)
{
// to randomly guess
return rand() % 2 == 1 ? true : false;
}
Ball CreateBall(int size)
{
Ball ball = {
.x = WIDTH / 2 - size / 2,
.y = HEIGHT / 2 - size / 2,
.size = size,
.xSpeed = SPEED * (HeadOrTails() ? 1 : -1),
.ySpeed = SPEED * (HeadOrTails() ? 1 : -1),
};
return ball;
}
void RenderBall(const Ball *ball)
{
int size = ball->size;
int halfSize = size / 2;
SDL_Rect ballRect = {
.x = ball->x - halfSize,
.y = ball->y - halfSize,
.w = size,
.h = size,
};
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &ballRect);
}
void UpdateBall(Ball *ball, float elapsed)
{
if (!started)
{
ball->x = WIDTH / 2;
ball->y = HEIGHT / 2;
return;
}
ball->x += ball->xSpeed * elapsed;
ball->y += ball->ySpeed * elapsed;
// to restrict the ball inside the window
if (ball->x < BALLSIZE / 2)
{
UpdateScore(2, 100);
}
if (ball->x > WIDTH - BALLSIZE / 2)
{
UpdateScore(1, 100);
}
if (ball->y < BALLSIZE / 2)
{
ball->ySpeed = abs(ball->ySpeed);
}
if (ball->y > HEIGHT - 60 - BALLSIZE / 2)
{
ball->ySpeed = -abs(ball->ySpeed);
}
}
Player MakePlayer(void)
{
Player player = {
.yPosition = HEIGHT / 2,
};
return player;
}
void UpdatePlayers(float elapsed)
{
const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
if (keyboardState[SDL_SCANCODE_SPACE])
{
started = true;
}
// probability
if ((player1.yPosition + PLAYERHEIGHT / 2) > (ball.y + BALLSIZE / 2) && rand() % 2)
{
if ((player1.yPosition > PLAYERHEIGHT / 2 + 10))
player1.yPosition -= 8;
}
else if ((player1.yPosition + PLAYERHEIGHT / 2) < (ball.y + BALLSIZE / 2) && rand() % 2)
{
if (player1.yPosition < HEIGHT - PLAYERHEIGHT / 2 - 70)
player1.yPosition += 8;
}
if (keyboardState[SDL_SCANCODE_UP])
{
if (player2.yPosition > PLAYERHEIGHT / 2 + 10)
player2.yPosition -= PLAYER_MOVE_SPEED * elapsed;
}
if (keyboardState[SDL_SCANCODE_DOWN])
{
if (player2.yPosition < HEIGHT - PLAYERHEIGHT / 2 - 70)
player2.yPosition += PLAYER_MOVE_SPEED * elapsed;
}
// check if ball collides with bars
SDL_Rect ballRect = {
.x = ball.x - ball.size / 2,
.y = ball.y - ball.size / 2,
.w = ball.size,
.h = ball.size,
};
SDL_Rect player1rect = {
.x = PLAYERMARGIN,
.y = (int)(player1.yPosition) - PLAYERHEIGHT / 2,
.w = PLAYERWIDTH,
.h = PLAYERHEIGHT,
};
if (SDL_HasIntersection(&ballRect, &player1rect))
{
ball.xSpeed = abs(ball.xSpeed); // makes ball go right
}
SDL_Rect player2rect = {
.x = WIDTH - PLAYERWIDTH - PLAYERMARGIN,
.y = (int)(player2.yPosition) - PLAYERHEIGHT / 2,
.w = PLAYERWIDTH,
.h = PLAYERHEIGHT,
};
if (SDL_HasIntersection(&ballRect, &player2rect))
{
ball.xSpeed = -abs(ball.xSpeed); // makes ball go left
}
}
void RenderPlayers(void)
{
// Render Player 1 (left, red)
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_Rect player1rect = {
.x = PLAYERMARGIN,
.y = (int)(player1.yPosition) - PLAYERHEIGHT / 2,
.w = PLAYERWIDTH,
.h = PLAYERHEIGHT,
};
SDL_RenderFillRect(renderer, &player1rect);
// Render Player 2 (right, blue)
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_Rect player2rect = {
.x = WIDTH - PLAYERWIDTH - PLAYERMARGIN,
.y = (int)(player2.yPosition) - PLAYERHEIGHT / 2,
.w = PLAYERWIDTH,
.h = PLAYERHEIGHT,
};
SDL_RenderFillRect(renderer, &player2rect);
}
void UpdateScore(int player, int points)
{
// to reinitialize the game
started = false;
ball.xSpeed += 10;
ball.ySpeed += 10;
if (player == 1)
{
player1Score = true;
player2Score = false;
player1.score += points;
fprintf(file, "---> Bot scored a point --- 😑😒\n");
fprintf(file, "-------------------------------------\n");
if (score.x > 30)
score.x -= 50;
}
if (player == 2)
{
player2Score = true;
player1Score = false;
player2.score += points;
fprintf(file, "---> You scored a point --- 😎😏\n");
fprintf(file, "-------------------------------------\n");
if (score.x + 120 < WIDTH)
score.x += 50;
}
char *formatString = "Player 1: %d || Player 2: %d";
int len = snprintf(NULL, 0, formatString, player1.score, player2.score);
char buf[len + 1];
snprintf(buf, len + 1, formatString, player1.score, player2.score);
SDL_SetWindowTitle(window, buf);
}
void RenderLines(float yPos)
{
SDL_Rect lineRect = {
.x = (WIDTH / 2) - 1,
.y = yPos,
.w = 2,
.h = 20,
};
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &lineRect);
}