Advertisement
Andrey_ZoZ

PinPong

May 6th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include <iostream>
  2. #include"windows.h"
  3. #include <chrono>
  4. #include <thread>
  5. #include <conio.h>
  6. using namespace std;
  7. const int minX{ 1 };
  8. const int minY{ 1 };
  9. const int maxX{ 79 };
  10. const int maxY{ 24 };
  11. void gotoxy(int xpos, int ypos)
  12. {
  13.     COORD scrn;
  14.     HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
  15.     scrn.X = xpos; scrn.Y = ypos;
  16.     SetConsoleCursorPosition(hOuput, scrn);
  17. }
  18. void changePos(int** massive, int maxX, int maxY,int currentX,int currentY,int dx,int dy)
  19. {
  20.     swap(massive[currentY][currentX], massive[currentY - dy][currentX - dx]);
  21.     gotoxy(currentX - dx, currentY - dy);
  22.     cout << " ";
  23.     gotoxy(currentX, currentY);
  24. }
  25. void printPole()
  26. {
  27.     for (int i = 0; i <= maxX; i++)
  28.     {
  29.         gotoxy(i, 0);
  30.         cout << "=";
  31.         gotoxy(i, maxY);
  32.         cout << "=";
  33.     }
  34.     for (int i = 0; i <= maxY; i++)
  35.     {
  36.         gotoxy(0, i);
  37.         cout << "|";
  38.         gotoxy(maxX, i);
  39.         cout << "|";
  40.        
  41.     }
  42. }
  43. void  game()
  44. {
  45.     int positionBoardPlayer2{ maxX / 2 - 4 };
  46.     int positionBoardPlayer1{ maxX / 2 - 4 };
  47.     int dx = { 1 }, dy = { 1 };
  48.     int currentX = 1;
  49.     int currentY = 1;
  50.     int score = 0;
  51.     string board = "------------";
  52.     int lenghtBoard = 12;
  53.     printPole();
  54.     gotoxy(positionBoardPlayer1, maxY-1);
  55.     cout << board;
  56.     gotoxy(positionBoardPlayer2, 1);
  57.     cout << board;
  58.     gotoxy(currentX, currentY);
  59.     int** massive = new int* [maxY-1];
  60.     int* bigArr(new int[maxX * maxY]);
  61.     for (int i = 0; i <= maxY; i++)
  62.     {
  63.         massive[i] = bigArr + maxX * i;
  64.     }
  65.     massive[currentY][currentX] = 0;
  66.    
  67.     while (score != 5)
  68.     {
  69.  
  70.         if (currentY == maxY-1 or currentY < minY)
  71.         {
  72.         dy = dy * (-1); currentY += dy;
  73.         swap(massive[currentY][currentX], massive[currentY - dy][currentX]);
  74.         } // check vertical limits
  75.         if (currentX == maxX-1 or currentX < minX) {
  76.         dx = dx * (-1); currentX += dx;
  77.         swap(massive[currentY][currentX], massive[currentY][currentX - dx]);
  78.         } // check horizontal limits
  79.        
  80.         gotoxy(currentX, currentY);
  81.         cout << massive[currentY][currentX];
  82.         currentX += dx;
  83.         currentY += dy;
  84.         Sleep(120);
  85.         changePos(massive, maxX, maxY, currentX, currentY,dx,dy);
  86.         if ((currentX > positionBoardPlayer1 and currentX < positionBoardPlayer1 + lenghtBoard) and currentY == maxY-1)
  87.         {
  88.             printf("BYE!");
  89.             printf("X = %2d, Y=%2d", currentX, currentY);
  90.         }
  91.         if ((currentX > positionBoardPlayer2 and currentX < positionBoardPlayer2 + lenghtBoard) and currentY == minY)
  92.         {
  93.             printf("BYE!");
  94.             printf("X = %2d, Y=%2d", currentX, currentY);
  95.         }
  96.         if (_kbhit())
  97.         {
  98.             auto key{ _getch() };
  99.             if (key == 'a' or key == 'A') { if (positionBoardPlayer1 > minX + 1) { gotoxy(positionBoardPlayer1 + lenghtBoard, maxY - 1); cout << " "; positionBoardPlayer1--; gotoxy(positionBoardPlayer1, maxY - 1); cout << board; };
  100.             if (key == 'd' or key == 'D') { if (positionBoardPlayer1 < maxX - 1) { gotoxy(positionBoardPlayer1, maxY - 1); cout << " "; positionBoardPlayer1++; gotoxy(positionBoardPlayer1, maxY - 1); cout << board; }; };
  101.             if (key == 'q' or key == 'Q') { if (positionBoardPlayer2 > minX + 1) { gotoxy(positionBoardPlayer2 + lenghtBoard, minY); cout << " "; positionBoardPlayer2--; gotoxy(positionBoardPlayer2, minY); cout << board;; }; };
  102.             if (key == 'e' or key == 'E') { if (positionBoardPlayer2 < maxX - 1) { gotoxy(positionBoardPlayer2, minY); cout << " "; positionBoardPlayer2++; gotoxy(positionBoardPlayer2, minY); cout << board; };
  103.             }; };
  104.         }
  105.  
  106.        
  107.  
  108.     }
  109. }
  110. void main()
  111. {
  112.     game();
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement