Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <conio.h>
- #include <ctime>
- #include <iomanip>
- using namespace std;
- int stop = 0;
- int down_left = 1;
- int up_left = 2;
- int left_ = 3;
- int right_ = 4;
- int up_right = 5;
- int down_right = 6;
- int direction; //This decides in what direction the ball is going to go
- int width = 40;
- int height = 21;
- int ballX = 9;
- int ballY = 9;
- int playerX = 1;
- int playerY = 9;
- int playerX_ = 1;
- int playerY_ = 10;
- int playerX__ = 1;
- int playerY__ = 11;
- int player2X = 38;
- int player2Y = 9;
- int player2X_ = 38;
- int player2Y_ = 10;
- int player2X__ = 38;
- int player2Y__ = 11;
- bool end_game = false;
- int points_player_one = 0;
- int points_player_two = 0;
- void draw()
- {
- for (int y = 0; y < height; y++)
- {
- for (int x = 0; x < width; x++)
- {
- if (y == 0 || y == 20 || x == 0 || x == 39)
- cout << "#";
- else if (y == ballY && x == ballX)
- cout << "O";
- else if (y == playerY && x == playerX)
- cout << "X";
- else if (y == playerY_ && x == playerX_)
- cout << "X";
- else if (y == playerY__ && x == playerX__)
- cout << "X";
- else if (y == player2Y && x == player2X)
- cout << "X";
- else if (y == player2Y_ && x == player2X)
- cout << "X";
- else if (y == player2Y__ && x == player2X__)
- cout << "X";
- else
- cout << " ";
- }
- cout << endl;
- }
- cout << setw(19) << points_player_one << " I " << points_player_two;
- system("cls");
- }
- void move_character()
- {
- if (_kbhit())
- {
- char input;
- input = _getch();
- if (input == 'w')
- {
- //Move first paddle
- playerY -= 2;
- playerY_ -= 2;
- playerY__ -=2;
- }
- else if (input == 's')
- {
- playerY += 2;
- playerY_ += 2;
- playerY__ += 2;
- }
- else if (input == 'o')
- {
- player2Y -= 2;
- player2Y_ -= 2;
- player2Y__ -= 2;
- }
- else if (input == 'l')
- {
- player2Y += 2;
- player2Y_ += 2;
- player2Y__ += 2;
- }
- else
- ;
- }
- }
- void logic()
- {
- int random_num;
- if (ballX == 38 && ballY == player2Y)
- {
- random_num = (rand() % 3) + 1;
- if (random_num == 1)
- {
- direction = down_left;
- }
- else if (random_num == 2)
- {
- direction = up_left;
- }
- else if (random_num == 3)
- {
- direction = left_;
- }
- }
- if (ballX == 38 && ballY == player2Y_)
- {
- random_num = (rand() % 3) + 1;
- if (random_num == 1)
- {
- direction = down_left;
- }
- else if (random_num == 2)
- {
- direction = up_left;
- }
- else if (random_num == 3)
- {
- direction = left_;
- }
- }
- if (ballX == 38 && ballY == player2Y__)
- {
- random_num = (rand() % 3) + 1;
- if (random_num == 1)
- {
- direction = down_left;
- }
- else if (random_num == 2)
- {
- direction = up_left;
- }
- else if (random_num == 3)
- {
- direction = left_;
- }
- }
- if (ballX == 1 && ballY == playerY)
- {
- random_num = (rand() % 3) + 1;
- if (random_num == 1)
- {
- direction = right_;
- }
- else if (random_num == 2)
- {
- direction = up_right;
- }
- else if (random_num == 3)
- {
- direction = down_right;
- }
- }
- else if (ballX == 1 && ballY == playerY_)
- {
- random_num = (rand() % 3) + 1;
- if (random_num == 1)
- {
- direction = right_;
- }
- else if (random_num == 2)
- {
- direction = up_right;
- }
- else if (random_num == 3)
- {
- direction = down_right;
- }
- }
- else if (ballX == 1 && ballY == playerY__)
- {
- random_num = (rand() % 3) + 1;
- if (random_num == 1)
- {
- direction = right_;
- }
- else if (random_num == 2)
- {
- direction = up_right;
- }
- else if (random_num == 3)
- {
- direction = down_right;
- }
- }
- if (ballY == 1 && direction == up_left)
- {
- direction = down_left;
- }
- else if (ballY == 1 && direction == up_right)
- {
- direction = down_right;
- }
- else if (ballY == 18 && direction == down_left)
- {
- direction = up_left;
- }
- else if (ballY == 18 && direction == down_right)
- {
- direction = up_right;
- }
- if (direction == left_)
- {
- ballX--;
- }
- else if (direction == up_left)
- {
- ballX--;
- ballY--;
- }
- else if (direction == down_left)
- {
- ballX--;
- ballY++;
- }
- else if (direction == right_)
- {
- ballX++;
- }
- else if (direction == up_right)
- {
- ballX++;
- ballY--;
- }
- else if (direction == down_right)
- {
- ballX++;
- ballY++;
- }
- if (ballX < 0)
- {
- ballX = 17;
- ballY = 9;
- direction = right_;
- points_player_two++;
- }
- else if (ballX > 41)
- {
- ballX = 17;
- ballY = 9;
- direction = left_;
- points_player_one++;
- }
- }
- int main()
- {
- srand(time(NULL)); //Generate random seed
- direction = left_;
- while (!end_game)
- {
- draw();
- move_character();
- logic();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement