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
|
#include "SDL2/SDL.h"
//#include "SDL2/SDL_ttf.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "stdbool.h"
bool checkCollision(SDL_Rect a, SDL_Rect b){
int leftA, leftB, rightA, rightB, topA, topB, bottomA, bottomB;
leftA = a.x;
rightA = a.x + a.w;
topA = a.y;
bottomA = a.y + a.h;
leftB = b.x;
rightB = b.x + b.w;
topB = b.y;
bottomB = b.y + b.h;
if(bottomA <= topB){
return false;
}
if(topA >= bottomB){
return false;
}
if(rightA <= leftB){
return false;
}
if(leftA >= rightB){
return false;
}
return true;
}
int main(int argc, char ** argv) {
SDL_Window *window;
SDL_Renderer *renderer;
int fps = 60;
int ticksPframe = 1000 / fps;
bool quit = false;
int p1vel = 0, p2vel = 0;
int ballvelx = 0, ballvely = 0;
int vel = 10;
// int p1score = 0, p2score = 0;
//TTF_Init;
SDL_Rect p1;
SDL_Rect p2;
SDL_Rect ball;
// SDL_Rect p1ScoreRect;
// SDL_Rect p2ScoreRect;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
// TTF_Font * text;
// SDL_Surface* p1SurfaceMsg;
// SDL_Surface* p2SurfaceMsg;
// SDL_Texture* p1ScoreMsg;
// SDL_Texture* p2ScoreMsg;
// SDL_Color color={0, 255, 0, 255};
//Creating the window and renderer
window = SDL_CreateWindow("Pong", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1000, 750, SDL_WINDOW_OPENGL);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
p1.w = 20;
p1.h = 150;
p1.x = 10;
p1.y = 300;
p2.w = 20;
p2.h = 150;
p2.x = 970;
p2.y = 300;
ball.w = 10;
ball.h = 10;
ball.x = 490;
ball.y = 365;
// p1ScoreRect.x = 450;
// p1ScoreRect.y = 10;
// p1ScoreRect.w = 40;
// p1ScoreRect.h = 50;
//
// p2ScoreRect.x = 510;
// p1ScoreRect.y = 10;
// p1ScoreRect.w = 40;
// p1ScoreRect.h = 50;
//
// text = TTF_OpenFont("NotoSerif-BlackItalic.ttf", 24);
// p1SurfaceMsg = TTF_RenderText_Solid(text, p1score, color);
// p2SurfaceMsg = TTF_RenderText_Solid(text, p2score, color);
// p1ScoreMsg = SDL_CreateTextureFromSurface(renderer, p1SurfaceMsg);
// p2ScoreMsg = SDL_CreateTextureFromSurface(renderer, p2SurfaceMsg);
ballvelx = -4;
ballvely = 5;
while(!quit){
//Check first frame to compare later for smooth framerate
int startFrame = SDL_GetTicks();
//Handling keyboard events
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_w:
p1vel = -vel;
break;
case SDLK_s:
p1vel = vel;
break;
case SDLK_i:
p2vel = -vel;
break;
case SDLK_k:
p2vel = vel;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym){
case SDLK_s:
if(p1vel > 0){p1vel = 0;}
break;
case SDLK_w:
if(p1vel < 0){p1vel = 0;}
break;
case SDLK_k:
if(p2vel > 0){p2vel = 0;}
break;
case SDLK_i:
if(p2vel < 0){p2vel = 0;}
break;
}
break;
case SDL_QUIT:
quit = true;
break;
}
}
SDL_Delay(1);
if(checkCollision(ball, p1) == true || checkCollision(ball, p2) == true){
ballvelx = -ballvelx * 1.1;
}
if(ball.y <= 0 || ball.y >= 740){
ballvely = -ballvely * 1.1;
}
if(ball.x <= 0){
//p2score++;
p1.w = 20;
p1.h = 150;
p1.x = 10;
p1.y = 300;
p2.w = 20;
p2.h = 150;
p2.x = 970;
p2.y = 300;
ball.w = 10;
ball.h = 10;
ball.x = 490;
ball.y = 365;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderDrawRect(renderer, &p1);
SDL_RenderDrawRect(renderer, &p2);
SDL_RenderDrawRect(renderer, &ball);
SDL_RenderDrawLine(renderer, 500, 0, 500, 740);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
}
if(ball.x + ball.w >= 1000){
//p1score++;
p1.w = 20;
p1.h = 150;
p1.x = 10;
p1.y = 300;
p2.w = 20;
p2.h = 150;
p2.x = 970;
p2.y = 300;
ball.w = 10;
ball.h = 10;
ball.x = 490;
ball.y = 365;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderDrawRect(renderer, &p1);
SDL_RenderDrawRect(renderer, &p2);
SDL_RenderDrawRect(renderer, &ball);
SDL_RenderDrawLine(renderer, 500, 0, 500, 740);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
}
//Updating position
p1.y += p1vel;
p2.y += p2vel;
ball.x += ballvelx;
ball.y += ballvely;
//Drawing the scene
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderDrawRect(renderer, &p1);
SDL_RenderDrawRect(renderer, &p2);
SDL_RenderDrawRect(renderer, &ball);
SDL_RenderDrawLine(renderer, 500, 0, 500, 740);
SDL_RenderPresent(renderer);
// Checking the last codes tick will help us make out how much time we've to wait before starting the next frame
// Note that this isn't a perfect solution and it could be improved upon since it only really works for faster machines, however it works for something simple like this
int lastFrame = SDL_GetTicks();
if(lastFrame - startFrame < ticksPframe){
SDL_Delay(ticksPframe - (lastFrame - startFrame));
}
}
// TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
|