-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.cc
295 lines (256 loc) · 10.7 KB
/
pong.cc
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
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cmath>
#include <ctime>
#include <cstdlib>
////////////////////////////////////////////////////////////
// Application entry
////////////////////////////////////////////////////////////
int random(int min, int max)
{
int randNum = rand()%(max-min + 1) + min;
return randNum;
}
int main()
{
std::srand(static_cast<unsigned int>(std::time(NULL)));
// Define some constants
const float pi = 3.14159f;
//const float gameWidth = 800.f;
//const float gameHeight = 600.f;
float gameWidth = 800.f;
float gameHeight = 600.f;
sf::Vector2f paddleSize(25, 100);
float ballRadius = 10.f;
// Define score
int score = 0;
// Create the window of the application
sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "Pong!",
sf::Style::Default);
// Enable Vertical Sync
// window.setVerticalSyncEnabled(true);
// Load the sounds used in the game
sf::SoundBuffer paddleSoundBuffer;
if (!paddleSoundBuffer.loadFromFile("resources/paddle_hit.wav"))
return EXIT_FAILURE;
sf::Sound paddleSound(paddleSoundBuffer);
sf::SoundBuffer wallSoundBuffer;
if(!wallSoundBuffer.loadFromFile("resources/wall_hit.wav"))
return EXIT_FAILURE;
sf::Sound wallSound(wallSoundBuffer);
// Create the left paddle
sf::RectangleShape leftPaddle;
leftPaddle.setSize(paddleSize - sf::Vector2f(3, 3));
leftPaddle.setOutlineThickness(3);
leftPaddle.setOutlineColor(sf::Color::Black);
leftPaddle.setFillColor(sf::Color::White);
leftPaddle.setOrigin(paddleSize / 2.f);
// Create the right paddle
sf::RectangleShape rightPaddle;
rightPaddle.setSize(paddleSize - sf::Vector2f(3, 3));
rightPaddle.setOutlineThickness(3);
rightPaddle.setOutlineColor(sf::Color::Black);
rightPaddle.setFillColor(sf::Color::White);
rightPaddle.setOrigin(paddleSize / 2.f);
// Create the ball
sf::CircleShape ball;
ball.setRadius(ballRadius - 3);
ball.setOutlineThickness(3);
ball.setOutlineColor(sf::Color::Black);
ball.setFillColor(sf::Color::White);
ball.setOrigin(ballRadius / 2, ballRadius / 2);
// Load the text font
sf::Font font;
if (!font.loadFromFile("resources/NES-Chimera.otf"))
return EXIT_FAILURE;
// Initialize the pause message
sf::Text pauseMessage;
pauseMessage.setFont(font);
pauseMessage.setCharacterSize(30);
pauseMessage.setPosition(170.f, 150.f);
pauseMessage.setFillColor(sf::Color::White);
pauseMessage.setString("Welcome to Pong!\nPress space to start the game");
// Initialize score text
sf::Text scoreText;
scoreText.setFont(font);
scoreText.setCharacterSize(40);
scoreText.setPosition(gameWidth / 2, gameHeight / 4);
scoreText.setFillColor(sf::Color::White);
// Define the paddles properties
sf::Clock AITimer;
const sf::Time AITime = sf::seconds(0.f);
const float paddleSpeed = 400.f;
float rightPaddleSpeed = 0.f;
const float ballSpeed = 400.f;
float ballAngle = 0.f; // to be changed later
sf::Clock clock;
bool isPlaying = false;
while (window.isOpen())
{
gameHeight = window.getSize().y;
gameWidth = window.getSize().x;
pauseMessage.setPosition(gameWidth / 3 , gameHeight / 3);
scoreText.setPosition(gameWidth / 2, gameHeight / 7);
// Handle events
sf::Event event;
while (window.pollEvent(event))
{
// Window closed or escape key pressed: exit
if ((event.type == sf::Event::Closed) ||
((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
{
window.close();
break;
}
// Space key pressed: play
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space))
{
if (!isPlaying)
{
// (re)start the game
isPlaying = true;
clock.restart();
// Reset the position of the paddles and ball
leftPaddle.setPosition(10 + paddleSize.x / 2, gameHeight / 2);
rightPaddle.setPosition(gameWidth - 10 - paddleSize.x / 2, gameHeight / 2);
ball.setPosition(gameWidth / 2, gameHeight / 2);
// Reset score
score = 0;
// Reset the ball angle
do
{
// Make sure the ball initial angle is not too much vertical
ballAngle = (std::rand() % 360) * 2 * pi / 360;
}
while (std::abs(std::cos(ballAngle)) < 0.7f);
}
}
else if(event.type == sf::Event::Resized)
{
window.setView(sf::View(sf::FloatRect(0.f, 0.f,static_cast<float>(window.getSize().x), static_cast<float>(window.getSize().y))));
}
}
if (isPlaying)
{
float deltaTime = clock.restart().asSeconds();
// Move the player's paddle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) &&
(leftPaddle.getPosition().y - paddleSize.y / 2 > 5.f))
{
leftPaddle.move(0.f, -paddleSpeed * deltaTime);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) &&
(leftPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f))
{
leftPaddle.move(0.f, paddleSpeed * deltaTime);
}
// Move the computer's paddle
if (((rightPaddleSpeed < 0.f) && (rightPaddle.getPosition().y - paddleSize.y / 2 > 5.f)) ||
((rightPaddleSpeed > 0.f) && (rightPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f)))
{
rightPaddle.move(0.f, rightPaddleSpeed * deltaTime);
}
// Update the computer's paddle direction according to the ball position
if (AITimer.getElapsedTime() > AITime)
{
int rands = random(0, 500);
AITimer.restart();
if (ball.getPosition().y + ballRadius > rightPaddle.getPosition().y + paddleSize.y / 2)
{
rightPaddleSpeed = paddleSpeed - rands;
std::cout << rands << std::endl;
}
else if (ball.getPosition().y - ballRadius < rightPaddle.getPosition().y - paddleSize.y / 2)
{
rightPaddleSpeed = -paddleSpeed + rands;
std::cout << rands << std::endl;
}
else
rightPaddleSpeed = 0.f;
}
// Move the ball
float factor = ballSpeed * deltaTime;
ball.move(std::cos(ballAngle) * factor, std::sin(ballAngle) * factor);
// Check collisions between the ball and the screen
if (ball.getPosition().x - ballRadius < 0.f)
{
isPlaying = false;
pauseMessage.setString("You lost!\nPress space to restart or\nescape to exit\n\n Your score was " + std::to_string(score));
}
if (ball.getPosition().x + ballRadius > gameWidth)
{
isPlaying = false;
pauseMessage.setString("You won!\nPress space to restart or\nescape to exit\n\nYour score was " + std::to_string(score));
}
if (ball.getPosition().y - ballRadius < 0.f)
{
wallSound.play();
ballAngle = -ballAngle;
ball.setPosition(ball.getPosition().x, ballRadius + 0.1f);
}
if (ball.getPosition().y + ballRadius > gameHeight)
{
wallSound.play();
ballAngle = -ballAngle;
ball.setPosition(ball.getPosition().x, gameHeight - ballRadius - 0.1f);
}
// Check the collisions between the ball and the paddles
// Left Paddle
if (ball.getPosition().x - ballRadius < leftPaddle.getPosition().x + paddleSize.x / 2 &&
ball.getPosition().x - ballRadius > leftPaddle.getPosition().x &&
ball.getPosition().y + ballRadius >= leftPaddle.getPosition().y - paddleSize.y / 2 &&
ball.getPosition().y - ballRadius <= leftPaddle.getPosition().y + paddleSize.y / 2)
{
if (ball.getPosition().y > leftPaddle.getPosition().y)
{
ballAngle = pi - ballAngle + (std::rand() % 20) * pi / 180;
}
else
{
ballAngle = pi - ballAngle - (std::rand() % 20) * pi / 180;
}
std::cout << "left" << std::endl;
score = score + 1;
scoreText.setString(std::to_string(score));
paddleSound.play();
ball.setPosition(leftPaddle.getPosition().x + ballRadius + paddleSize.x / 2 + 0.1f, ball.getPosition().y);
}
// Right Paddle
if (ball.getPosition().x + ballRadius > rightPaddle.getPosition().x - paddleSize.x / 2 &&
ball.getPosition().x + ballRadius < rightPaddle.getPosition().x &&
ball.getPosition().y + ballRadius >= rightPaddle.getPosition().y - paddleSize.y / 2 &&
ball.getPosition().y - ballRadius <= rightPaddle.getPosition().y + paddleSize.y / 2)
{
if (ball.getPosition().y > rightPaddle.getPosition().y)
ballAngle = pi - ballAngle + (std::rand() % 20) * pi / 180;
else
ballAngle = pi - ballAngle - (std::rand() % 20) * pi / 180;
std::cout << "right" << std::endl;
paddleSound.play();
ball.setPosition(rightPaddle.getPosition().x - ballRadius - paddleSize.x / 2 - 0.1f, ball.getPosition().y);
}
}
// Clear the window
window.clear(sf::Color::Black);
if (isPlaying)
{
// Draw the paddles and the ball
window.draw(scoreText);
window.draw(leftPaddle);
window.draw(rightPaddle);
window.draw(ball);
}
else
{
// Draw the pause message
window.draw(pauseMessage);
}
// Display things on screen
window.display();
}
return EXIT_SUCCESS;
}