Quellcode durchsuchen

Collisions Moved to Ball Object

Daniel vor 5 Jahren
Ursprung
Commit
c0879593e8
6 geänderte Dateien mit 25 neuen und 14 gelöschten Zeilen
  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 Datei anzeigen

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

+ 15
- 0
functions.cpp Datei anzeigen

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
 Paddle::Paddle() {
106
 Paddle::Paddle() {
92
     size.x = 25;
107
     size.x = 25;
93
     size.y = 100;
108
     size.y = 100;

BIN
functions.o Datei anzeigen


BIN
pong Datei anzeigen


+ 5
- 14
pong.cpp Datei anzeigen

30
 /// \return Application exit code
30
 /// \return Application exit code
31
 ///
31
 ///
32
 ////////////////////////////////////////////////////////////
32
 ////////////////////////////////////////////////////////////
33
+
33
 int main()
34
 int main()
34
 {
35
 {
35
     srand(static_cast<unsigned int>(time(NULL)));
36
     srand(static_cast<unsigned int>(time(NULL)));
36
 
37
 
37
     // Define some constants
38
     // Define some constants
38
     const float pi = 3.1415;
39
     const float pi = 3.1415;
39
-    const int gameWidth = 800;
40
-    const int gameHeight = 600;
40
+
41
 
41
 
42
     // Create the window of the application
42
     // Create the window of the application
43
     sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong",
43
     sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong",
197
                 isPlaying = false;
197
                 isPlaying = false;
198
                 pauseMessage.setString("You Won!\n" + inputString);
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
             // Check the collisions between the ball and the paddles
204
             // Check the collisions between the ball and the paddles
214
             // Left Paddle
205
             // Left Paddle

BIN
pong.o Datei anzeigen