-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.c
769 lines (523 loc) · 12.5 KB
/
pong.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
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
//Using libs SDL, glibc
#include <SDL.h> //SDL version 2.0
#include <stdlib.h>
#include <stdio.h>
#define SCREEN_WIDTH 640 //window height
#define SCREEN_HEIGHT 480 //window width
//function prototypes
//initilise SDL
int init(int w, int h, int argc, char *args[]);
typedef struct ball_s {
int x, y; /* position on the screen */
int w,h; // ball width and height
int dx, dy; /* movement vector */
} ball_t;
typedef struct paddle {
int x,y;
int w,h;
} paddle_t;
// Program globals
static ball_t ball;
static paddle_t paddle[2];
int score[] = {0,0};
int width, height; //used if fullscreen
SDL_Window* window = NULL; //The window we'll be rendering to
SDL_Renderer *renderer; //The renderer SDL will use to draw to the screen
//surfaces
static SDL_Surface *screen;
static SDL_Surface *title;
static SDL_Surface *numbermap;
static SDL_Surface *end;
//textures
SDL_Texture *screen_texture;
//inisilise starting position and sizes of game elemements
static void init_game() {
ball.x = screen->w / 2;
ball.y = screen->h / 2;
ball.w = 10;
ball.h = 10;
ball.dy = 1;
ball.dx = 1;
paddle[0].x = 20;
paddle[0].y = screen->h / 2 - 50 ;
paddle[0].w = 10;
paddle[0].h = 50;
paddle[1].x = screen->w - 20 - 10;
paddle[1].y = screen->h / 2 - 50;
paddle[1].w = 10;
paddle[1].h = 50;
}
int check_score() {
int i;
//loop through player scores
for(i = 0; i < 2; i++) {
//check if score is @ the score win limit
if (score[i] == 10 ) {
//reset scores
score[0] = 0;
score[1] = 0;
//return 1 if player 1 score @ limit
if (i == 0) {
return 1;
//return 2 if player 2 score is @ limit
} else {
return 2;
}
}
}
//return 0 if no one has reached a score of 10 yet
return 0;
}
//if return value is 1 collision occured. if return is 0, no collision.
int check_collision(ball_t a, paddle_t b) {
int left_a, left_b;
int right_a, right_b;
int top_a, top_b;
int bottom_a, bottom_b;
left_a = a.x;
right_a = a.x + a.w;
top_a = a.y;
bottom_a = a.y + a.h;
left_b = b.x;
right_b = b.x + b.w;
top_b = b.y;
bottom_b = b.y + b.h;
if (left_a > right_b) {
return 0;
}
if (right_a < left_b) {
return 0;
}
if (top_a > bottom_b) {
return 0;
}
if (bottom_a < top_b) {
return 0;
}
return 1;
}
/* This routine moves each ball by its motion vector. */
static void move_ball() {
/* Move the ball by its motion vector. */
ball.x += ball.dx;
ball.y += ball.dy;
/* Turn the ball around if it hits the edge of the screen. */
if (ball.x < 0) {
score[1] += 1;
init_game();
}
if (ball.x > screen->w - 10) {
score[0] += 1;
init_game();
}
if (ball.y < 0 || ball.y > screen->h - 10) {
ball.dy = -ball.dy;
}
//check for collision with the paddle
int i;
for (i = 0; i < 2; i++) {
int c = check_collision(ball, paddle[i]);
//collision detected
if (c == 1) {
//ball moving left
if (ball.dx < 0) {
ball.dx -= 1;
//ball moving right
} else {
ball.dx += 1;
}
//change ball direction
ball.dx = -ball.dx;
//change ball angle based on where on the paddle it hit
int hit_pos = (paddle[i].y + paddle[i].h) - ball.y;
if (hit_pos >= 0 && hit_pos < 7) {
ball.dy = 4;
}
if (hit_pos >= 7 && hit_pos < 14) {
ball.dy = 3;
}
if (hit_pos >= 14 && hit_pos < 21) {
ball.dy = 2;
}
if (hit_pos >= 21 && hit_pos < 28) {
ball.dy = 1;
}
if (hit_pos >= 28 && hit_pos < 32) {
ball.dy = 0;
}
if (hit_pos >= 32 && hit_pos < 39) {
ball.dy = -1;
}
if (hit_pos >= 39 && hit_pos < 46) {
ball.dy = -2;
}
if (hit_pos >= 46 && hit_pos < 53) {
ball.dy = -3;
}
if (hit_pos >= 53 && hit_pos <= 60) {
ball.dy = -4;
}
//ball moving right
if (ball.dx > 0) {
//teleport ball to avoid mutli collision glitch
if (ball.x < 30) {
ball.x = 30;
}
//ball moving left
} else {
//teleport ball to avoid mutli collision glitch
if (ball.x > 600) {
ball.x = 600;
}
}
}
}
}
static void move_paddle_ai() {
int center = paddle[0].y + 25;
int screen_center = screen->h / 2 - 25;
int ball_speed = ball.dy;
if (ball_speed < 0) {
ball_speed = -ball_speed;
}
//ball moving right
if (ball.dx > 0) {
//return to center position
if (center < screen_center) {
paddle[0].y += ball_speed;
} else {
paddle[0].y -= ball_speed;
}
//ball moving left
} else {
//ball moving down
if (ball.dy > 0) {
if (ball.y > center) {
paddle[0].y += ball_speed;
} else {
paddle[0].y -= ball_speed;
}
}
//ball moving up
if (ball.dy < 0) {
if (ball.y < center) {
paddle[0].y -= ball_speed;
} else {
paddle[0].y += ball_speed;
}
}
//ball moving stright across
if (ball.dy == 0) {
if (ball.y < center) {
paddle[0].y -= 5;
} else {
paddle[0].y += 5;
}
}
}
}
static void move_paddle(int d) {
// if the down arrow is pressed move paddle down
if (d == 0) {
if(paddle[1].y >= screen->h - paddle[1].h) {
paddle[1].y = screen->h - paddle[1].h;
} else {
paddle[1].y += 5;
}
}
// if the up arrow is pressed move paddle up
if (d == 1) {
if(paddle[1].y <= 0) {
paddle[1].y = 0;
} else {
paddle[1].y -= 5;
}
}
}
static void draw_game_over(int p) {
SDL_Rect p1;
SDL_Rect p2;
SDL_Rect cpu;
SDL_Rect dest;
p1.x = 0;
p1.y = 0;
p1.w = end->w;
p1.h = 75;
p2.x = 0;
p2.y = 75;
p2.w = end->w;
p2.h = 75;
cpu.x = 0;
cpu.y = 150;
cpu.w = end->w;
cpu.h = 75;
dest.x = (screen->w / 2) - (end->w / 2);
dest.y = (screen->h / 2) - (75 / 2);
dest.w = end->w;
dest.h = 75;
switch (p) {
case 1:
SDL_BlitSurface(end, &p1, screen, &dest);
break;
case 2:
SDL_BlitSurface(end, &p2, screen, &dest);
break;
default:
SDL_BlitSurface(end, &cpu, screen, &dest);
}
}
static void draw_menu() {
SDL_Rect src;
SDL_Rect dest;
src.x = 0;
src.y = 0;
src.w = title->w;
src.h = title->h;
dest.x = (screen->w / 2) - (src.w / 2);
dest.y = (screen->h / 2) - (src.h / 2);
dest.w = title->w;
dest.h = title->h;
SDL_BlitSurface(title, &src, screen, &dest);
}
static void draw_background() {
SDL_Rect src;
//draw bg with net
src.x = 0;
src.y = 0;
src.w = screen->w;
src.h = screen->h;
//draw the backgorund
//int r = SDL_FillRect(screen,&src,0);
//if (r !=0){
// printf("fill rectangle faliled in func draw_background()");
//}
}
static void draw_net() {
SDL_Rect net;
net.x = screen->w / 2;
net.y = 20;
net.w = 5;
net.h = 15;
//draw the net
int i,r;
for(i = 0; i < 15; i++) {
r = SDL_FillRect(screen, &net, 0xffffffff);
if (r != 0) {
printf("fill rectangle faliled in func draw_net()");
}
net.y = net.y + 30;
}
}
static void draw_ball() {
SDL_Rect src;
src.x = ball.x;
src.y = ball.y;
src.w = ball.w;
src.h = ball.h;
int r = SDL_FillRect(screen , &src, 0xffffffff);
if (r !=0){
printf("fill rectangle faliled in func drawball()");
}
}
static void draw_paddle() {
SDL_Rect src;
int i;
for (i = 0; i < 2; i++) {
src.x = paddle[i].x;
src.y = paddle[i].y;
src.w = paddle[i].w;
src.h = paddle[i].h;
int r = SDL_FillRect(screen, &src, 0xffffffff);
if (r !=0){
printf("fill rectangle faliled in func draw_paddle()");
}
}
}
static void draw_player_0_score() {
SDL_Rect src;
SDL_Rect dest;
src.x = 0;
src.y = 0;
src.w = 64;
src.h = 64;
dest.x = (screen->w / 2) - src.w - 12; //12 is just padding spacing
dest.y = 0;
dest.w = 64;
dest.h = 64;
if (score[0] > 0 && score[0] < 10) {
src.x += src.w * score[0];
}
SDL_BlitSurface(numbermap, &src, screen, &dest);
}
static void draw_player_1_score() {
SDL_Rect src;
SDL_Rect dest;
src.x = 0;
src.y = 0;
src.w = 64;
src.h = 64;
dest.x = (screen->w / 2) + 12;
dest.y = 0;
dest.w = 64;
dest.h = 64;
if (score[1] > 0 && score[1] < 10) {
src.x += src.w * score[1];
}
SDL_BlitSurface(numbermap, &src, screen, &dest);
}
int main (int argc, char *args[]) {
//SDL Window setup
if (init(SCREEN_WIDTH, SCREEN_HEIGHT, argc, args) == 1) {
return 0;
}
SDL_GetWindowSize(window, &width, &height);
int sleep = 0;
int quit = 0;
int state = 0;
int r = 0;
Uint32 next_game_tick = SDL_GetTicks();
// Initialize the ball position data.
init_game();
//render loop
while(quit == 0) {
//check for new events every frame
SDL_PumpEvents();
const Uint8 *keystate = SDL_GetKeyboardState(NULL);
if (keystate[SDL_SCANCODE_ESCAPE]) {
quit = 1;
}
if (keystate[SDL_SCANCODE_DOWN]) {
move_paddle(0);
}
if (keystate[SDL_SCANCODE_UP]) {
move_paddle(1);
}
//draw background
SDL_RenderClear(renderer);
SDL_FillRect(screen, NULL, 0x000000ff);
//display main menu
if (state == 0 ) {
if (keystate[SDL_SCANCODE_SPACE]) {
state = 1;
}
//draw menu
draw_menu();
//display gameover
} else if (state == 2) {
if (keystate[SDL_SCANCODE_SPACE]) {
state = 0;
//delay for a little bit so the space bar press dosnt get triggered twice
//while the main menu is showing
SDL_Delay(500);
}
if (r == 1) {
//if player 1 is AI if player 1 was human display the return value of r not 3
draw_game_over(3);
} else {
//display gameover
draw_game_over(r);
}
//display the game
} else if (state == 1) {
//check score
r = check_score();
//if either player wins, change to game over state
if (r == 1) {
state = 2;
} else if (r == 2) {
state = 2;
}
//paddle ai movement
move_paddle_ai();
//* Move the balls for the next frame.
move_ball();
//draw net
draw_net();
//draw paddles
draw_paddle();
//* Put the ball on the screen.
draw_ball();
//draw the score
draw_player_0_score();
//draw the score
draw_player_1_score();
}
SDL_UpdateTexture(screen_texture, NULL, screen->pixels, screen->w * sizeof (Uint32));
SDL_RenderCopy(renderer, screen_texture, NULL, NULL);
//draw to the display
SDL_RenderPresent(renderer);
//time it takes to render frame in milliseconds
next_game_tick += 1000 / 60;
sleep = next_game_tick - SDL_GetTicks();
if( sleep >= 0 ) {
SDL_Delay(sleep);
}
}
//free loaded images
SDL_FreeSurface(screen);
SDL_FreeSurface(title);
SDL_FreeSurface(numbermap);
SDL_FreeSurface(end);
//free renderer and all textures used with it
SDL_DestroyRenderer(renderer);
//Destroy window
SDL_DestroyWindow(window);
//Quit SDL subsystems
SDL_Quit();
return 0;
}
int init(int width, int height, int argc, char *args[]) {
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return 1;
}
int i;
for (i = 0; i < argc; i++) {
//Create window
if(strcmp(args[i], "-f")) {
SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN, &window, &renderer);
} else {
SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_FULLSCREEN_DESKTOP, &window, &renderer);
}
}
if (window == NULL) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return 1;
}
//create the screen sruface where all the elemnts will be drawn onto (ball, paddles, net etc)
screen = SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_RGBA32);
if (screen == NULL) {
printf("Could not create the screen surfce! SDL_Error: %s\n", SDL_GetError());
return 1;
}
//create the screen texture to render the screen surface to the actual display
screen_texture = SDL_CreateTextureFromSurface(renderer, screen);
if (screen_texture == NULL) {
printf("Could not create the screen_texture! SDL_Error: %s\n", SDL_GetError());
return 1;
}
//Load the title image
title = SDL_LoadBMP("title.bmp");
if (title == NULL) {
printf("Could not Load title image! SDL_Error: %s\n", SDL_GetError());
return 1;
}
//Load the numbermap image
numbermap = SDL_LoadBMP("numbermap.bmp");
if (numbermap == NULL) {
printf("Could not Load numbermap image! SDL_Error: %s\n", SDL_GetError());
return 1;
}
//Load the gameover image
end = SDL_LoadBMP("gameover.bmp");
if (end == NULL) {
printf("Could not Load title image! SDL_Error: %s\n", SDL_GetError());
return 1;
}
// Set the title colourkey.
Uint32 colorkey = SDL_MapRGB(title->format, 255, 0, 255);
SDL_SetColorKey(title, SDL_TRUE, colorkey);
SDL_SetColorKey(numbermap, SDL_TRUE, colorkey);
return 0;
}