Browse Source

Collisions Moved to Ball Object

Daniel 5 years ago
parent
commit
c0879593e8
6 changed files with 25 additions and 14 deletions
  1. 5
    0
      Classes.h
  2. 15
    0
      functions.cpp
  3. BIN
      functions.o
  4. BIN
      pong
  5. 5
    14
      pong.cpp
  6. BIN
      pong.o

+ 5
- 0
Classes.h View File

@@ -1,6 +1,10 @@
1 1
 #include <SFML/Graphics.hpp>
2 2
 #include <SFML/Audio.hpp>
3 3
 
4
+const int gameWidth = 800;
5
+const int gameHeight = 600;
6
+
7
+
4 8
 class Paddle : public sf::RectangleShape {
5 9
     private:
6 10
     float x,
@@ -39,5 +43,6 @@ class Ball : public sf::CircleShape {
39 43
         void render(sf::RenderWindow) const;
40 44
         void checkLeftPaddle(Paddle leftPaddle, sf::Sound ballSound);
41 45
         void checkRightPaddle(Paddle rightPaddle, sf::Sound ballSound);
46
+        void checkCollisions(sf::Sound ballsound);
42 47
 
43 48
 };

+ 15
- 0
functions.cpp View File

@@ -88,6 +88,21 @@ void Ball::checkRightPaddle(Paddle rightPaddle, sf::Sound ballSound) {
88 88
     }
89 89
 }
90 90
 
91
+void Ball::checkCollisions(sf::Sound ballSound) {
92
+    if (getY() - radius < 0.f)
93
+            {
94
+                ballSound.play();
95
+                angle = -angle;
96
+                setPosition(getX(), radius + 0.1);
97
+            }
98
+            if (getY() + radius > gameHeight)
99
+            {
100
+                ballSound.play();
101
+                angle = -angle;
102
+                setPosition(getX(), gameHeight - radius - 0.1);
103
+            }
104
+}
105
+
91 106
 Paddle::Paddle() {
92 107
     size.x = 25;
93 108
     size.y = 100;

BIN
functions.o View File


BIN
pong View File


+ 5
- 14
pong.cpp View File

@@ -30,14 +30,14 @@ string resourcesDir()
30 30
 /// \return Application exit code
31 31
 ///
32 32
 ////////////////////////////////////////////////////////////
33
+
33 34
 int main()
34 35
 {
35 36
     srand(static_cast<unsigned int>(time(NULL)));
36 37
 
37 38
     // Define some constants
38 39
     const float pi = 3.1415;
39
-    const int gameWidth = 800;
40
-    const int gameHeight = 600;
40
+
41 41
 
42 42
     // Create the window of the application
43 43
     sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong",
@@ -197,18 +197,9 @@ int main()
197 197
                 isPlaying = false;
198 198
                 pauseMessage.setString("You Won!\n" + inputString);
199 199
             }
200
-            if (ball.getY() - ball.getRadius() < 0.f)
201
-            {
202
-                ballSound.play();
203
-                ball.setAngle(-ball.getAngle());
204
-                ball.setPosition(ball.getX(), ball.getRadius() + 0.1f);
205
-            }
206
-            if (ball.getY() + ball.getRadius() > gameHeight)
207
-            {
208
-                ballSound.play();
209
-                ball.setAngle(-ball.getAngle());
210
-                ball.setPosition(ball.getX(), gameHeight - ball.getRadius() - 0.1f);
211
-            }
200
+
201
+            
202
+            ball.checkCollisions(ballSound);
212 203
 
213 204
             // Check the collisions between the ball and the paddles
214 205
             // Left Paddle

BIN
pong.o View File