Daniel 5 anni fa
parent
commit
9d0e7e37a5
11 ha cambiato i file con 191 aggiunte e 86 eliminazioni
  1. 2
    2
      .vscode/c_cpp_properties.json
  2. 82
    0
      .vscode/settings.json
  3. 0
    22
      Ball.h
  4. 43
    0
      Classes.h
  5. 6
    2
      Makefile
  6. 0
    20
      Paddle.h
  7. 47
    4
      functions.cpp
  8. BIN
      functions.o
  9. BIN
      pong
  10. 11
    36
      pong.cpp
  11. BIN
      pong.o

+ 2
- 2
.vscode/c_cpp_properties.json Vedi File

@@ -6,10 +6,10 @@
6 6
                 "${workspaceFolder}/**"
7 7
             ],
8 8
             "defines": [],
9
-            "compilerPath": "/usr/bin/clang",
9
+            "compilerPath": "/usr/bin/g++",
10 10
             "cStandard": "c11",
11 11
             "cppStandard": "c++17",
12
-            "intelliSenseMode": "clang-x64"
12
+            "intelliSenseMode": "gcc-x64"
13 13
         }
14 14
     ],
15 15
     "version": 4

+ 82
- 0
.vscode/settings.json Vedi File

@@ -0,0 +1,82 @@
1
+{
2
+    "files.associations": {
3
+        "cctype": "cpp",
4
+        "clocale": "cpp",
5
+        "cmath": "cpp",
6
+        "cstdarg": "cpp",
7
+        "cstddef": "cpp",
8
+        "cstdio": "cpp",
9
+        "cstdlib": "cpp",
10
+        "cstring": "cpp",
11
+        "ctime": "cpp",
12
+        "cwchar": "cpp",
13
+        "cwctype": "cpp",
14
+        "array": "cpp",
15
+        "atomic": "cpp",
16
+        "*.tcc": "cpp",
17
+        "bitset": "cpp",
18
+        "chrono": "cpp",
19
+        "codecvt": "cpp",
20
+        "condition_variable": "cpp",
21
+        "cstdint": "cpp",
22
+        "deque": "cpp",
23
+        "list": "cpp",
24
+        "unordered_map": "cpp",
25
+        "vector": "cpp",
26
+        "exception": "cpp",
27
+        "algorithm": "cpp",
28
+        "functional": "cpp",
29
+        "iterator": "cpp",
30
+        "map": "cpp",
31
+        "memory": "cpp",
32
+        "memory_resource": "cpp",
33
+        "numeric": "cpp",
34
+        "optional": "cpp",
35
+        "random": "cpp",
36
+        "ratio": "cpp",
37
+        "set": "cpp",
38
+        "string": "cpp",
39
+        "string_view": "cpp",
40
+        "system_error": "cpp",
41
+        "tuple": "cpp",
42
+        "type_traits": "cpp",
43
+        "utility": "cpp",
44
+        "fstream": "cpp",
45
+        "initializer_list": "cpp",
46
+        "iomanip": "cpp",
47
+        "iosfwd": "cpp",
48
+        "iostream": "cpp",
49
+        "istream": "cpp",
50
+        "limits": "cpp",
51
+        "mutex": "cpp",
52
+        "new": "cpp",
53
+        "ostream": "cpp",
54
+        "sstream": "cpp",
55
+        "stdexcept": "cpp",
56
+        "streambuf": "cpp",
57
+        "thread": "cpp",
58
+        "cinttypes": "cpp",
59
+        "typeinfo": "cpp",
60
+        "variant": "cpp",
61
+        "__bit_reference": "cpp",
62
+        "__config": "cpp",
63
+        "__debug": "cpp",
64
+        "__errc": "cpp",
65
+        "__functional_base": "cpp",
66
+        "__hash_table": "cpp",
67
+        "__locale": "cpp",
68
+        "__mutex_base": "cpp",
69
+        "__node_handle": "cpp",
70
+        "__nullptr": "cpp",
71
+        "__split_buffer": "cpp",
72
+        "__string": "cpp",
73
+        "__threading_support": "cpp",
74
+        "__tree": "cpp",
75
+        "__tuple": "cpp",
76
+        "bit": "cpp",
77
+        "ios": "cpp",
78
+        "locale": "cpp",
79
+        "queue": "cpp",
80
+        "stack": "cpp"
81
+    }
82
+}

+ 0
- 22
Ball.h Vedi File

@@ -1,22 +0,0 @@
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
-        float getRadius() const;
20
-        void render(sf::RenderWindow) const;
21
-
22
-};

+ 43
- 0
Classes.h Vedi File

@@ -0,0 +1,43 @@
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
+};
21
+
22
+class Ball : public sf::CircleShape {
23
+    private:
24
+        float radius = 10,
25
+              angle = 0;
26
+        const float ballSpeed = 400;
27
+    
28
+    public:
29
+        Ball();
30
+        Ball(int radius);
31
+        void move(float x, float y);
32
+        void setPosition(float x, float y);
33
+        float getX() const;
34
+        float getY() const;
35
+        float getSpeed() const;
36
+        float getRadius() const;
37
+        float getAngle() const;
38
+        void setAngle(float a);
39
+        void render(sf::RenderWindow) const;
40
+        void checkLeftPaddle(Paddle leftPaddle, sf::Sound ballSound);
41
+        void checkRightPaddle(Paddle rightPaddle, sf::Sound ballSound);
42
+
43
+};

+ 6
- 2
Makefile Vedi File

@@ -1,2 +1,6 @@
1
-all: pong.cpp resources
2
-	g++ pong.cpp functions.cpp -o pong  -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio
1
+all: pong.o functions.o resources
2
+	g++ pong.o functions.o -o pong  -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio; ./pong
3
+pong.o: pong.cpp
4
+	g++ -c pong.cpp
5
+functions.o: functions.cpp
6
+	g++ -c functions.cpp

+ 0
- 20
Paddle.h Vedi File

@@ -1,20 +0,0 @@
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
-};

+ 47
- 4
functions.cpp Vedi File

@@ -1,10 +1,11 @@
1
-#include "Ball.h"
2
-#include "Paddle.h"
1
+#include "Classes.h"
3 2
 #include "Game.h"
3
+#include <cmath>
4
+#include <iostream>
4 5
 
5 6
 
6 7
 Ball::Ball() {
7
-    setRadius(radius - 3);
8
+    setRadius(radius);
8 9
     setOutlineThickness(3);
9 10
     setOutlineColor(sf::Color::Black);
10 11
     setFillColor(sf::Color::White);
@@ -12,7 +13,7 @@ Ball::Ball() {
12 13
 }
13 14
 
14 15
 Ball::Ball(int r) {
15
-    setRadius(r - 3);
16
+    setRadius(r);
16 17
     setOutlineThickness(3);
17 18
     setOutlineColor(sf::Color::Black);
18 19
     setFillColor(sf::Color::White);
@@ -44,6 +45,48 @@ float Ball::getRadius() const {
44 45
     return radius;
45 46
 }
46 47
 
48
+float Ball::getAngle() const {
49
+    return angle;
50
+}
51
+
52
+void Ball::setAngle(float a) {
53
+    angle = a;
54
+}
55
+
56
+void Ball::checkLeftPaddle(Paddle leftPaddle, sf::Sound ballSound) {
57
+    
58
+    if (getPosition().x - radius < leftPaddle.getX() + leftPaddle.getSize().x / 2 &&
59
+        getPosition().x - radius > leftPaddle.getX() &&
60
+        getPosition().y + radius <= leftPaddle.getY() + leftPaddle.getSize().y / 2 &&
61
+        getPosition().y - radius >= leftPaddle.getY() - leftPaddle.getSize().y / 2)
62
+        
63
+    {
64
+        if (getPosition().y > leftPaddle.getY())
65
+            angle = M_PI - angle + (rand() % 20) * M_PI / 180;
66
+        else
67
+            angle = M_PI - angle - (rand() % 20) * M_PI / 180;
68
+
69
+        ballSound.play();
70
+        setPosition(leftPaddle.getX() + radius + leftPaddle.getSize().x / 2 + 0.1f, getPosition().y);
71
+    }
72
+}
73
+
74
+void Ball::checkRightPaddle(Paddle rightPaddle, sf::Sound ballSound) {
75
+    
76
+    if (getPosition().x + radius > rightPaddle.getX() - rightPaddle.getSize().x / 2 &&
77
+        getPosition().x + radius < rightPaddle.getX() &&
78
+        getPosition().y + radius >= rightPaddle.getY() - rightPaddle.getSize().y / 2 &&
79
+        getPosition().y - radius <= rightPaddle.getY() + rightPaddle.getSize().y / 2)
80
+    {
81
+        if (getPosition().y > rightPaddle.getY())
82
+            angle = radius - angle + (rand() % 20) * radius / 180;
83
+        else
84
+            angle = radius - angle - (rand() % 20) * M_PI / 180;
85
+
86
+        ballSound.play();
87
+        setPosition(rightPaddle.getX() - radius - rightPaddle.getSize().x / 2 - 0.1, getPosition().y);
88
+    }
89
+}
47 90
 
48 91
 Paddle::Paddle() {
49 92
     size.x = 25;

BIN
functions.o Vedi File


BIN
pong Vedi File


+ 11
- 36
pong.cpp Vedi File

@@ -6,8 +6,7 @@
6 6
 #include <cmath>
7 7
 #include <ctime>
8 8
 #include <cstdlib>
9
-#include "Ball.h"
10
-#include "Paddle.h"
9
+#include "Classes.h"
11 10
 
12 11
 #ifdef SFML_SYSTEM_IOS
13 12
 #include <SFML/Main.hpp>
@@ -82,8 +81,7 @@ int main()
82 81
     sf::Clock AITimer;
83 82
     const sf::Time AITime   = sf::seconds(0.1f);
84 83
     const float paddleSpeed = 400.f;
85
-    float rightPaddleSpeed  = 0.f;
86
-    float ballAngle         = 0.f; // to be changed later
84
+    float rightPaddleSpeed  = 0.f; // to be changed later
87 85
 
88 86
     sf::Clock clock;
89 87
     bool isPlaying = false;
@@ -120,9 +118,9 @@ int main()
120 118
                     do
121 119
                     {
122 120
                         // Make sure the ball initial angle is not too much vertical
123
-                        ballAngle = (rand() % 360) * 2 * pi / 360;
121
+                        ball.setAngle((rand() % 360) * 2 * pi / 360);
124 122
                     }
125
-                    while (abs(cos(ballAngle)) < 0.7f);
123
+                    while (abs(cos(ball.getAngle())) < 0.7f);
126 124
                 }
127 125
             }
128 126
             
@@ -180,7 +178,7 @@ int main()
180 178
 
181 179
             // Move the ball
182 180
             float factor = ball.getSpeed() * deltaTime;
183
-            ball.move(cos(ballAngle) * factor, sin(ballAngle) * factor);
181
+            ball.move(cos(ball.getAngle()) * factor, sin(ball.getAngle()) * factor);
184 182
 
185 183
             #ifdef SFML_SYSTEM_IOS
186 184
             const string inputString = "Touch the screen to restart";
@@ -189,7 +187,7 @@ int main()
189 187
             #endif
190 188
             
191 189
             // Check collisions between the ball and the screen
192
-            if (ball.getX() - ball.getRadius() < 0.f)
190
+            if (ball.getX() - ball.getRadius() < 0)
193 191
             {
194 192
                 isPlaying = false;
195 193
                 pauseMessage.setString("You Lost!\n" + inputString);
@@ -202,46 +200,23 @@ int main()
202 200
             if (ball.getY() - ball.getRadius() < 0.f)
203 201
             {
204 202
                 ballSound.play();
205
-                ballAngle = -ballAngle;
203
+                ball.setAngle(-ball.getAngle());
206 204
                 ball.setPosition(ball.getX(), ball.getRadius() + 0.1f);
207 205
             }
208 206
             if (ball.getY() + ball.getRadius() > gameHeight)
209 207
             {
210 208
                 ballSound.play();
211
-                ballAngle = -ballAngle;
209
+                ball.setAngle(-ball.getAngle());
212 210
                 ball.setPosition(ball.getX(), gameHeight - ball.getRadius() - 0.1f);
213 211
             }
214 212
 
215 213
             // Check the collisions between the ball and the paddles
216 214
             // Left Paddle
217
-            if (ball.getX() - ball.getRadius() < leftPaddle.getX() + leftPaddle.getSize().x / 2 &&
218
-                ball.getX() - ball.getRadius() > leftPaddle.getX() &&
219
-                ball.getY() + ball.getRadius() >= leftPaddle.getY() - leftPaddle.getSize().y / 2 &&
220
-                ball.getY() - ball.getRadius() <= leftPaddle.getY() + leftPaddle.getSize().y / 2)
221
-            {
222
-                if (ball.getY() > leftPaddle.getY())
223
-                    ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
224
-                else
225
-                    ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
226
-
227
-                ballSound.play();
228
-                ball.setPosition(leftPaddle.getX() + ball.getRadius() + leftPaddle.getSize().x / 2 + 0.1f, ball.getY());
229
-            }
215
+            ball.checkLeftPaddle(leftPaddle, ballSound);
230 216
 
231 217
             // Right Paddle
232
-            if (ball.getX() + ball.getRadius() > rightPaddle.getX() - leftPaddle.getSize().x / 2 &&
233
-                ball.getX() + ball.getRadius() < rightPaddle.getX() &&
234
-                ball.getY() + ball.getRadius() >= rightPaddle.getY() - leftPaddle.getSize().y / 2 &&
235
-                ball.getY() - ball.getRadius() <= rightPaddle.getY() + leftPaddle.getSize().y / 2)
236
-            {
237
-                if (ball.getY() > rightPaddle.getY())
238
-                    ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
239
-                else
240
-                    ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
241
-
242
-                ballSound.play();
243
-                ball.setPosition(rightPaddle.getX() - ball.getRadius() - leftPaddle.getSize().x / 2 - 0.1f, ball.getY());
244
-            }
218
+            ball.checkRightPaddle(rightPaddle, ballSound);
219
+            
245 220
         }
246 221
 
247 222
         // Clear the window

BIN
pong.o Vedi File