|
@@ -1,4 +1,3 @@
|
1
|
|
-
|
2
|
1
|
////////////////////////////////////////////////////////////
|
3
|
2
|
// Headers
|
4
|
3
|
////////////////////////////////////////////////////////////
|
|
@@ -7,12 +6,16 @@
|
7
|
6
|
#include <cmath>
|
8
|
7
|
#include <ctime>
|
9
|
8
|
#include <cstdlib>
|
|
9
|
+#include "Ball.h"
|
|
10
|
+#include "Paddle.h"
|
10
|
11
|
|
11
|
12
|
#ifdef SFML_SYSTEM_IOS
|
12
|
13
|
#include <SFML/Main.hpp>
|
13
|
14
|
#endif
|
14
|
15
|
|
15
|
|
-std::string resourcesDir()
|
|
16
|
+using namespace std;
|
|
17
|
+
|
|
18
|
+string resourcesDir()
|
16
|
19
|
{
|
17
|
20
|
#ifdef SFML_SYSTEM_IOS
|
18
|
21
|
return "";
|
|
@@ -21,6 +24,7 @@ std::string resourcesDir()
|
21
|
24
|
#endif
|
22
|
25
|
}
|
23
|
26
|
|
|
27
|
+
|
24
|
28
|
////////////////////////////////////////////////////////////
|
25
|
29
|
/// Entry point of application
|
26
|
30
|
///
|
|
@@ -29,14 +33,14 @@ std::string resourcesDir()
|
29
|
33
|
////////////////////////////////////////////////////////////
|
30
|
34
|
int main()
|
31
|
35
|
{
|
32
|
|
- std::srand(static_cast<unsigned int>(std::time(NULL)));
|
|
36
|
+ srand(static_cast<unsigned int>(time(NULL)));
|
33
|
37
|
|
34
|
38
|
// Define some constants
|
35
|
|
- const float pi = 3.14159f;
|
|
39
|
+ const float pi = 3.14159;
|
36
|
40
|
const int gameWidth = 800;
|
37
|
41
|
const int gameHeight = 600;
|
38
|
|
- sf::Vector2f paddleSize(25, 100);
|
39
|
|
- float ballRadius = 10.f;
|
|
42
|
+
|
|
43
|
+ float ballRadius = 10;
|
40
|
44
|
|
41
|
45
|
// Create the window of the application
|
42
|
46
|
sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong",
|
|
@@ -50,28 +54,13 @@ int main()
|
50
|
54
|
sf::Sound ballSound(ballSoundBuffer);
|
51
|
55
|
|
52
|
56
|
// Create the left paddle
|
53
|
|
- sf::RectangleShape leftPaddle;
|
54
|
|
- leftPaddle.setSize(paddleSize - sf::Vector2f(3, 3));
|
55
|
|
- leftPaddle.setOutlineThickness(3);
|
56
|
|
- leftPaddle.setOutlineColor(sf::Color::Black);
|
57
|
|
- leftPaddle.setFillColor(sf::Color(100, 100, 200));
|
58
|
|
- leftPaddle.setOrigin(paddleSize / 2.f);
|
|
57
|
+ Paddle leftPaddle;
|
59
|
58
|
|
60
|
59
|
// Create the right paddle
|
61
|
|
- sf::RectangleShape rightPaddle;
|
62
|
|
- rightPaddle.setSize(paddleSize - sf::Vector2f(3, 3));
|
63
|
|
- rightPaddle.setOutlineThickness(3);
|
64
|
|
- rightPaddle.setOutlineColor(sf::Color::Black);
|
65
|
|
- rightPaddle.setFillColor(sf::Color(200, 100, 100));
|
66
|
|
- rightPaddle.setOrigin(paddleSize / 2.f);
|
|
60
|
+ Paddle rightPaddle;
|
67
|
61
|
|
68
|
62
|
// Create the ball
|
69
|
|
- sf::CircleShape ball;
|
70
|
|
- ball.setRadius(ballRadius - 3);
|
71
|
|
- ball.setOutlineThickness(3);
|
72
|
|
- ball.setOutlineColor(sf::Color::Black);
|
73
|
|
- ball.setFillColor(sf::Color::White);
|
74
|
|
- ball.setOrigin(ballRadius / 2, ballRadius / 2);
|
|
63
|
+ Ball ball;
|
75
|
64
|
|
76
|
65
|
// Load the text font
|
77
|
66
|
sf::Font font;
|
|
@@ -82,7 +71,7 @@ int main()
|
82
|
71
|
sf::Text pauseMessage;
|
83
|
72
|
pauseMessage.setFont(font);
|
84
|
73
|
pauseMessage.setCharacterSize(40);
|
85
|
|
- pauseMessage.setPosition(170.f, 150.f);
|
|
74
|
+ pauseMessage.setPosition(170, 150);
|
86
|
75
|
//pauseMessage.setFillColor(sf::Color::White);
|
87
|
76
|
|
88
|
77
|
#ifdef SFML_SYSTEM_IOS
|
|
@@ -96,7 +85,6 @@ int main()
|
96
|
85
|
const sf::Time AITime = sf::seconds(0.1f);
|
97
|
86
|
const float paddleSpeed = 400.f;
|
98
|
87
|
float rightPaddleSpeed = 0.f;
|
99
|
|
- const float ballSpeed = 400.f;
|
100
|
88
|
float ballAngle = 0.f; // to be changed later
|
101
|
89
|
|
102
|
90
|
sf::Clock clock;
|
|
@@ -126,17 +114,17 @@ int main()
|
126
|
114
|
clock.restart();
|
127
|
115
|
|
128
|
116
|
// Reset the position of the paddles and ball
|
129
|
|
- leftPaddle.setPosition(10 + paddleSize.x / 2, gameHeight / 2);
|
130
|
|
- rightPaddle.setPosition(gameWidth - 10 - paddleSize.x / 2, gameHeight / 2);
|
|
117
|
+ leftPaddle.setPosition(10 + leftPaddle.getSize().x / 2, gameHeight / 2);
|
|
118
|
+ rightPaddle.setPosition(gameWidth - 10 - leftPaddle.getSize().x / 2, gameHeight / 2);
|
131
|
119
|
ball.setPosition(gameWidth / 2, gameHeight / 2);
|
132
|
120
|
|
133
|
121
|
// Reset the ball angle
|
134
|
122
|
do
|
135
|
123
|
{
|
136
|
124
|
// Make sure the ball initial angle is not too much vertical
|
137
|
|
- ballAngle = (std::rand() % 360) * 2 * pi / 360;
|
|
125
|
+ ballAngle = (rand() % 360) * 2 * pi / 360;
|
138
|
126
|
}
|
139
|
|
- while (std::abs(std::cos(ballAngle)) < 0.7f);
|
|
127
|
+ while (abs(cos(ballAngle)) < 0.7f);
|
140
|
128
|
}
|
141
|
129
|
}
|
142
|
130
|
|
|
@@ -150,18 +138,19 @@ int main()
|
150
|
138
|
}
|
151
|
139
|
}
|
152
|
140
|
|
|
141
|
+
|
153
|
142
|
if (isPlaying)
|
154
|
143
|
{
|
155
|
144
|
float deltaTime = clock.restart().asSeconds();
|
156
|
145
|
|
157
|
146
|
// Move the player's paddle
|
158
|
147
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) &&
|
159
|
|
- (leftPaddle.getPosition().y - paddleSize.y / 2 > 5.f))
|
|
148
|
+ (leftPaddle.getY() - leftPaddle.getSize().y / 2 > 5))
|
160
|
149
|
{
|
161
|
150
|
leftPaddle.move(0.f, -paddleSpeed * deltaTime);
|
162
|
151
|
}
|
163
|
152
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) &&
|
164
|
|
- (leftPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f))
|
|
153
|
+ (leftPaddle.getY() + leftPaddle.getSize().y / 2 < gameHeight - 5))
|
165
|
154
|
{
|
166
|
155
|
leftPaddle.move(0.f, paddleSpeed * deltaTime);
|
167
|
156
|
}
|
|
@@ -170,12 +159,11 @@ int main()
|
170
|
159
|
{
|
171
|
160
|
sf::Vector2i pos = sf::Touch::getPosition(0);
|
172
|
161
|
sf::Vector2f mappedPos = window.mapPixelToCoords(pos);
|
173
|
|
- leftPaddle.setPosition(leftPaddle.getPosition().x, mappedPos.y);
|
|
162
|
+ leftPaddle.setPosition(leftPaddle.getX(), mappedPos.y);
|
174
|
163
|
}
|
175
|
|
-
|
176
|
164
|
// Move the computer's paddle
|
177
|
|
- if (((rightPaddleSpeed < 0.f) && (rightPaddle.getPosition().y - paddleSize.y / 2 > 5.f)) ||
|
178
|
|
- ((rightPaddleSpeed > 0.f) && (rightPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f)))
|
|
165
|
+ if (((rightPaddleSpeed < 0.f) && (rightPaddle.getY() - leftPaddle.getSize().y / 2 > 5.f)) ||
|
|
166
|
+ ((rightPaddleSpeed > 0.f) && (rightPaddle.getY() + leftPaddle.getSize().y / 2 < gameHeight - 5.f)))
|
179
|
167
|
{
|
180
|
168
|
rightPaddle.move(0.f, rightPaddleSpeed * deltaTime);
|
181
|
169
|
}
|
|
@@ -184,77 +172,77 @@ int main()
|
184
|
172
|
if (AITimer.getElapsedTime() > AITime)
|
185
|
173
|
{
|
186
|
174
|
AITimer.restart();
|
187
|
|
- if (ball.getPosition().y + ballRadius > rightPaddle.getPosition().y + paddleSize.y / 2)
|
|
175
|
+ if (ball.getY() + ballRadius > rightPaddle.getY() + leftPaddle.getSize().y / 2)
|
188
|
176
|
rightPaddleSpeed = paddleSpeed;
|
189
|
|
- else if (ball.getPosition().y - ballRadius < rightPaddle.getPosition().y - paddleSize.y / 2)
|
|
177
|
+ else if (ball.getY() - ballRadius < rightPaddle.getY() - leftPaddle.getSize().y / 2)
|
190
|
178
|
rightPaddleSpeed = -paddleSpeed;
|
191
|
179
|
else
|
192
|
180
|
rightPaddleSpeed = 0.f;
|
193
|
181
|
}
|
194
|
182
|
|
195
|
183
|
// Move the ball
|
196
|
|
- float factor = ballSpeed * deltaTime;
|
197
|
|
- ball.move(std::cos(ballAngle) * factor, std::sin(ballAngle) * factor);
|
|
184
|
+ float factor = ball.getSpeed() * deltaTime;
|
|
185
|
+ ball.move(cos(ballAngle) * factor, sin(ballAngle) * factor);
|
198
|
186
|
|
199
|
187
|
#ifdef SFML_SYSTEM_IOS
|
200
|
|
- const std::string inputString = "Touch the screen to restart";
|
|
188
|
+ const string inputString = "Touch the screen to restart";
|
201
|
189
|
#else
|
202
|
|
- const std::string inputString = "Press space to restart or\nescape to exit";
|
|
190
|
+ const string inputString = "Press space to restart or\nescape to exit";
|
203
|
191
|
#endif
|
204
|
192
|
|
205
|
193
|
// Check collisions between the ball and the screen
|
206
|
|
- if (ball.getPosition().x - ballRadius < 0.f)
|
|
194
|
+ if (ball.getX() - ballRadius < 0.f)
|
207
|
195
|
{
|
208
|
196
|
isPlaying = false;
|
209
|
197
|
pauseMessage.setString("You Lost!\n" + inputString);
|
210
|
198
|
}
|
211
|
|
- if (ball.getPosition().x + ballRadius > gameWidth)
|
|
199
|
+ if (ball.getX() + ballRadius > gameWidth)
|
212
|
200
|
{
|
213
|
201
|
isPlaying = false;
|
214
|
202
|
pauseMessage.setString("You Won!\n" + inputString);
|
215
|
203
|
}
|
216
|
|
- if (ball.getPosition().y - ballRadius < 0.f)
|
|
204
|
+ if (ball.getY() - ballRadius < 0.f)
|
217
|
205
|
{
|
218
|
206
|
ballSound.play();
|
219
|
207
|
ballAngle = -ballAngle;
|
220
|
|
- ball.setPosition(ball.getPosition().x, ballRadius + 0.1f);
|
|
208
|
+ ball.setPosition(ball.getX(), ballRadius + 0.1f);
|
221
|
209
|
}
|
222
|
|
- if (ball.getPosition().y + ballRadius > gameHeight)
|
|
210
|
+ if (ball.getY() + ballRadius > gameHeight)
|
223
|
211
|
{
|
224
|
212
|
ballSound.play();
|
225
|
213
|
ballAngle = -ballAngle;
|
226
|
|
- ball.setPosition(ball.getPosition().x, gameHeight - ballRadius - 0.1f);
|
|
214
|
+ ball.setPosition(ball.getX(), gameHeight - ballRadius - 0.1f);
|
227
|
215
|
}
|
228
|
216
|
|
229
|
217
|
// Check the collisions between the ball and the paddles
|
230
|
218
|
// Left Paddle
|
231
|
|
- if (ball.getPosition().x - ballRadius < leftPaddle.getPosition().x + paddleSize.x / 2 &&
|
232
|
|
- ball.getPosition().x - ballRadius > leftPaddle.getPosition().x &&
|
233
|
|
- ball.getPosition().y + ballRadius >= leftPaddle.getPosition().y - paddleSize.y / 2 &&
|
234
|
|
- ball.getPosition().y - ballRadius <= leftPaddle.getPosition().y + paddleSize.y / 2)
|
|
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)
|
235
|
223
|
{
|
236
|
|
- if (ball.getPosition().y > leftPaddle.getPosition().y)
|
237
|
|
- ballAngle = pi - ballAngle + (std::rand() % 20) * pi / 180;
|
|
224
|
+ if (ball.getY() > leftPaddle.getY())
|
|
225
|
+ ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
|
238
|
226
|
else
|
239
|
|
- ballAngle = pi - ballAngle - (std::rand() % 20) * pi / 180;
|
|
227
|
+ ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
|
240
|
228
|
|
241
|
229
|
ballSound.play();
|
242
|
|
- ball.setPosition(leftPaddle.getPosition().x + ballRadius + paddleSize.x / 2 + 0.1f, ball.getPosition().y);
|
|
230
|
+ ball.setPosition(leftPaddle.getX() + ballRadius + leftPaddle.getSize().x / 2 + 0.1f, ball.getY());
|
243
|
231
|
}
|
244
|
232
|
|
245
|
233
|
// Right Paddle
|
246
|
|
- if (ball.getPosition().x + ballRadius > rightPaddle.getPosition().x - paddleSize.x / 2 &&
|
247
|
|
- ball.getPosition().x + ballRadius < rightPaddle.getPosition().x &&
|
248
|
|
- ball.getPosition().y + ballRadius >= rightPaddle.getPosition().y - paddleSize.y / 2 &&
|
249
|
|
- ball.getPosition().y - ballRadius <= rightPaddle.getPosition().y + paddleSize.y / 2)
|
|
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)
|
250
|
238
|
{
|
251
|
|
- if (ball.getPosition().y > rightPaddle.getPosition().y)
|
252
|
|
- ballAngle = pi - ballAngle + (std::rand() % 20) * pi / 180;
|
|
239
|
+ if (ball.getY() > rightPaddle.getY())
|
|
240
|
+ ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
|
253
|
241
|
else
|
254
|
|
- ballAngle = pi - ballAngle - (std::rand() % 20) * pi / 180;
|
|
242
|
+ ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
|
255
|
243
|
|
256
|
244
|
ballSound.play();
|
257
|
|
- ball.setPosition(rightPaddle.getPosition().x - ballRadius - paddleSize.x / 2 - 0.1f, ball.getPosition().y);
|
|
245
|
+ ball.setPosition(rightPaddle.getX() - ballRadius - leftPaddle.getSize().x / 2 - 0.1f, ball.getY());
|
258
|
246
|
}
|
259
|
247
|
}
|
260
|
248
|
|