瀏覽代碼

Moved BallSound to the Ball class

Daniel 5 年之前
父節點
當前提交
be61ca233f
共有 6 個檔案被更改,包括 17 行新增23 行删除
  1. 6
    3
      Classes.h
  2. 6
    3
      functions.cpp
  3. 二進制
      functions.o
  4. 二進制
      pong
  5. 5
    17
      pong.cpp
  6. 二進制
      pong.o

+ 6
- 3
Classes.h 查看文件

@@ -5,6 +5,7 @@ const int gameWidth = 800;
5 5
 const int gameHeight = 600;
6 6
 
7 7
 
8
+
8 9
 class Paddle : public sf::RectangleShape {
9 10
     private:
10 11
     float x,
@@ -28,6 +29,8 @@ class Ball : public sf::CircleShape {
28 29
         float radius = 10,
29 30
               angle = 0;
30 31
         const float ballSpeed = 400;
32
+        sf::SoundBuffer ballSoundBuffer;
33
+        sf::Sound ballSound;
31 34
     
32 35
     public:
33 36
         Ball();
@@ -40,8 +43,8 @@ class Ball : public sf::CircleShape {
40 43
         float getRadius() const;
41 44
         float getAngle() const;
42 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 查看文件

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

二進制
functions.o 查看文件


二進制
pong 查看文件


+ 5
- 17
pong.cpp 查看文件

@@ -14,15 +14,6 @@
14 14
 
15 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 19
 /// Entry point of application
@@ -45,10 +36,7 @@ int main()
45 36
     window.setVerticalSyncEnabled(true);
46 37
 
47 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 41
     // Create the left paddle
54 42
     Paddle leftPaddle;
@@ -61,7 +49,7 @@ int main()
61 49
 
62 50
     // Load the text font
63 51
     sf::Font font;
64
-    if (!font.loadFromFile(resourcesDir() + "sansation.ttf"))
52
+    if (!font.loadFromFile("resources/sansation.ttf"))
65 53
         return EXIT_FAILURE;
66 54
 
67 55
     // Initialize the pause message
@@ -198,14 +186,14 @@ int main()
198 186
                 pauseMessage.setString("You Won!\n" + inputString);
199 187
             }
200 188
 
201
-            ball.checkCollisions(ballSound);
189
+            ball.checkCollisions();
202 190
 
203 191
             // Check the collisions between the ball and the paddles
204 192
             // Left Paddle
205
-            ball.checkLeftPaddle(leftPaddle, ballSound);
193
+            ball.checkLeftPaddle(leftPaddle);
206 194
 
207 195
             // Right Paddle
208
-            ball.checkRightPaddle(rightPaddle, ballSound);
196
+            ball.checkRightPaddle(rightPaddle);
209 197
             
210 198
         }
211 199
 

二進制
pong.o 查看文件