Browse Source

Moved BallSound to the Ball class

Daniel 5 years ago
parent
commit
be61ca233f
6 changed files with 17 additions and 23 deletions
  1. 6
    3
      Classes.h
  2. 6
    3
      functions.cpp
  3. BIN
      functions.o
  4. BIN
      pong
  5. 5
    17
      pong.cpp
  6. BIN
      pong.o

+ 6
- 3
Classes.h View File

5
 const int gameHeight = 600;
5
 const int gameHeight = 600;
6
 
6
 
7
 
7
 
8
+
8
 class Paddle : public sf::RectangleShape {
9
 class Paddle : public sf::RectangleShape {
9
     private:
10
     private:
10
     float x,
11
     float x,
28
         float radius = 10,
29
         float radius = 10,
29
               angle = 0;
30
               angle = 0;
30
         const float ballSpeed = 400;
31
         const float ballSpeed = 400;
32
+        sf::SoundBuffer ballSoundBuffer;
33
+        sf::Sound ballSound;
31
     
34
     
32
     public:
35
     public:
33
         Ball();
36
         Ball();
40
         float getRadius() const;
43
         float getRadius() const;
41
         float getAngle() const;
44
         float getAngle() const;
42
         void setAngle(float a);
45
         void setAngle(float a);
43
-        void checkLeftPaddle(const Paddle &leftPaddle, sf::Sound &ballSound);
44
-        void checkRightPaddle(const Paddle &rightPaddle, sf::Sound &ballSound);
45
-        void checkCollisions(sf::Sound &ballsound);
46
+        void checkLeftPaddle(const Paddle &leftPaddle);
47
+        void checkRightPaddle(const Paddle &rightPaddle);
48
+        void checkCollisions();
46
 
49
 
47
 };
50
 };

+ 6
- 3
functions.cpp View File

5
 
5
 
6
 
6
 
7
 Ball::Ball() {
7
 Ball::Ball() {
8
+    if (!ballSoundBuffer.loadFromFile("resources/ball.wav"))
9
+        exit(EXIT_FAILURE);
10
+    ballSound.setBuffer(ballSoundBuffer);
8
     setRadius(radius);
11
     setRadius(radius);
9
     setOutlineThickness(3);
12
     setOutlineThickness(3);
10
     setOutlineColor(sf::Color::Black);
13
     setOutlineColor(sf::Color::Black);
53
     angle = a;
56
     angle = a;
54
 }
57
 }
55
 
58
 
56
-void Ball::checkLeftPaddle(const Paddle &leftPaddle, sf::Sound &ballSound) {
59
+void Ball::checkLeftPaddle(const Paddle &leftPaddle) {
57
     
60
     
58
     if (getPosition().x - radius < leftPaddle.getX() + leftPaddle.getSize().x / 2 &&
61
     if (getPosition().x - radius < leftPaddle.getX() + leftPaddle.getSize().x / 2 &&
59
         getPosition().x - radius > leftPaddle.getX() &&
62
         getPosition().x - radius > leftPaddle.getX() &&
71
     }
74
     }
72
 }
75
 }
73
 
76
 
74
-void Ball::checkRightPaddle(const Paddle &rightPaddle, sf::Sound &ballSound) {
77
+void Ball::checkRightPaddle(const Paddle &rightPaddle) {
75
     
78
     
76
     if (getPosition().x + radius > rightPaddle.getX() - rightPaddle.getSize().x / 2 &&
79
     if (getPosition().x + radius > rightPaddle.getX() - rightPaddle.getSize().x / 2 &&
77
         getPosition().x + radius < rightPaddle.getX() &&
80
         getPosition().x + radius < rightPaddle.getX() &&
88
     }
91
     }
89
 }
92
 }
90
 
93
 
91
-void Ball::checkCollisions(sf::Sound &ballSound) {
94
+void Ball::checkCollisions() {
92
     if (getY() - radius < 0.f)
95
     if (getY() - radius < 0.f)
93
             {
96
             {
94
                 ballSound.play();
97
                 ballSound.play();

BIN
functions.o View File


BIN
pong View File


+ 5
- 17
pong.cpp View File

14
 
14
 
15
 using namespace std;
15
 using namespace std;
16
 
16
 
17
-string resourcesDir()
18
-{
19
-#ifdef SFML_SYSTEM_IOS
20
-    return "";
21
-#else
22
-    return "resources/";
23
-#endif
24
-}
25
-
26
 
17
 
27
 ////////////////////////////////////////////////////////////
18
 ////////////////////////////////////////////////////////////
28
 /// Entry point of application
19
 /// Entry point of application
45
     window.setVerticalSyncEnabled(true);
36
     window.setVerticalSyncEnabled(true);
46
 
37
 
47
     // Load the sounds used in the game
38
     // Load the sounds used in the game
48
-    sf::SoundBuffer ballSoundBuffer;
49
-    if (!ballSoundBuffer.loadFromFile(resourcesDir() + "ball.wav"))
50
-        return EXIT_FAILURE;
51
-    sf::Sound ballSound(ballSoundBuffer);
39
+    
52
 
40
 
53
     // Create the left paddle
41
     // Create the left paddle
54
     Paddle leftPaddle;
42
     Paddle leftPaddle;
61
 
49
 
62
     // Load the text font
50
     // Load the text font
63
     sf::Font font;
51
     sf::Font font;
64
-    if (!font.loadFromFile(resourcesDir() + "sansation.ttf"))
52
+    if (!font.loadFromFile("resources/sansation.ttf"))
65
         return EXIT_FAILURE;
53
         return EXIT_FAILURE;
66
 
54
 
67
     // Initialize the pause message
55
     // Initialize the pause message
198
                 pauseMessage.setString("You Won!\n" + inputString);
186
                 pauseMessage.setString("You Won!\n" + inputString);
199
             }
187
             }
200
 
188
 
201
-            ball.checkCollisions(ballSound);
189
+            ball.checkCollisions();
202
 
190
 
203
             // Check the collisions between the ball and the paddles
191
             // Check the collisions between the ball and the paddles
204
             // Left Paddle
192
             // Left Paddle
205
-            ball.checkLeftPaddle(leftPaddle, ballSound);
193
+            ball.checkLeftPaddle(leftPaddle);
206
 
194
 
207
             // Right Paddle
195
             // Right Paddle
208
-            ball.checkRightPaddle(rightPaddle, ballSound);
196
+            ball.checkRightPaddle(rightPaddle);
209
             
197
             
210
         }
198
         }
211
 
199
 

BIN
pong.o View File