Advertisement
Guest User

Untitled

a guest
Apr 28th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.27 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/Audio.hpp>
  3. #include <SFML/Network.hpp>
  4. #include <random>  
  5. #include <cstdlib>
  6. #include <string>
  7. #define EXIT_FAILURE 1
  8. #define EXIT_SUCCESS 0
  9.  
  10. int randmoNumber (int intervalMin, int intervalMax);
  11. void loadMusic (std::string locationMusic);
  12. void loadFont (std::string locationFont);
  13.  
  14. int main (int argc, char * argv[])
  15. {
  16.     const int WIDTH = 700, HEIGHT = 400, DEPTH_OF_COLOR = 32, MAX_FPS = 60, PADDLE_WIDTH = 20, PADDLE_HEIGHT = 90;
  17.     const float PI = 3.14159;
  18.     const std::string SEPARATION = "|";
  19.     int scorePlayerFirst = 0, scorePlayerSecond = 0;
  20.     float ballRadius = 10.f;
  21.     std::string locationMainMusic = "\"resources/fontComic.ttf\"";
  22.     std::string locationMainFont = "\"resources/soundtrack.wav\"";
  23.     enum MoveBall { up, down, right, left };
  24.     sf::Vector2f paddleSize (PADDLE_WIDTH, PADDLE_HEIGHT);
  25.  
  26.     //Load font
  27.     sf::Font mainFont;
  28.     if (!mainFont.loadFromFile ("resources/fontComic.ttf"))
  29.         return EXIT_FAILURE;
  30.    
  31.     //Load music
  32.     sf::Music mainMusic;
  33.     if (!mainMusic.openFromFile ("resources/soundtrack.wav"))
  34.         return EXIT_FAILURE;
  35.     mainMusic.setLoop (true);
  36.     mainMusic.setVolume (1);
  37.     mainMusic.play ();
  38.  
  39.     // Create the left paddle
  40.     sf::RectangleShape leftPaddle;
  41.     leftPaddle.setSize (paddleSize - sf::Vector2f (3, 3));
  42.     leftPaddle.setOutlineThickness (3);
  43.     leftPaddle.setOutlineColor (sf::Color::White);
  44.     leftPaddle.setFillColor (sf::Color::Black);
  45.     leftPaddle.setOrigin (paddleSize / 2.f);
  46.     leftPaddle.setPosition (20, 200);
  47.  
  48.     // Create the right paddle
  49.     sf::RectangleShape rightPaddle;
  50.     rightPaddle.setSize (paddleSize - sf::Vector2f (3, 3));
  51.     rightPaddle.setOutlineThickness (3);
  52.     rightPaddle.setOutlineColor (sf::Color::White);
  53.     rightPaddle.setFillColor (sf::Color::Black);
  54.     rightPaddle.setOrigin (paddleSize / 2.f);
  55.     rightPaddle.setPosition (680, 200);
  56.  
  57.     // Create the ball
  58.     sf::CircleShape ball;
  59.     ball.setRadius (ballRadius - 3);
  60.     ball.setOutlineThickness (3);
  61.     ball.setOutlineColor (sf::Color::White);
  62.     ball.setFillColor (sf::Color::Black);
  63.     ball.setOrigin (ballRadius / 2, ballRadius / 2);
  64.     ball.setPosition (350, 200);
  65.    
  66.     //TODO: Score
  67.     sf::Text score ("wynik", mainFont);
  68.     score.setCharacterSize (30);
  69.     score.setStyle (sf::Text::Bold);
  70.     score.setPosition (325, 10);
  71.  
  72.     //Main settings of windows
  73.     sf::RenderWindow mainWindow (sf::VideoMode (WIDTH, HEIGHT, DEPTH_OF_COLOR), "PING-PONG", sf::Style::Titlebar | sf::Style::Close);
  74.     mainWindow.setFramerateLimit (MAX_FPS);
  75.  
  76.     //Main loop game
  77.     while (mainWindow.isOpen ())
  78.     {
  79.         sf::Event eventCloseMainWindow;
  80.         while (mainWindow.pollEvent (eventCloseMainWindow))
  81.         {
  82.             if (eventCloseMainWindow.type == sf::Event::Closed)
  83.                 mainWindow.close ();
  84.            
  85.         }
  86.  
  87.         //Limit move right paddle
  88.         if (rightPaddle.getPosition ().y < 0)
  89.         {
  90.             rightPaddle.setPosition (680, 0);
  91.         }
  92.         else if (rightPaddle.getPosition ().y > 400)
  93.         {
  94.             rightPaddle.setPosition (680, 400);
  95.         }
  96.         //Limit move left paddle
  97.         if (leftPaddle.getPosition ().y < 0)
  98.         {
  99.             leftPaddle.setPosition (20, 0);
  100.         }
  101.         else if (leftPaddle.getPosition ().y > 400)
  102.         {
  103.             leftPaddle.setPosition (20, 400);
  104.         }
  105.  
  106.         //Move left paddles
  107.         if (sf::Keyboard::isKeyPressed (sf::Keyboard::Up))
  108.         {
  109.             leftPaddle.move (0, -3);
  110.         }
  111.         else if (sf::Keyboard::isKeyPressed (sf::Keyboard::Down))
  112.         {
  113.             leftPaddle.move (0, 3);
  114.         }
  115.         //Move right paddles
  116.         if (sf::Keyboard::isKeyPressed (sf::Keyboard::W))
  117.         {
  118.             rightPaddle.move (0, -3);
  119.         }
  120.         else if (sf::Keyboard::isKeyPressed (sf::Keyboard::S))
  121.         {
  122.             rightPaddle.move (0, 3);
  123.         }
  124.        
  125.         //Colision bounds
  126.         sf::FloatRect leftPaddleCollision = leftPaddle.getGlobalBounds ();
  127.         sf::FloatRect rightPaddleCollision = rightPaddle.getGlobalBounds ();
  128.         sf::FloatRect ballCollision = ball.getGlobalBounds ();
  129.         //TODO: Collision action
  130.         if (leftPaddleCollision.intersects (ballCollision))
  131.         {
  132.             return EXIT_SUCCESS;
  133.         }
  134.         mainWindow.clear (sf::Color::Black);
  135. //      mainWindow.draw (score);
  136.         mainWindow.draw (leftPaddle);
  137.         mainWindow.draw (rightPaddle);
  138.         mainWindow.draw (ball);
  139.         mainWindow.display ();
  140.     }
  141.  
  142.     return 0;
  143. }
  144.  
  145. int randmoNumber (int intervalMin, int intervalMax)
  146. {
  147.     std::random_device random;
  148.     std::mt19937 gen (random ());
  149.     std::uniform_int_distribution<> randomNumber (intervalMin, intervalMax);
  150.     return randomNumber (gen);
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement