Daniel 5 years ago
parent
commit
d6589196da
7 changed files with 447 additions and 66 deletions
  1. 21
    0
      Ball.h
  2. 1
    2
      Makefile
  3. 20
    0
      Paddle.h
  4. 71
    0
      functions.cpp
  5. 282
    0
      oldpong.cpp
  6. BIN
      pong
  7. 52
    64
      pong.cpp

+ 21
- 0
Ball.h View File

@@ -0,0 +1,21 @@
1
+#include <SFML/Graphics.hpp>
2
+#include <SFML/Audio.hpp>
3
+
4
+class Ball : public sf::CircleShape {
5
+    private:
6
+        float x,
7
+              y,
8
+              radius = 10;
9
+        const float ballSpeed = 400;
10
+    
11
+    public:
12
+        Ball();
13
+        Ball(int radius);
14
+        void move(float x, float y);
15
+        void setPosition(float x, float y);
16
+        float getX() const;
17
+        float getY() const;
18
+        float getSpeed() const;
19
+        void render(sf::RenderWindow) const;
20
+
21
+};

+ 1
- 2
Makefile View File

@@ -1,3 +1,2 @@
1 1
 all: pong.cpp resources
2
-	g++ pong.cpp -o pong  -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
3
-
2
+	g++ pong.cpp functions.cpp -o pong  -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio

+ 20
- 0
Paddle.h View File

@@ -0,0 +1,20 @@
1
+#include <SFML/Graphics.hpp>
2
+#include <SFML/Audio.hpp>
3
+
4
+class Paddle : public sf::RectangleShape {
5
+    private:
6
+    float x,
7
+          y;
8
+    sf::Vector2f size;
9
+
10
+    public:
11
+        Paddle();
12
+        void move(float x, float y);
13
+        void draw(sf::RenderWindow);
14
+        float getX() const;
15
+        float getY() const;
16
+        sf::Vector2f getSize() const;
17
+        void setPosition(float x, float y);
18
+        void render(sf::RenderWindow) const;
19
+
20
+};

+ 71
- 0
functions.cpp View File

@@ -0,0 +1,71 @@
1
+#include "Ball.h"
2
+#include "Paddle.h"
3
+
4
+
5
+Ball::Ball() {
6
+    setRadius(radius - 3);
7
+    setOutlineThickness(3);
8
+    setOutlineColor(sf::Color::Black);
9
+    setFillColor(sf::Color::White);
10
+    setOrigin(radius / 2, radius / 2);
11
+}
12
+
13
+Ball::Ball(int r) {
14
+    setRadius(r - 3);
15
+    setOutlineThickness(3);
16
+    setOutlineColor(sf::Color::Black);
17
+    setFillColor(sf::Color::White);
18
+    setOrigin(r / 2, r / 2);
19
+    radius = r;
20
+}
21
+
22
+float Ball::getX() const {
23
+    return getPosition().x;
24
+}
25
+
26
+float Ball::getY() const {
27
+    return getPosition().y;
28
+}
29
+
30
+void Ball::setPosition(float x, float y) {
31
+    sf::Transformable::setPosition(x, y);
32
+}
33
+
34
+float Ball::getSpeed() const {
35
+    return ballSpeed;
36
+}
37
+
38
+void Ball::move(float x, float y) {
39
+    sf::Transformable::move(x, y);
40
+}
41
+
42
+
43
+Paddle::Paddle() {
44
+    size.x = 25;
45
+    size.y = 100;
46
+    setSize(sf::Vector2f(25, 100) - sf::Vector2f(3, 3));
47
+    setOutlineThickness(3);
48
+    setOutlineColor(sf::Color::Black);
49
+    setFillColor(sf::Color(200, 200, 200));
50
+    setOrigin(sf::Vector2f(25, 100) / 2.f);
51
+}
52
+
53
+float Paddle::getY() const {
54
+    return getPosition().y;
55
+}
56
+
57
+float Paddle::getX() const {
58
+    return getPosition().x;
59
+}
60
+
61
+void Paddle::setPosition(float x, float y) {
62
+    sf::Transformable::setPosition(x, y);
63
+}
64
+
65
+sf::Vector2f Paddle::getSize() const {
66
+    return size;
67
+}
68
+
69
+void Paddle::move(float x, float y) {
70
+    sf::Transformable::move(x, y);
71
+}

+ 282
- 0
oldpong.cpp View File

@@ -0,0 +1,282 @@
1
+
2
+////////////////////////////////////////////////////////////
3
+// Headers
4
+////////////////////////////////////////////////////////////
5
+#include <SFML/Graphics.hpp>
6
+#include <SFML/Audio.hpp>
7
+#include <cmath>
8
+#include <ctime>
9
+#include <cstdlib>
10
+
11
+#ifdef SFML_SYSTEM_IOS
12
+#include <SFML/Main.hpp>
13
+#endif
14
+
15
+std::string resourcesDir()
16
+{
17
+#ifdef SFML_SYSTEM_IOS
18
+    return "";
19
+#else
20
+    return "resources/";
21
+#endif
22
+}
23
+
24
+////////////////////////////////////////////////////////////
25
+/// Entry point of application
26
+///
27
+/// \return Application exit code
28
+///
29
+////////////////////////////////////////////////////////////
30
+int main()
31
+{
32
+    std::srand(static_cast<unsigned int>(std::time(NULL)));
33
+
34
+    // Define some constants
35
+    const float pi = 3.14159f;
36
+    const int gameWidth = 800;
37
+    const int gameHeight = 600;
38
+    sf::Vector2f paddleSize(25, 100);
39
+    float ballRadius = 10.f;
40
+
41
+    // Create the window of the application
42
+    sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong",
43
+                            sf::Style::Titlebar | sf::Style::Close);
44
+    window.setVerticalSyncEnabled(true);
45
+
46
+    // Load the sounds used in the game
47
+    sf::SoundBuffer ballSoundBuffer;
48
+    if (!ballSoundBuffer.loadFromFile(resourcesDir() + "ball.wav"))
49
+        return EXIT_FAILURE;
50
+    sf::Sound ballSound(ballSoundBuffer);
51
+
52
+    // 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, 100));
58
+    leftPaddle.setOrigin(paddleSize / 2.f);
59
+
60
+    // 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(100, 100, 100));
66
+    rightPaddle.setOrigin(paddleSize / 2.f);
67
+
68
+    // 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);
75
+
76
+    // Load the text font
77
+    sf::Font font;
78
+    if (!font.loadFromFile(resourcesDir() + "sansation.ttf"))
79
+        return EXIT_FAILURE;
80
+
81
+    // Initialize the pause message
82
+    sf::Text pauseMessage;
83
+    pauseMessage.setFont(font);
84
+    pauseMessage.setCharacterSize(40);
85
+    pauseMessage.setPosition(170.f, 150.f);
86
+    //pauseMessage.setFillColor(sf::Color::White);
87
+    
88
+    #ifdef SFML_SYSTEM_IOS
89
+    pauseMessage.setString("Welcome to SFML pong!\nTouch the screen to start the game");
90
+    #else
91
+    pauseMessage.setString("Welcome to SFML pong!\nPress space to start the game");
92
+    #endif
93
+
94
+    // Define the paddles properties
95
+    sf::Clock AITimer;
96
+    const sf::Time AITime   = sf::seconds(0.1f);
97
+    const float paddleSpeed = 400.f;
98
+    float rightPaddleSpeed  = 0.f;
99
+    const float ballSpeed   = 400.f;
100
+    float ballAngle         = 0.f; // to be changed later
101
+
102
+    sf::Clock clock;
103
+    bool isPlaying = false;
104
+    while (window.isOpen())
105
+    {
106
+        // Handle events
107
+        sf::Event event;
108
+        while (window.pollEvent(event))
109
+        {
110
+            // Window closed or escape key pressed: exit
111
+            if ((event.type == sf::Event::Closed) ||
112
+               ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
113
+            {
114
+                window.close();
115
+                break;
116
+            }
117
+
118
+            // Space key pressed: play
119
+            if (((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space)) ||
120
+                (event.type == sf::Event::TouchBegan))
121
+            {
122
+                if (!isPlaying)
123
+                {
124
+                    // (re)start the game
125
+                    isPlaying = true;
126
+                    clock.restart();
127
+
128
+                    // 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);
131
+                    ball.setPosition(gameWidth / 2, gameHeight / 2);
132
+
133
+                    // Reset the ball angle
134
+                    do
135
+                    {
136
+                        // Make sure the ball initial angle is not too much vertical
137
+                        ballAngle = (std::rand() % 360) * 2 * pi / 360;
138
+                    }
139
+                    while (std::abs(std::cos(ballAngle)) < 0.7f);
140
+                }
141
+            }
142
+            
143
+            // Window size changed, adjust view appropriately
144
+            if (event.type == sf::Event::Resized)
145
+            {
146
+                sf::View view;
147
+                view.setSize(gameWidth, gameHeight);
148
+                view.setCenter(gameWidth/2.f, gameHeight/2.f);
149
+                window.setView(view);
150
+            }
151
+        }
152
+
153
+        if (isPlaying)
154
+        {
155
+            float deltaTime = clock.restart().asSeconds();
156
+
157
+            // Move the player's paddle
158
+            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) &&
159
+               (leftPaddle.getPosition().y - paddleSize.y / 2 > 5.f))
160
+            {
161
+                leftPaddle.move(0.f, -paddleSpeed * deltaTime);
162
+            }
163
+            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) &&
164
+               (leftPaddle.getPosition().y + paddleSize.y / 2 < gameHeight - 5.f))
165
+            {
166
+                leftPaddle.move(0.f, paddleSpeed * deltaTime);
167
+            }
168
+            
169
+            if (sf::Touch::isDown(0))
170
+            {
171
+                sf::Vector2i pos = sf::Touch::getPosition(0);
172
+                sf::Vector2f mappedPos = window.mapPixelToCoords(pos);
173
+                leftPaddle.setPosition(leftPaddle.getPosition().x, mappedPos.y);
174
+            }
175
+
176
+            // 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)))
179
+            {
180
+                rightPaddle.move(0.f, rightPaddleSpeed * deltaTime);
181
+            }
182
+
183
+            // Update the computer's paddle direction according to the ball position
184
+            if (AITimer.getElapsedTime() > AITime)
185
+            {
186
+                AITimer.restart();
187
+                if (ball.getPosition().y + ballRadius > rightPaddle.getPosition().y + paddleSize.y / 2)
188
+                    rightPaddleSpeed = paddleSpeed;
189
+                else if (ball.getPosition().y - ballRadius < rightPaddle.getPosition().y - paddleSize.y / 2)
190
+                    rightPaddleSpeed = -paddleSpeed;
191
+                else
192
+                    rightPaddleSpeed = 0.f;
193
+            }
194
+
195
+            // Move the ball
196
+            float factor = ballSpeed * deltaTime;
197
+            ball.move(std::cos(ballAngle) * factor, std::sin(ballAngle) * factor);
198
+
199
+            #ifdef SFML_SYSTEM_IOS
200
+            const std::string inputString = "Touch the screen to restart";
201
+            #else
202
+            const std::string inputString = "Press space to restart or\nescape to exit";
203
+            #endif
204
+            
205
+            // Check collisions between the ball and the screen
206
+            if (ball.getPosition().x - ballRadius < 0.f)
207
+            {
208
+                isPlaying = false;
209
+                pauseMessage.setString("You Lost!\n" + inputString);
210
+            }
211
+            if (ball.getPosition().x + ballRadius > gameWidth)
212
+            {
213
+                isPlaying = false;
214
+                pauseMessage.setString("You Won!\n" + inputString);
215
+            }
216
+            if (ball.getPosition().y - ballRadius < 0.f)
217
+            {
218
+                ballSound.play();
219
+                ballAngle = -ballAngle;
220
+                ball.setPosition(ball.getPosition().x, ballRadius + 0.1f);
221
+            }
222
+            if (ball.getPosition().y + ballRadius > gameHeight)
223
+            {
224
+                ballSound.play();
225
+                ballAngle = -ballAngle;
226
+                ball.setPosition(ball.getPosition().x, gameHeight - ballRadius - 0.1f);
227
+            }
228
+
229
+            // Check the collisions between the ball and the paddles
230
+            // 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)
235
+            {
236
+                if (ball.getPosition().y > leftPaddle.getPosition().y)
237
+                    ballAngle = pi - ballAngle + (std::rand() % 20) * pi / 180;
238
+                else
239
+                    ballAngle = pi - ballAngle - (std::rand() % 20) * pi / 180;
240
+
241
+                ballSound.play();
242
+                ball.setPosition(leftPaddle.getPosition().x + ballRadius + paddleSize.x / 2 + 0.1f, ball.getPosition().y);
243
+            }
244
+
245
+            // 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)
250
+            {
251
+                if (ball.getPosition().y > rightPaddle.getPosition().y)
252
+                    ballAngle = pi - ballAngle + (std::rand() % 20) * pi / 180;
253
+                else
254
+                    ballAngle = pi - ballAngle - (std::rand() % 20) * pi / 180;
255
+
256
+                ballSound.play();
257
+                ball.setPosition(rightPaddle.getPosition().x - ballRadius - paddleSize.x / 2 - 0.1f, ball.getPosition().y);
258
+            }
259
+        }
260
+
261
+        // Clear the window
262
+        window.clear(sf::Color(50, 200, 50));
263
+
264
+        if (isPlaying)
265
+        {
266
+            // Draw the paddles and the ball
267
+            window.draw(leftPaddle);
268
+            window.draw(rightPaddle);
269
+            window.draw(ball);
270
+        }
271
+        else
272
+        {
273
+            // Draw the pause message
274
+            window.draw(pauseMessage);
275
+        }
276
+
277
+        // Display things on screen
278
+        window.display();
279
+    }
280
+
281
+    return EXIT_SUCCESS;
282
+}

BIN
pong View File


+ 52
- 64
pong.cpp View File

@@ -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