Advertisement
Felanpro

Ping Pong (Graphics version)

Mar 24th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.70 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <SFML/Window.hpp>
  4. #include <SFML/Graphics.hpp>
  5. #include <SFML/Audio.hpp>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. void logic();
  11. void reset();
  12.  
  13. int width = 800;
  14. int height = 600;
  15.  
  16. //Player on coordinates
  17. int x_player1 = 5;
  18. int y_player1 = (height/2) - 80;
  19. int direction_of_player_1;
  20. int player1_points = 0;
  21.  
  22. //Player two coordinates
  23. int x_player2 = width - 25;
  24. int y_player2 = (height/2) - 80;
  25. int direction_of_player_2;
  26. int player2_points = 0;
  27.  
  28. //Ball coordinates
  29. int x_ball = (width / 2);
  30. int y_ball = (height / 2);
  31. int direction_of_ball;
  32. sf::RectangleShape ball;
  33.  
  34. int random_int; //This is essential
  35.  
  36. int main()
  37. {
  38.     srand(time(NULL));
  39.     //Initialize ball direction in the beginning
  40.     direction_of_ball = 4;
  41.  
  42.     sf::RenderWindow window(sf::VideoMode(width, height), "Ping Pong");
  43.     window.setFramerateLimit(70);
  44.     window.setVerticalSyncEnabled(true);
  45.  
  46.     //Player one rectangle
  47.     sf::RectangleShape player_one;
  48.     player_one.setFillColor(sf::Color::Black);
  49.     player_one.setSize(sf::Vector2f(20, 80));
  50.  
  51.     //Player two rectangle
  52.     sf::RectangleShape player_two;
  53.     player_two.setFillColor(sf::Color::Black);
  54.     player_two.setSize(sf::Vector2f(20, 80));
  55.  
  56.     //The ball
  57.     ball.setFillColor(sf::Color::Red);
  58.     ball.setSize(sf::Vector2f(20, 20));
  59.    
  60.     //Some 8 bit music
  61.     sf::Music music;
  62.     music.openFromFile("8bit_music.ogg");
  63.     music.setVolume(50);
  64.     music.setLoop(true); //Keep on going forever
  65.     music.play();
  66.  
  67.     //Some iconic text
  68.     sf::Font black_ops_font;
  69.     black_ops_font.loadFromFile("Black Ops 2 Font.ttf");
  70.  
  71.     sf::Text points_display_player1;
  72.     points_display_player1.setFont(black_ops_font);
  73.     points_display_player1.setFillColor(sf::Color::Black);
  74.     points_display_player1.setPosition((width/2) - 70, height - 50);
  75.     string points_string_player1;
  76.  
  77.     sf::Text points_display_player2;
  78.     points_display_player2.setFont(black_ops_font);
  79.     points_display_player2.setFillColor(sf::Color::Black);
  80.     points_display_player2.setPosition((width / 2) + 30, height - 50);
  81.     string points_string_player2;
  82.  
  83.     sf::Event event;
  84.     while (window.isOpen())
  85.     {
  86.         window.clear(sf::Color::White);
  87.         while (window.pollEvent(event))
  88.         {
  89.             if (event.type == sf::Event::Closed)
  90.                 window.close();
  91.  
  92.             if (event.type == sf::Event::KeyPressed)
  93.             {
  94.                 if (event.key.code == sf::Keyboard::W)
  95.                 {
  96.                     direction_of_player_1 = 1;
  97.                 }
  98.                 else if (event.key.code == sf::Keyboard::S)
  99.                 {
  100.                     direction_of_player_1 = 2;
  101.                 }
  102.                 else if (event.key.code == sf::Keyboard::Up)
  103.                 {
  104.                     direction_of_player_2 = 1;
  105.                 }
  106.                 else if (event.key.code == sf::Keyboard::Down)
  107.                 {
  108.                     direction_of_player_2 = 2;
  109.                 }
  110.             }
  111.         }
  112.         points_string_player1 = to_string(player1_points);
  113.         points_string_player2 = to_string(player2_points);
  114.         points_display_player1.setString(points_string_player1);
  115.         points_display_player2.setString(points_string_player2);
  116.  
  117.         logic();
  118.  
  119.         player_one.setPosition(x_player1, y_player1);
  120.         player_two.setPosition(x_player2, y_player2);
  121.         ball.setPosition(x_ball, y_ball);
  122.  
  123.         window.draw(player_one);
  124.         window.draw(player_two);
  125.         window.draw(ball);
  126.         window.draw(points_display_player1);
  127.         window.draw(points_display_player2);
  128.  
  129.         window.display();
  130.     }
  131.  
  132.     int pause; cin >> pause; //Pause the program
  133.     return 0;
  134. }
  135.  
  136. void logic()
  137. {
  138.     //Change position of player 1
  139.     if (direction_of_player_1 == 1)
  140.     {
  141.         if (y_player1 == 0)
  142.             ;
  143.         else
  144.             y_player1 -= 5;
  145.     }
  146.     else if (direction_of_player_1 == 2)
  147.     {
  148.         if ((y_player1 + 80) > height)
  149.             ;
  150.         else
  151.             y_player1 += 5;
  152.     }
  153.  
  154.     //Change position of player 2
  155.     if (direction_of_player_2 == 1)
  156.     {
  157.         if (y_player2 == 0)
  158.             ;
  159.         else
  160.             y_player2 -= 5;
  161.     }
  162.     else if (direction_of_player_2 == 2)
  163.     {
  164.         if ((y_player2 + 80) > height)
  165.             ;
  166.         else
  167.             y_player2 += 5;
  168.     }
  169.  
  170.     //Change position of the ball
  171.     //In case it touches player 1
  172.     if (abs(x_ball - x_player1) <= 20 )
  173.     {
  174.         if (y_ball > y_player1)
  175.         {
  176.             if (abs(y_ball - y_player1) < 80)
  177.             {
  178.                 random_int = (rand() % 3) + 1;
  179.                 direction_of_ball = random_int;
  180.             }
  181.         }
  182.         else if (y_ball < y_player1)
  183.         {
  184.             if (abs(y_ball - y_player1) < 20)
  185.             {
  186.                 random_int = (rand() % 3) + 1;
  187.                 direction_of_ball = random_int;
  188.             }
  189.         }
  190.     }
  191.     //In case it touches player 2
  192.     if (abs(x_ball - x_player2) <= 20)
  193.     {
  194.         if (y_ball > y_player2)
  195.         {
  196.             if (abs(y_ball - y_player2) < 80)
  197.             {
  198.                 random_int = (rand() % 3) + 4;
  199.                 direction_of_ball = random_int;
  200.             }
  201.         }
  202.         else if (y_ball < y_player2)
  203.         {
  204.             if (abs(y_ball - y_player2) < 20)
  205.             {
  206.                 random_int = (rand() % 3) + 4;
  207.                 direction_of_ball = random_int;
  208.             }
  209.         }
  210.     }
  211.  
  212.     /*
  213.     1 = right
  214.     2 = up_right
  215.     3 = down_right
  216.     4 = left
  217.     5 = up_left
  218.     6 = down_left
  219.     */
  220.     //Check direction and execute command
  221.     if (direction_of_ball == 1)
  222.     {
  223.         x_ball += 5;
  224.     }
  225.     else if (direction_of_ball == 2)
  226.     {
  227.         x_ball += 5;
  228.         y_ball -= 5;
  229.     }
  230.     else if (direction_of_ball == 3)
  231.     {
  232.         x_ball += 5;
  233.         y_ball += 5;
  234.     }
  235.     else if (direction_of_ball == 4)
  236.     {
  237.         x_ball -= 5;
  238.     }
  239.     else if (direction_of_ball == 5)
  240.     {
  241.         x_ball -= 5;
  242.         y_ball -= 5;
  243.     }
  244.     else if (direction_of_ball == 6)
  245.     {
  246.         x_ball -= 5;
  247.         y_ball += 5;
  248.     }
  249.  
  250.     if (x_ball < 0)
  251.     {
  252.         reset();
  253.         direction_of_ball = 1;
  254.         player2_points++;
  255.     }
  256.     else if (x_ball > width - 20)
  257.     {
  258.         reset();
  259.         direction_of_ball = 4;
  260.         player1_points++;
  261.     }
  262.     else if (direction_of_ball == 2 && y_ball == 0)
  263.         direction_of_ball = 3;
  264.     else if (direction_of_ball == 3 && y_ball > (height - 20))
  265.         direction_of_ball = 2;
  266.     else if (direction_of_ball == 5 && y_ball == 0)
  267.         direction_of_ball = 6;
  268.     else if (direction_of_ball == 6 && y_ball > (height - 20))
  269.         direction_of_ball = 5;
  270. }
  271.  
  272. void reset()
  273. {
  274.     x_ball = width / 2;
  275.     y_ball = height / 2;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement