Advertisement
Felanpro

Ping Pong

Mar 17th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 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 = 20;
  20.  
  21. int ballX = 9;
  22. int ballY = 9;
  23.  
  24. int playerX = 1;
  25. int playerY = 9;
  26.  
  27. int player2X = 38;
  28. int player2Y = 9;
  29.  
  30. bool end_game = false;
  31.  
  32. int points_player_one = 0;
  33. int points_player_two = 0;
  34.  
  35. void draw()
  36. {
  37.     for (int y = 0; y < height; y++)
  38.     {
  39.         for (int x = 0; x < width; x++)
  40.         {
  41.             if (y == 0 || y == 19 || x == 0 || x == 39)
  42.                 cout << "#";
  43.             else if (y == ballY && x == ballX)
  44.                 cout << "O";
  45.             else if (y == playerY && x == playerX)
  46.                 cout << "X";
  47.             else if (y == player2Y && x == player2X)
  48.                 cout << "X";
  49.             else
  50.                 cout << " ";
  51.         }
  52.         cout << endl;
  53.     }
  54.     cout << setw(19) << points_player_one << " I " << points_player_two;
  55.     system("cls");
  56. }
  57.  
  58.  
  59. void move_character()
  60. {
  61.     if (_kbhit())
  62.     {
  63.         char input;
  64.  
  65.         input = _getch();
  66.  
  67.         if (input == 'w')
  68.             playerY--;
  69.         else if (input == 's')
  70.             playerY++;
  71.         else if (input == 'o')
  72.             player2Y--;
  73.         else if (input == 'l')
  74.             player2Y++;
  75.         else
  76.             ;
  77.     }
  78. }
  79. void logic()
  80. {
  81.     int random_num;
  82.     if (ballX == 38 && ballY == player2Y)
  83.     {
  84.         random_num = (rand() % 3) + 1;
  85.         if (random_num == 1)
  86.         {
  87.             direction = down_left;
  88.         }
  89.         else if (random_num == 2)
  90.         {
  91.             direction = up_left;
  92.         }
  93.         else if (random_num == 3)
  94.         {
  95.             direction = left_;
  96.         }
  97.     }
  98.  
  99.     if(ballX == 1 && ballY == playerY)
  100.     {
  101.         random_num = (rand() % 3) + 1;
  102.         if (random_num == 1)
  103.         {
  104.             direction = right_;
  105.         }
  106.         else if (random_num == 2)
  107.         {
  108.             direction = up_right;
  109.         }
  110.         else if (random_num == 3)
  111.         {
  112.             direction = down_right;
  113.         }
  114.     }
  115.    
  116.  
  117.     if (ballY == 1 && direction == up_left)
  118.     {
  119.         direction = down_left;
  120.     }
  121.     else if (ballY == 1 && direction == up_right)
  122.     {
  123.         direction = down_right;
  124.     }
  125.     else if (ballY == 18 && direction == down_left)
  126.     {
  127.         direction = up_left;
  128.     }
  129.     else if (ballY == 18 && direction == down_right)
  130.     {
  131.         direction = up_right;
  132.     }
  133.  
  134.     if (direction == left_)
  135.     {
  136.         ballX--;
  137.     }
  138.     else if (direction == up_left)
  139.     {
  140.         ballX--;
  141.         ballY--;
  142.     }
  143.     else if (direction == down_left)
  144.     {
  145.         ballX--;
  146.         ballY++;
  147.     }
  148.     else if (direction == right_)
  149.     {
  150.         ballX++;
  151.     }
  152.     else if (direction == up_right)
  153.     {
  154.         ballX++;
  155.         ballY--;
  156.     }
  157.     else if (direction == down_right)
  158.     {
  159.         ballX++;
  160.         ballY++;
  161.     }
  162.  
  163.     if (ballX < 0)
  164.     {
  165.         ballX = 17;
  166.         ballY = 9;
  167.         direction = right_;
  168.         points_player_two++;
  169.     }
  170.     else if (ballX > 40)
  171.     {
  172.         ballX = 17;
  173.         ballY = 9;
  174.         direction = left_;
  175.         points_player_one++;
  176.     }
  177. }
  178.  
  179. int main()
  180. {
  181.     srand(time(NULL)); //Generate random seed
  182.     direction = left_;
  183.  
  184.     while (!end_game)
  185.     {
  186.         draw();
  187.         move_character();
  188.         logic();
  189.     }
  190.  
  191.     return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement