Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include"windows.h"
- #include <chrono>
- #include <thread>
- #include <conio.h>
- using namespace std;
- const int minX{ 1 };
- const int minY{ 1 };
- const int maxX{ 79 };
- const int maxY{ 24 };
- void gotoxy(int xpos, int ypos)
- {
- COORD scrn;
- HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
- scrn.X = xpos; scrn.Y = ypos;
- SetConsoleCursorPosition(hOuput, scrn);
- }
- void changePos(int** massive, int maxX, int maxY,int currentX,int currentY,int dx,int dy)
- {
- swap(massive[currentY][currentX], massive[currentY - dy][currentX - dx]);
- gotoxy(currentX - dx, currentY - dy);
- cout << " ";
- gotoxy(currentX, currentY);
- }
- void printPole()
- {
- for (int i = 0; i <= maxX; i++)
- {
- gotoxy(i, 0);
- cout << "=";
- gotoxy(i, maxY);
- cout << "=";
- }
- for (int i = 0; i <= maxY; i++)
- {
- gotoxy(0, i);
- cout << "|";
- gotoxy(maxX, i);
- cout << "|";
- }
- }
- void game()
- {
- int positionBoardPlayer2{ maxX / 2 - 4 };
- int positionBoardPlayer1{ maxX / 2 - 4 };
- int dx = { 1 }, dy = { 1 };
- int currentX = 1;
- int currentY = 1;
- int score = 0;
- string board = "------------";
- int lenghtBoard = 12;
- printPole();
- gotoxy(positionBoardPlayer1, maxY-1);
- cout << board;
- gotoxy(positionBoardPlayer2, 1);
- cout << board;
- gotoxy(currentX, currentY);
- int** massive = new int* [maxY-1];
- int* bigArr(new int[maxX * maxY]);
- for (int i = 0; i <= maxY; i++)
- {
- massive[i] = bigArr + maxX * i;
- }
- massive[currentY][currentX] = 0;
- while (score != 5)
- {
- if (currentY == maxY-1 or currentY < minY)
- {
- dy = dy * (-1); currentY += dy;
- swap(massive[currentY][currentX], massive[currentY - dy][currentX]);
- } // check vertical limits
- if (currentX == maxX-1 or currentX < minX) {
- dx = dx * (-1); currentX += dx;
- swap(massive[currentY][currentX], massive[currentY][currentX - dx]);
- } // check horizontal limits
- gotoxy(currentX, currentY);
- cout << massive[currentY][currentX];
- currentX += dx;
- currentY += dy;
- Sleep(120);
- changePos(massive, maxX, maxY, currentX, currentY,dx,dy);
- if ((currentX > positionBoardPlayer1 and currentX < positionBoardPlayer1 + lenghtBoard) and currentY == maxY-1)
- {
- printf("BYE!");
- printf("X = %2d, Y=%2d", currentX, currentY);
- }
- if ((currentX > positionBoardPlayer2 and currentX < positionBoardPlayer2 + lenghtBoard) and currentY == minY)
- {
- printf("BYE!");
- printf("X = %2d, Y=%2d", currentX, currentY);
- }
- if (_kbhit())
- {
- auto key{ _getch() };
- if (key == 'a' or key == 'A') { if (positionBoardPlayer1 > minX + 1) { gotoxy(positionBoardPlayer1 + lenghtBoard, maxY - 1); cout << " "; positionBoardPlayer1--; gotoxy(positionBoardPlayer1, maxY - 1); cout << board; };
- if (key == 'd' or key == 'D') { if (positionBoardPlayer1 < maxX - 1) { gotoxy(positionBoardPlayer1, maxY - 1); cout << " "; positionBoardPlayer1++; gotoxy(positionBoardPlayer1, maxY - 1); cout << board; }; };
- if (key == 'q' or key == 'Q') { if (positionBoardPlayer2 > minX + 1) { gotoxy(positionBoardPlayer2 + lenghtBoard, minY); cout << " "; positionBoardPlayer2--; gotoxy(positionBoardPlayer2, minY); cout << board;; }; };
- if (key == 'e' or key == 'E') { if (positionBoardPlayer2 < maxX - 1) { gotoxy(positionBoardPlayer2, minY); cout << " "; positionBoardPlayer2++; gotoxy(positionBoardPlayer2, minY); cout << board; };
- }; };
- }
- }
- }
- void main()
- {
- game();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement