Advertisement
Felanpro

Ping Pong V.2

Mar 17th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <ctime>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. int stop = 0;
  10. int down_left = 1;
  11. int up_left = 2;
  12. int left_ = 3;
  13. int right_ = 4;
  14. int up_right = 5;
  15. int down_right = 6;
  16. int direction; //This decides in what direction the ball is going to go
  17.  
  18. int width = 40;
  19. int height = 21;
  20.  
  21. int ballX = 9;
  22. int ballY = 9;
  23.  
  24. int playerX = 1;
  25. int playerY = 9;
  26. int playerX_ = 1;
  27. int playerY_ = 10;
  28. int playerX__ = 1;
  29. int playerY__ = 11;
  30.  
  31. int player2X = 38;
  32. int player2Y = 9;
  33. int player2X_ = 38;
  34. int player2Y_ = 10;
  35. int player2X__ = 38;
  36. int player2Y__ = 11;
  37.  
  38. bool end_game = false;
  39.  
  40. int points_player_one = 0;
  41. int points_player_two = 0;
  42.  
  43. void draw()
  44. {
  45.     for (int y = 0; y < height; y++)
  46.     {
  47.         for (int x = 0; x < width; x++)
  48.         {
  49.             if (y == 0 || y == 20 || x == 0 || x == 39)
  50.                 cout << "#";
  51.             else if (y == ballY && x == ballX)
  52.                 cout << "O";
  53.             else if (y == playerY && x == playerX)
  54.                 cout << "X";
  55.             else if (y == playerY_ && x == playerX_)
  56.                 cout << "X";
  57.             else if (y == playerY__ && x == playerX__)
  58.                 cout << "X";
  59.             else if (y == player2Y && x == player2X)
  60.                 cout << "X";
  61.             else if (y == player2Y_ && x == player2X)
  62.                 cout << "X";
  63.             else if (y == player2Y__ && x == player2X__)
  64.                 cout << "X";
  65.             else
  66.                 cout << " ";
  67.         }
  68.         cout << endl;
  69.     }
  70.     cout << setw(19) << points_player_one << " I " << points_player_two;
  71.     system("cls");
  72. }
  73.  
  74.  
  75. void move_character()
  76. {
  77.     if (_kbhit())
  78.     {
  79.         char input;
  80.  
  81.         input = _getch();
  82.  
  83.         if (input == 'w')
  84.         {
  85.             //Move first paddle
  86.             playerY -= 2;
  87.             playerY_ -= 2;
  88.             playerY__ -=2;
  89.         }
  90.         else if (input == 's')
  91.         {
  92.             playerY += 2;
  93.             playerY_ += 2;
  94.             playerY__ += 2;
  95.         }
  96.         else if (input == 'o')
  97.         {
  98.             player2Y -= 2;
  99.             player2Y_ -= 2;
  100.             player2Y__ -= 2;
  101.         }
  102.         else if (input == 'l')
  103.         {
  104.             player2Y += 2;
  105.             player2Y_ += 2;
  106.             player2Y__ += 2;
  107.         }
  108.         else
  109.             ;
  110.     }
  111. }
  112. void logic()
  113. {
  114.     int random_num;
  115.     if (ballX == 38 && ballY == player2Y)
  116.     {
  117.         random_num = (rand() % 3) + 1;
  118.         if (random_num == 1)
  119.         {
  120.             direction = down_left;
  121.         }
  122.         else if (random_num == 2)
  123.         {
  124.             direction = up_left;
  125.         }
  126.         else if (random_num == 3)
  127.         {
  128.             direction = left_;
  129.         }
  130.     }
  131.     if (ballX == 38 && ballY == player2Y_)
  132.     {
  133.         random_num = (rand() % 3) + 1;
  134.         if (random_num == 1)
  135.         {
  136.             direction = down_left;
  137.         }
  138.         else if (random_num == 2)
  139.         {
  140.             direction = up_left;
  141.         }
  142.         else if (random_num == 3)
  143.         {
  144.             direction = left_;
  145.         }
  146.     }
  147.     if (ballX == 38 && ballY == player2Y__)
  148.     {
  149.         random_num = (rand() % 3) + 1;
  150.         if (random_num == 1)
  151.         {
  152.             direction = down_left;
  153.         }
  154.         else if (random_num == 2)
  155.         {
  156.             direction = up_left;
  157.         }
  158.         else if (random_num == 3)
  159.         {
  160.             direction = left_;
  161.         }
  162.     }
  163.  
  164.     if (ballX == 1 && ballY == playerY)
  165.     {
  166.         random_num = (rand() % 3) + 1;
  167.         if (random_num == 1)
  168.         {
  169.             direction = right_;
  170.         }
  171.         else if (random_num == 2)
  172.         {
  173.             direction = up_right;
  174.         }
  175.         else if (random_num == 3)
  176.         {
  177.             direction = down_right;
  178.         }
  179.     }
  180.     else if (ballX == 1 && ballY == playerY_)
  181.     {
  182.         random_num = (rand() % 3) + 1;
  183.         if (random_num == 1)
  184.         {
  185.             direction = right_;
  186.         }
  187.         else if (random_num == 2)
  188.         {
  189.             direction = up_right;
  190.         }
  191.         else if (random_num == 3)
  192.         {
  193.             direction = down_right;
  194.         }
  195.     }
  196.     else if (ballX == 1 && ballY == playerY__)
  197.     {
  198.         random_num = (rand() % 3) + 1;
  199.         if (random_num == 1)
  200.         {
  201.             direction = right_;
  202.         }
  203.         else if (random_num == 2)
  204.         {
  205.             direction = up_right;
  206.         }
  207.         else if (random_num == 3)
  208.         {
  209.             direction = down_right;
  210.         }
  211.     }
  212.  
  213.  
  214.     if (ballY == 1 && direction == up_left)
  215.     {
  216.         direction = down_left;
  217.     }
  218.     else if (ballY == 1 && direction == up_right)
  219.     {
  220.         direction = down_right;
  221.     }
  222.     else if (ballY == 18 && direction == down_left)
  223.     {
  224.         direction = up_left;
  225.     }
  226.     else if (ballY == 18 && direction == down_right)
  227.     {
  228.         direction = up_right;
  229.     }
  230.  
  231.     if (direction == left_)
  232.     {
  233.         ballX--;
  234.     }
  235.     else if (direction == up_left)
  236.     {
  237.         ballX--;
  238.         ballY--;
  239.     }
  240.     else if (direction == down_left)
  241.     {
  242.         ballX--;
  243.         ballY++;
  244.     }
  245.     else if (direction == right_)
  246.     {
  247.         ballX++;
  248.     }
  249.     else if (direction == up_right)
  250.     {
  251.         ballX++;
  252.         ballY--;
  253.     }
  254.     else if (direction == down_right)
  255.     {
  256.         ballX++;
  257.         ballY++;
  258.     }
  259.  
  260.     if (ballX < 0)
  261.     {
  262.         ballX = 17;
  263.         ballY = 9;
  264.         direction = right_;
  265.         points_player_two++;
  266.     }
  267.     else if (ballX > 41)
  268.     {
  269.         ballX = 17;
  270.         ballY = 9;
  271.         direction = left_;
  272.         points_player_one++;
  273.     }
  274. }
  275.  
  276. int main()
  277. {
  278.     srand(time(NULL)); //Generate random seed
  279.     direction = left_;
  280.  
  281.     while (!end_game)
  282.     {
  283.         draw();
  284.         move_character();
  285.         logic();
  286.     }
  287.  
  288.     return 0;
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement