-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht.cpp
More file actions
470 lines (387 loc) · 14.1 KB
/
t.cpp
File metadata and controls
470 lines (387 loc) · 14.1 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
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
//for running the game use the following cmd in linux after installing sfml in linux using command -> sudo apt-get install libsfml-dev
//for compiling the program use command -> g++ t.cpp -lsfml-graphics -lsfml-window -lsfml-system -o t
//to run the executible use command -> ./t
#include <iostream> //l226726 Maaz Khan
#include <unistd.h>
#include <pthread.h>
#include <string>
#include <SFML/Graphics.hpp>
using namespace std;
// GLOBAL VARIABLES
int paddle1_movement = 0;
int paddle2_movement = 0;
int playerOption = 0;
bool ballWaiting = false;
bool pointScored = false;
bool gameWon = false;
int serviceTurn = 1;
int paddle1ScoreCount=0;
int paddle2ScoreCount=0;
int ballSpeed=0;
// Define structs to hold arguments for the threads functions
struct Thread1Args //for ball
{
sf::Vector2f* ballPosition;
sf::Vector2f* ballVelocity;
sf::RenderWindow* window;
sf::Vector2f* paddle1Position;
sf::Vector2f* paddle2Position;
};
struct Thread2Args //for paddle1
{
sf::Vector2f* paddle1Position;
sf::RenderWindow* window;
};
struct Thread3Args //for paddle2
{
sf::Vector2f* paddle2Position;
sf::RenderWindow* window;
};
void* moveBall(void* arg)
{
Thread1Args* args = static_cast<Thread1Args*>(arg);
while (args->window->isOpen())
{
// Update ball position
if(!ballWaiting)
{
*(args->ballPosition) += *(args->ballVelocity);
}
if (args->ballPosition->y <= 0 || args->ballPosition->y >= args->window->getSize().y)
{
args->ballVelocity->y = -args->ballVelocity->y; // Reverse vertical velocity on wall collision
}
// Check for collisions with paddles
sf::Vector2f ballPos = *(args->ballPosition);
sf::Vector2f paddle1Pos = *(args->paddle1Position);
sf::Vector2f paddle2Pos = *(args->paddle2Position);
// Collision with paddle1 //30 is the width of the paddle so we are checking touchness of the ball
if (ballPos.x >= paddle1Pos.x - 10 && ballPos.x <= paddle1Pos.x + 30 && ballPos.y >= paddle1Pos.y && ballPos.y <= paddle1Pos.y + 200)
{
// Calculate the reflection angle and adjust ball velocity
if(ballPos.y >= paddle1Pos.y && ballPos.y <= paddle1Pos.y + 60)
{
args->ballVelocity->x = -1;
args->ballVelocity->y = -1;
}
else if(ballPos.y > paddle1Pos.y + 60 && ballPos.y <= paddle1Pos.y + 130){
args->ballVelocity->x = -1;
args->ballVelocity->y = 0;
}
else if(ballPos.y > paddle1Pos.y + 130 && ballPos.y <= paddle1Pos.y + 200){
args->ballVelocity->x = -1;
args->ballVelocity->y = 1; // Adjust ball speed as needed
}
}
// Collision with paddle2
if (ballPos.x <= paddle2Pos.x + 30 && ballPos.x >= paddle2Pos.x && ballPos.y >= paddle2Pos.y && ballPos.y <= paddle2Pos.y + 200)
{
// Calculate the reflection angle and adjust ball velocity
if(ballPos.y >= paddle2Pos.y && ballPos.y <= paddle2Pos.y + 60)
{
args->ballVelocity->x = 1;
args->ballVelocity->y = -1;
}
else if(ballPos.y > paddle2Pos.y + 60 && ballPos.y <= paddle2Pos.y + 130){
args->ballVelocity->x = 1;
args->ballVelocity->y = 0;
}
else if(ballPos.y > paddle2Pos.y + 130 && ballPos.y <= paddle2Pos.y + 200){
args->ballVelocity->x = 1;
args->ballVelocity->y = 1; // Adjust ball speed as needed
}
}
// Check if the ball is missed by the paddles
if(args->ballPosition->x <= 0 || args->ballPosition->x >= args->window->getSize().x)
{
// Set pointScored to true to indicate that a point has been scored
pointScored = true;
// Set ballWaiting to true to stop the ball movement
ballWaiting = true;
if(ballPos.x<=0)
{
serviceTurn=1;
paddle1ScoreCount++;
}
else
{
serviceTurn=2;
paddle2ScoreCount++;
}
// Reset the ball position to the center
args->ballPosition->x = args->window->getSize().x / 2;
args->ballPosition->y = args->window->getSize().y / 2;
//whoever makes point the next service will be from him...
if(serviceTurn==1)
{
args->ballVelocity->x = 1; // Reset ball velocity
args->ballVelocity->y = 0; // Reset ball velocity
}
else{
args->ballVelocity->x = -1; // Reset ball velocity
args->ballVelocity->y = 0; // Reset ball velocity
}
//or we can use this syntax for doing the same thing i.e setting ball velocity for service appropriately..
//args->ballVelocity->x=serviceTurn==1 ? 1:-1; //it will return these options | first option is for true case and second option is for false case..
//args->ballvelocity->y=0;
// Delay for displaying point label
usleep(1000000); //1 second
if(paddle1ScoreCount==5 || paddle2ScoreCount==5)
{
gameWon=true;
usleep(3000000);
// Reset the scores
paddle1ScoreCount = 0;
paddle2ScoreCount = 0;
gameWon=false;
}
// Reset pointScored and ballWaiting after delay
pointScored = false;
ballWaiting = false;
}
if(ballSpeed==1)
// Sleep for a short duration to control ball speed
usleep(900); // Adjust as needed for controlling the motion or speed of the ball..
else
usleep(500);
}
return nullptr;
}
void* movePaddle1(void* arg)
{
Thread2Args* args = static_cast<Thread2Args*>(arg);
while (args->window->isOpen())
{
//moving paddle
if (paddle1_movement==-1)
{
args->paddle1Position->y -= 1;
}
if (paddle1_movement==1)
{
args->paddle1Position->y += 1;
}
// Sleep to avoid consuming excessive CPU resources
usleep(500); // Adjust as needed
}
return nullptr;
}
void* movePaddle2(void* arg)
{
Thread3Args* args = static_cast<Thread3Args*>(arg);
while (args->window->isOpen())
{
if(playerOption==2)
{
//moving paddle
if (paddle2_movement==-1)
{
args->paddle2Position->y -= 1;
}
if (paddle2_movement==1)
{
args->paddle2Position->y += 1;
}
// Sleep to avoid consuming excessive CPU resources
usleep(500); // Adjust as needed
}
else if(playerOption == 1) //automatically move paddle up and down randomly....
{
bool movingUp=true;
bool movingDown=false;
while(args->window->isOpen()) //we havenot done while(1) because it does not terminate and terminal have to be close and open again to be for use...
{
if(movingUp)
{
args->paddle2Position->y -= 1;
if(args->paddle2Position->y < 0)
{
movingDown=true;
movingUp=false;
}
usleep(200);
}
if(movingDown)
{
args->paddle2Position->y += 1;
if(args->paddle2Position->y > 815)
{
movingUp=true;
movingDown=false;
}
usleep(200);
}
}
}
}
return nullptr;
}
int main()
{
cout << "\n\n_________WELCOME TO PONG GAME!_________\n\nINPUTS ( 1 -> single player | 2 -> two player )\nChoice: ";
while (cin >> playerOption)
{
if (playerOption == 1 || playerOption == 2)
break;
else
cout << "\n\nError: invalid input made\n input again : ";
}
cout<<"Alright! Now select ball motion speed :\n( 1 -> slow | 2 -> fast )\nChoice: ";
while (cin >> ballSpeed)
{
if (ballSpeed == 1 || ballSpeed == 2)
break;
else
cout << "\n\nError: invalid input made\n input again : ";
}
// Create the game window
sf::RenderWindow window(sf::VideoMode(1900, 1000), "- P O N G -");
// Initialize ball position and velocity
sf::Vector2f ballPosition(window.getSize().x / 2, window.getSize().y / 2);
sf::Vector2f ballVelocity(1, 0); // Adjust velocity as needed //initial direction of the ball..
sf::Vector2f paddle1Position(1890, ((window.getSize().y / 2) - 100)); //-100 for completely in middle vertically..
sf::Vector2f paddle2Position(0, ((window.getSize().y / 2) - 100)); //-100 for completely in middle vertically..
// Draw game objects
sf::CircleShape ball(10);
sf::RectangleShape paddle1(sf::Vector2f(30, 200));
sf::RectangleShape paddle2(sf::Vector2f(30, 200));
sf::Font font;
if(!font.loadFromFile("Bonza.otf")) //load font from file
{
return EXIT_FAILURE;
}
sf::Text scoreLabel;
scoreLabel.setFont(font);
scoreLabel.setString("( SCORE )");
scoreLabel.setCharacterSize(30);
scoreLabel.setFillColor(sf::Color::White);
scoreLabel.setPosition(910,10);
// Create a thread1 argument struct
Thread1Args args1;
args1.ballPosition = &ballPosition;
args1.ballVelocity = &ballVelocity;
args1.paddle1Position = &paddle1Position;
args1.paddle2Position = &paddle2Position;
args1.window = &window;
pthread_t thread1;
pthread_create(&thread1, nullptr, moveBall, &args1);
// Create a thread2 argument struct
Thread2Args args2;
args2.paddle1Position = &paddle1Position;
args2.window = &window;
pthread_t thread2;
pthread_create(&thread2, nullptr, movePaddle1, &args2);
// Create a thread3 argument struct
Thread3Args args3;
args3.paddle2Position = &paddle2Position;
args3.window = &window;
pthread_t thread3;
pthread_create(&thread3, nullptr, movePaddle2, &args3);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
ball.setFillColor(sf::Color::Red);
ball.setPosition(ballPosition);
paddle1.setFillColor(sf::Color::Green);
paddle1.setPosition(paddle1Position);
paddle2.setFillColor(sf::Color::Blue);
paddle2.setPosition(paddle2Position);
// Handle paddle1 movement
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && paddle1Position.y > 0)
{
paddle1_movement=-1;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && paddle1Position.y < 815)
{
paddle1_movement=1;
}
else{
paddle1_movement=0;
}
if(playerOption==2)
{
// Handle paddle2 movement
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && paddle2Position.y > 0)
{
paddle2_movement=-1;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && paddle2Position.y < 815)
{
paddle2_movement=1;
}
else{
paddle2_movement=0;
}
}
// Clear the window
window.clear(sf::Color::Black);
string paddle1ScoreStr = std::to_string(paddle1ScoreCount);
sf::Text paddle1ScoreInteger;
paddle1ScoreInteger.setFont(font);
paddle1ScoreInteger.setString(paddle1ScoreStr);
paddle1ScoreInteger.setCharacterSize(25);
paddle1ScoreInteger.setFillColor(sf::Color::Green);
paddle1ScoreInteger.setPosition(1000,60);
string paddle2ScoreStr = std::to_string(paddle2ScoreCount);
sf::Text paddle2ScoreInteger;
paddle2ScoreInteger.setFont(font);
paddle2ScoreInteger.setString(paddle2ScoreStr);
paddle2ScoreInteger.setCharacterSize(25);
paddle2ScoreInteger.setFillColor(sf::Color::Blue);
paddle2ScoreInteger.setPosition(907,60);
if(!ballWaiting)
{
window.draw(ball);
}
else
{
if (gameWon)
{
// Display the victory message
sf::Text victoryMessage;
victoryMessage.setFont(font);
victoryMessage.setCharacterSize(100);
victoryMessage.setFillColor(sf::Color::Yellow);
victoryMessage.setPosition(750, 400);
if (paddle1ScoreCount == 5)
{
victoryMessage.setString("Paddle 1 WON!");
}
else
{
victoryMessage.setString("Paddle 2 WON!");
}
window.draw(victoryMessage);
}
else
{
// Display the point message
sf::Text pointLabel;
pointLabel.setFont(font);
pointLabel.setString("POINT!");
pointLabel.setCharacterSize(100);
pointLabel.setFillColor(sf::Color::Red);
pointLabel.setPosition(855, 400);
window.draw(pointLabel);
}
}
window.draw(paddle1);
window.draw(paddle2);
window.draw(scoreLabel);
window.draw(paddle1ScoreInteger);
window.draw(paddle2ScoreInteger);
window.display();
}
// Wait for the threads to finish
pthread_join(thread1, nullptr);
pthread_join(thread2, nullptr);
pthread_join(thread3, nullptr);
return 0;
}