Daniel 5 yıl önce
ebeveyn
işleme
616bc10e2a
6 değiştirilmiş dosya ile 52 ekleme ve 26 silme
  1. 16
    0
      .vscode/c_cpp_properties.json
  2. 1
    0
      Ball.h
  3. 7
    1
      Game.h
  4. 10
    5
      functions.cpp
  5. BIN
      pong
  6. 18
    20
      pong.cpp

+ 16
- 0
.vscode/c_cpp_properties.json Dosyayı Görüntüle

@@ -0,0 +1,16 @@
1
+{
2
+    "configurations": [
3
+        {
4
+            "name": "Linux",
5
+            "includePath": [
6
+                "${workspaceFolder}/**"
7
+            ],
8
+            "defines": [],
9
+            "compilerPath": "/usr/bin/clang",
10
+            "cStandard": "c11",
11
+            "cppStandard": "c++17",
12
+            "intelliSenseMode": "clang-x64"
13
+        }
14
+    ],
15
+    "version": 4
16
+}

+ 1
- 0
Ball.h Dosyayı Görüntüle

@@ -16,6 +16,7 @@ class Ball : public sf::CircleShape {
16 16
         float getX() const;
17 17
         float getY() const;
18 18
         float getSpeed() const;
19
+        float getRadius() const;
19 20
         void render(sf::RenderWindow) const;
20 21
 
21 22
 };

+ 7
- 1
Game.h Dosyayı Görüntüle

@@ -4,9 +4,15 @@
4 4
 class Game {
5 5
     private:
6 6
         sf::Clock AITimer,
7
-                  Clock;
7
+                  clock;
8
+        sf::Time AITime;
9
+        float paddleSpeed,
10
+              rightPaddleSpeed,
11
+              ballAngle;
8 12
         bool isPlaying;
13
+        const int gameWidth = 800, gameHeight = 600;
9 14
     public:
10 15
         Game();
16
+        void whilePlaying(Paddle leftPaddle, Paddle rightPaddle, Ball ball, sf::Window window);
11 17
 
12 18
 };

+ 10
- 5
functions.cpp Dosyayı Görüntüle

@@ -40,6 +40,10 @@ void Ball::move(float x, float y) {
40 40
     sf::Transformable::move(x, y);
41 41
 }
42 42
 
43
+float Ball::getRadius() const {
44
+    return radius;
45
+}
46
+
43 47
 
44 48
 Paddle::Paddle() {
45 49
     size.x = 25;
@@ -71,9 +75,10 @@ void Paddle::move(float x, float y) {
71 75
     sf::Transformable::move(x, y);
72 76
 }
73 77
 
74
-Ball::Ball() {
75
-    const sf::Time AITime = sf::seconds(0.1f);
76
-    const float paddleSpeed = 400.f;
77
-    float rightPaddleSpeed = 0.f;
78
-    float ballAngle = 0.f;
78
+Game::Game() {
79
+    isPlaying = false;
80
+    AITime = sf::seconds(0.1);
81
+    paddleSpeed = 400;
82
+    rightPaddleSpeed = 0;
83
+    ballAngle = 0;
79 84
 }


+ 18
- 20
pong.cpp Dosyayı Görüntüle

@@ -39,8 +39,6 @@ int main()
39 39
     const float pi = 3.1415;
40 40
     const int gameWidth = 800;
41 41
     const int gameHeight = 600;
42
-    
43
-    float ballRadius = 10;
44 42
 
45 43
     // Create the window of the application
46 44
     sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong",
@@ -172,9 +170,9 @@ int main()
172 170
             if (AITimer.getElapsedTime() > AITime)
173 171
             {
174 172
                 AITimer.restart();
175
-                if (ball.getY() + ballRadius > rightPaddle.getY() + leftPaddle.getSize().y / 2)
173
+                if (ball.getY() + ball.getRadius() > rightPaddle.getY() + leftPaddle.getSize().y / 2)
176 174
                     rightPaddleSpeed = paddleSpeed;
177
-                else if (ball.getY() - ballRadius < rightPaddle.getY() - leftPaddle.getSize().y / 2)
175
+                else if (ball.getY() - ball.getRadius() < rightPaddle.getY() - leftPaddle.getSize().y / 2)
178 176
                     rightPaddleSpeed = -paddleSpeed;
179 177
                 else
180 178
                     rightPaddleSpeed = 0.f;
@@ -191,35 +189,35 @@ int main()
191 189
             #endif
192 190
             
193 191
             // Check collisions between the ball and the screen
194
-            if (ball.getX() - ballRadius < 0.f)
192
+            if (ball.getX() - ball.getRadius() < 0.f)
195 193
             {
196 194
                 isPlaying = false;
197 195
                 pauseMessage.setString("You Lost!\n" + inputString);
198 196
             }
199
-            if (ball.getX() + ballRadius > gameWidth)
197
+            if (ball.getX() + ball.getRadius() > gameWidth)
200 198
             {
201 199
                 isPlaying = false;
202 200
                 pauseMessage.setString("You Won!\n" + inputString);
203 201
             }
204
-            if (ball.getY() - ballRadius < 0.f)
202
+            if (ball.getY() - ball.getRadius() < 0.f)
205 203
             {
206 204
                 ballSound.play();
207 205
                 ballAngle = -ballAngle;
208
-                ball.setPosition(ball.getX(), ballRadius + 0.1f);
206
+                ball.setPosition(ball.getX(), ball.getRadius() + 0.1f);
209 207
             }
210
-            if (ball.getY() + ballRadius > gameHeight)
208
+            if (ball.getY() + ball.getRadius() > gameHeight)
211 209
             {
212 210
                 ballSound.play();
213 211
                 ballAngle = -ballAngle;
214
-                ball.setPosition(ball.getX(), gameHeight - ballRadius - 0.1f);
212
+                ball.setPosition(ball.getX(), gameHeight - ball.getRadius() - 0.1f);
215 213
             }
216 214
 
217 215
             // Check the collisions between the ball and the paddles
218 216
             // Left Paddle
219
-            if (ball.getX() - ballRadius < leftPaddle.getX() + leftPaddle.getSize().x / 2 &&
220
-                ball.getX() - ballRadius > leftPaddle.getX() &&
221
-                ball.getY() + ballRadius >= leftPaddle.getY() - leftPaddle.getSize().y / 2 &&
222
-                ball.getY() - ballRadius <= leftPaddle.getY() + leftPaddle.getSize().y / 2)
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)
223 221
             {
224 222
                 if (ball.getY() > leftPaddle.getY())
225 223
                     ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
@@ -227,14 +225,14 @@ int main()
227 225
                     ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
228 226
 
229 227
                 ballSound.play();
230
-                ball.setPosition(leftPaddle.getX() + ballRadius + leftPaddle.getSize().x / 2 + 0.1f, ball.getY());
228
+                ball.setPosition(leftPaddle.getX() + ball.getRadius() + leftPaddle.getSize().x / 2 + 0.1f, ball.getY());
231 229
             }
232 230
 
233 231
             // Right Paddle
234
-            if (ball.getX() + ballRadius > rightPaddle.getX() - leftPaddle.getSize().x / 2 &&
235
-                ball.getX() + ballRadius < rightPaddle.getX() &&
236
-                ball.getY() + ballRadius >= rightPaddle.getY() - leftPaddle.getSize().y / 2 &&
237
-                ball.getY() - ballRadius <= rightPaddle.getY() + leftPaddle.getSize().y / 2)
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)
238 236
             {
239 237
                 if (ball.getY() > rightPaddle.getY())
240 238
                     ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
@@ -242,7 +240,7 @@ int main()
242 240
                     ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
243 241
 
244 242
                 ballSound.play();
245
-                ball.setPosition(rightPaddle.getX() - ballRadius - leftPaddle.getSize().x / 2 - 0.1f, ball.getY());
243
+                ball.setPosition(rightPaddle.getX() - ball.getRadius() - leftPaddle.getSize().x / 2 - 0.1f, ball.getY());
246 244
             }
247 245
         }
248 246