|
@@ -6,8 +6,7 @@
|
6
|
6
|
#include <cmath>
|
7
|
7
|
#include <ctime>
|
8
|
8
|
#include <cstdlib>
|
9
|
|
-#include "Ball.h"
|
10
|
|
-#include "Paddle.h"
|
|
9
|
+#include "Classes.h"
|
11
|
10
|
|
12
|
11
|
#ifdef SFML_SYSTEM_IOS
|
13
|
12
|
#include <SFML/Main.hpp>
|
|
@@ -82,8 +81,7 @@ int main()
|
82
|
81
|
sf::Clock AITimer;
|
83
|
82
|
const sf::Time AITime = sf::seconds(0.1f);
|
84
|
83
|
const float paddleSpeed = 400.f;
|
85
|
|
- float rightPaddleSpeed = 0.f;
|
86
|
|
- float ballAngle = 0.f; // to be changed later
|
|
84
|
+ float rightPaddleSpeed = 0.f; // to be changed later
|
87
|
85
|
|
88
|
86
|
sf::Clock clock;
|
89
|
87
|
bool isPlaying = false;
|
|
@@ -120,9 +118,9 @@ int main()
|
120
|
118
|
do
|
121
|
119
|
{
|
122
|
120
|
// Make sure the ball initial angle is not too much vertical
|
123
|
|
- ballAngle = (rand() % 360) * 2 * pi / 360;
|
|
121
|
+ ball.setAngle((rand() % 360) * 2 * pi / 360);
|
124
|
122
|
}
|
125
|
|
- while (abs(cos(ballAngle)) < 0.7f);
|
|
123
|
+ while (abs(cos(ball.getAngle())) < 0.7f);
|
126
|
124
|
}
|
127
|
125
|
}
|
128
|
126
|
|
|
@@ -180,7 +178,7 @@ int main()
|
180
|
178
|
|
181
|
179
|
// Move the ball
|
182
|
180
|
float factor = ball.getSpeed() * deltaTime;
|
183
|
|
- ball.move(cos(ballAngle) * factor, sin(ballAngle) * factor);
|
|
181
|
+ ball.move(cos(ball.getAngle()) * factor, sin(ball.getAngle()) * factor);
|
184
|
182
|
|
185
|
183
|
#ifdef SFML_SYSTEM_IOS
|
186
|
184
|
const string inputString = "Touch the screen to restart";
|
|
@@ -189,7 +187,7 @@ int main()
|
189
|
187
|
#endif
|
190
|
188
|
|
191
|
189
|
// Check collisions between the ball and the screen
|
192
|
|
- if (ball.getX() - ball.getRadius() < 0.f)
|
|
190
|
+ if (ball.getX() - ball.getRadius() < 0)
|
193
|
191
|
{
|
194
|
192
|
isPlaying = false;
|
195
|
193
|
pauseMessage.setString("You Lost!\n" + inputString);
|
|
@@ -202,46 +200,23 @@ int main()
|
202
|
200
|
if (ball.getY() - ball.getRadius() < 0.f)
|
203
|
201
|
{
|
204
|
202
|
ballSound.play();
|
205
|
|
- ballAngle = -ballAngle;
|
|
203
|
+ ball.setAngle(-ball.getAngle());
|
206
|
204
|
ball.setPosition(ball.getX(), ball.getRadius() + 0.1f);
|
207
|
205
|
}
|
208
|
206
|
if (ball.getY() + ball.getRadius() > gameHeight)
|
209
|
207
|
{
|
210
|
208
|
ballSound.play();
|
211
|
|
- ballAngle = -ballAngle;
|
|
209
|
+ ball.setAngle(-ball.getAngle());
|
212
|
210
|
ball.setPosition(ball.getX(), gameHeight - ball.getRadius() - 0.1f);
|
213
|
211
|
}
|
214
|
212
|
|
215
|
213
|
// Check the collisions between the ball and the paddles
|
216
|
214
|
// Left Paddle
|
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)
|
221
|
|
- {
|
222
|
|
- if (ball.getY() > leftPaddle.getY())
|
223
|
|
- ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
|
224
|
|
- else
|
225
|
|
- ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
|
226
|
|
-
|
227
|
|
- ballSound.play();
|
228
|
|
- ball.setPosition(leftPaddle.getX() + ball.getRadius() + leftPaddle.getSize().x / 2 + 0.1f, ball.getY());
|
229
|
|
- }
|
|
215
|
+ ball.checkLeftPaddle(leftPaddle, ballSound);
|
230
|
216
|
|
231
|
217
|
// Right Paddle
|
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)
|
236
|
|
- {
|
237
|
|
- if (ball.getY() > rightPaddle.getY())
|
238
|
|
- ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
|
239
|
|
- else
|
240
|
|
- ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
|
241
|
|
-
|
242
|
|
- ballSound.play();
|
243
|
|
- ball.setPosition(rightPaddle.getX() - ball.getRadius() - leftPaddle.getSize().x / 2 - 0.1f, ball.getY());
|
244
|
|
- }
|
|
218
|
+ ball.checkRightPaddle(rightPaddle, ballSound);
|
|
219
|
+
|
245
|
220
|
}
|
246
|
221
|
|
247
|
222
|
// Clear the window
|