Daniel 4 gadus atpakaļ
vecāks
revīzija
616bc10e2a
6 mainītis faili ar 52 papildinājumiem un 26 dzēšanām
  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ārs
      pong
  6. 18
    20
      pong.cpp

+ 16
- 0
.vscode/c_cpp_properties.json Parādīt failu

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 Parādīt failu

16
         float getX() const;
16
         float getX() const;
17
         float getY() const;
17
         float getY() const;
18
         float getSpeed() const;
18
         float getSpeed() const;
19
+        float getRadius() const;
19
         void render(sf::RenderWindow) const;
20
         void render(sf::RenderWindow) const;
20
 
21
 
21
 };
22
 };

+ 7
- 1
Game.h Parādīt failu

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

+ 10
- 5
functions.cpp Parādīt failu

40
     sf::Transformable::move(x, y);
40
     sf::Transformable::move(x, y);
41
 }
41
 }
42
 
42
 
43
+float Ball::getRadius() const {
44
+    return radius;
45
+}
46
+
43
 
47
 
44
 Paddle::Paddle() {
48
 Paddle::Paddle() {
45
     size.x = 25;
49
     size.x = 25;
71
     sf::Transformable::move(x, y);
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
 }

Binārs
pong Parādīt failu


+ 18
- 20
pong.cpp Parādīt failu

39
     const float pi = 3.1415;
39
     const float pi = 3.1415;
40
     const int gameWidth = 800;
40
     const int gameWidth = 800;
41
     const int gameHeight = 600;
41
     const int gameHeight = 600;
42
-    
43
-    float ballRadius = 10;
44
 
42
 
45
     // Create the window of the application
43
     // Create the window of the application
46
     sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong",
44
     sf::RenderWindow window(sf::VideoMode(gameWidth, gameHeight, 32), "SFML Pong",
172
             if (AITimer.getElapsedTime() > AITime)
170
             if (AITimer.getElapsedTime() > AITime)
173
             {
171
             {
174
                 AITimer.restart();
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
                     rightPaddleSpeed = paddleSpeed;
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
                     rightPaddleSpeed = -paddleSpeed;
176
                     rightPaddleSpeed = -paddleSpeed;
179
                 else
177
                 else
180
                     rightPaddleSpeed = 0.f;
178
                     rightPaddleSpeed = 0.f;
191
             #endif
189
             #endif
192
             
190
             
193
             // Check collisions between the ball and the screen
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
                 isPlaying = false;
194
                 isPlaying = false;
197
                 pauseMessage.setString("You Lost!\n" + inputString);
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
                 isPlaying = false;
199
                 isPlaying = false;
202
                 pauseMessage.setString("You Won!\n" + inputString);
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
                 ballSound.play();
204
                 ballSound.play();
207
                 ballAngle = -ballAngle;
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
                 ballSound.play();
210
                 ballSound.play();
213
                 ballAngle = -ballAngle;
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
             // Check the collisions between the ball and the paddles
215
             // Check the collisions between the ball and the paddles
218
             // Left Paddle
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
                 if (ball.getY() > leftPaddle.getY())
222
                 if (ball.getY() > leftPaddle.getY())
225
                     ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
223
                     ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
227
                     ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
225
                     ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
228
 
226
 
229
                 ballSound.play();
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
             // Right Paddle
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
                 if (ball.getY() > rightPaddle.getY())
237
                 if (ball.getY() > rightPaddle.getY())
240
                     ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
238
                     ballAngle = pi - ballAngle + (rand() % 20) * pi / 180;
242
                     ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
240
                     ballAngle = pi - ballAngle - (rand() % 20) * pi / 180;
243
 
241
 
244
                 ballSound.play();
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