|
@@ -39,8 +39,6 @@ int main()
|
39
|
39
|
const float pi = 3.1415;
|
40
|
40
|
const int gameWidth = 800;
|
41
|
41
|
const int gameHeight = 600;
|
42
|
|
-
|
43
|
|
- float ballRadius = 10;
|
44
|
42
|
|
45
|
43
|
// Create the window of the application
|
46
|
44
|
sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong",
|
|
@@ -172,9 +170,9 @@ int main()
|
172
|
170
|
if (AITimer.getElapsedTime() > AITime)
|
173
|
171
|
{
|
174
|
172
|
AITimer.restart();
|
175
|
|
- if (ball.getY() + ballRadius > rightPaddle.getY() + leftPaddle.getSize().y / 2)
|
|
173
|
+ if (ball.getY() + ball.getRadius() > rightPaddle.getY() + leftPaddle.getSize().y / 2)
|
176
|
174
|
rightPaddleSpeed = paddleSpeed;
|
177
|
|
- else if (ball.getY() - ballRadius < rightPaddle.getY() - leftPaddle.getSize().y / 2)
|
|
175
|
+ else if (ball.getY() - ball.getRadius() < rightPaddle.getY() - leftPaddle.getSize().y / 2)
|
178
|
176
|
rightPaddleSpeed = -paddleSpeed;
|
179
|
177
|
else
|
180
|
178
|
rightPaddleSpeed = 0.f;
|
|
@@ -191,35 +189,35 @@ int main()
|
191
|
189
|
#endif
|
192
|
190
|
|
193
|
191
|
// Check collisions between the ball and the screen
|
194
|
|
- if (ball.getX() - ballRadius < 0.f)
|
|
192
|
+ if (ball.getX() - ball.getRadius() < 0.f)
|
195
|
193
|
{
|
196
|
194
|
isPlaying = false;
|
197
|
195
|
pauseMessage.setString("You Lost!\n" + inputString);
|
198
|
196
|
}
|
199
|
|
- if (ball.getX() + ballRadius > gameWidth)
|
|
197
|
+ if (ball.getX() + ball.getRadius() > gameWidth)
|
200
|
198
|
{
|
201
|
199
|
isPlaying = false;
|
202
|
200
|
pauseMessage.setString("You Won!\n" + inputString);
|
203
|
201
|
}
|
204
|
|
- if (ball.getY() - ballRadius < 0.f)
|
|
202
|
+ if (ball.getY() - ball.getRadius() < 0.f)
|
205
|
203
|
{
|
206
|
204
|
ballSound.play();
|
207
|
205
|
ballAngle = -ballAngle;
|
208
|
|
- ball.setPosition(ball.getX(), ballRadius + 0.1f);
|
|
206
|
+ ball.setPosition(ball.getX(), ball.getRadius() + 0.1f);
|
209
|
207
|
}
|
210
|
|
- if (ball.getY() + ballRadius > gameHeight)
|
|
208
|
+ if (ball.getY() + ball.getRadius() > gameHeight)
|
211
|
209
|
{
|
212
|
210
|
ballSound.play();
|
213
|
211
|
ballAngle = -ballAngle;
|
214
|
|
- ball.setPosition(ball.getX(), gameHeight - ballRadius - 0.1f);
|
|
212
|
+ ball.setPosition(ball.getX(), gameHeight - ball.getRadius() - 0.1f);
|
215
|
213
|
}
|
216
|
214
|
|
217
|
215
|
// Check the collisions between the ball and the paddles
|
218
|
216
|
// Left Paddle
|
219
|
|
- if (ball.getX() - ballRadius < leftPaddle.getX() + leftPaddle.getSize().x / 2 &&
|
220
|
|
- ball.getX() - ballRadius > leftPaddle.getX() &&
|
221
|
|
- ball.getY() + ballRadius >= leftPaddle.getY() - leftPaddle.getSize().y / 2 &&
|
222
|
|
- ball.getY() - ballRadius <= leftPaddle.getY() + leftPaddle.getSize().y / 2)
|
|
217
|
+ if (ball.getX() - ball.getRadius() < leftPaddle.getX() + leftPaddle.getSize().x / 2 &&
|
|
218
|
+ ball.getX() - ball.getRadius() > leftPaddle.getX() &&
|
|
219
|
+ ball.getY() + ball.getRadius() >= leftPaddle.getY() - leftPaddle.getSize().y / 2 &&
|
|
220
|
+ ball.getY() - ball.getRadius() <= leftPaddle.getY() + leftPaddle.getSize().y / 2)
|
223
|
221
|
{
|
224
|
222
|
if (ball.getY() > leftPaddle.getY())
|
225
|
223
|
ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
|
|
@@ -227,14 +225,14 @@ int main()
|
227
|
225
|
ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
|
228
|
226
|
|
229
|
227
|
ballSound.play();
|
230
|
|
- ball.setPosition(leftPaddle.getX() + ballRadius + leftPaddle.getSize().x / 2 + 0.1f, ball.getY());
|
|
228
|
+ ball.setPosition(leftPaddle.getX() + ball.getRadius() + leftPaddle.getSize().x / 2 + 0.1f, ball.getY());
|
231
|
229
|
}
|
232
|
230
|
|
233
|
231
|
// Right Paddle
|
234
|
|
- if (ball.getX() + ballRadius > rightPaddle.getX() - leftPaddle.getSize().x / 2 &&
|
235
|
|
- ball.getX() + ballRadius < rightPaddle.getX() &&
|
236
|
|
- ball.getY() + ballRadius >= rightPaddle.getY() - leftPaddle.getSize().y / 2 &&
|
237
|
|
- ball.getY() - ballRadius <= rightPaddle.getY() + leftPaddle.getSize().y / 2)
|
|
232
|
+ if (ball.getX() + ball.getRadius() > rightPaddle.getX() - leftPaddle.getSize().x / 2 &&
|
|
233
|
+ ball.getX() + ball.getRadius() < rightPaddle.getX() &&
|
|
234
|
+ ball.getY() + ball.getRadius() >= rightPaddle.getY() - leftPaddle.getSize().y / 2 &&
|
|
235
|
+ ball.getY() - ball.getRadius() <= rightPaddle.getY() + leftPaddle.getSize().y / 2)
|
238
|
236
|
{
|
239
|
237
|
if (ball.getY() > rightPaddle.getY())
|
240
|
238
|
ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
|
|
@@ -242,7 +240,7 @@ int main()
|
242
|
240
|
ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
|
243
|
241
|
|
244
|
242
|
ballSound.play();
|
245
|
|
- ball.setPosition(rightPaddle.getX() - ballRadius - leftPaddle.getSize().x / 2 - 0.1f, ball.getY());
|
|
243
|
+ ball.setPosition(rightPaddle.getX() - ball.getRadius() - leftPaddle.getSize().x / 2 - 0.1f, ball.getY());
|
246
|
244
|
}
|
247
|
245
|
}
|
248
|
246
|
|