Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <SFML/Window.hpp>
- #include <SFML/Graphics.hpp>
- #include <SFML/Audio.hpp>
- #include <string>
- using namespace std;
- void logic();
- void reset();
- int width = 800;
- int height = 600;
- //Player on coordinates
- int x_player1 = 5;
- int y_player1 = (height/2) - 80;
- int direction_of_player_1;
- int player1_points = 0;
- //Player two coordinates
- int x_player2 = width - 25;
- int y_player2 = (height/2) - 80;
- int direction_of_player_2;
- int player2_points = 0;
- //Ball coordinates
- int x_ball = (width / 2);
- int y_ball = (height / 2);
- int direction_of_ball;
- sf::RectangleShape ball;
- int random_int; //This is essential
- int main()
- {
- srand(time(NULL));
- //Initialize ball direction in the beginning
- direction_of_ball = 4;
- sf::RenderWindow window(sf::VideoMode(width, height), "Ping Pong");
- window.setFramerateLimit(70);
- window.setVerticalSyncEnabled(true);
- //Player one rectangle
- sf::RectangleShape player_one;
- player_one.setFillColor(sf::Color::Black);
- player_one.setSize(sf::Vector2f(20, 80));
- //Player two rectangle
- sf::RectangleShape player_two;
- player_two.setFillColor(sf::Color::Black);
- player_two.setSize(sf::Vector2f(20, 80));
- //The ball
- ball.setFillColor(sf::Color::Red);
- ball.setSize(sf::Vector2f(20, 20));
- //Some 8 bit music
- sf::Music music;
- music.openFromFile("8bit_music.ogg");
- music.setVolume(50);
- music.setLoop(true); //Keep on going forever
- music.play();
- //Some iconic text
- sf::Font black_ops_font;
- black_ops_font.loadFromFile("Black Ops 2 Font.ttf");
- sf::Text points_display_player1;
- points_display_player1.setFont(black_ops_font);
- points_display_player1.setFillColor(sf::Color::Black);
- points_display_player1.setPosition((width/2) - 70, height - 50);
- string points_string_player1;
- sf::Text points_display_player2;
- points_display_player2.setFont(black_ops_font);
- points_display_player2.setFillColor(sf::Color::Black);
- points_display_player2.setPosition((width / 2) + 30, height - 50);
- string points_string_player2;
- sf::Event event;
- while (window.isOpen())
- {
- window.clear(sf::Color::White);
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- if (event.type == sf::Event::KeyPressed)
- {
- if (event.key.code == sf::Keyboard::W)
- {
- direction_of_player_1 = 1;
- }
- else if (event.key.code == sf::Keyboard::S)
- {
- direction_of_player_1 = 2;
- }
- else if (event.key.code == sf::Keyboard::Up)
- {
- direction_of_player_2 = 1;
- }
- else if (event.key.code == sf::Keyboard::Down)
- {
- direction_of_player_2 = 2;
- }
- }
- }
- points_string_player1 = to_string(player1_points);
- points_string_player2 = to_string(player2_points);
- points_display_player1.setString(points_string_player1);
- points_display_player2.setString(points_string_player2);
- logic();
- player_one.setPosition(x_player1, y_player1);
- player_two.setPosition(x_player2, y_player2);
- ball.setPosition(x_ball, y_ball);
- window.draw(player_one);
- window.draw(player_two);
- window.draw(ball);
- window.draw(points_display_player1);
- window.draw(points_display_player2);
- window.display();
- }
- int pause; cin >> pause; //Pause the program
- return 0;
- }
- void logic()
- {
- //Change position of player 1
- if (direction_of_player_1 == 1)
- {
- if (y_player1 == 0)
- ;
- else
- y_player1 -= 5;
- }
- else if (direction_of_player_1 == 2)
- {
- if ((y_player1 + 80) > height)
- ;
- else
- y_player1 += 5;
- }
- //Change position of player 2
- if (direction_of_player_2 == 1)
- {
- if (y_player2 == 0)
- ;
- else
- y_player2 -= 5;
- }
- else if (direction_of_player_2 == 2)
- {
- if ((y_player2 + 80) > height)
- ;
- else
- y_player2 += 5;
- }
- //Change position of the ball
- //In case it touches player 1
- if (abs(x_ball - x_player1) <= 20 )
- {
- if (y_ball > y_player1)
- {
- if (abs(y_ball - y_player1) < 80)
- {
- random_int = (rand() % 3) + 1;
- direction_of_ball = random_int;
- }
- }
- else if (y_ball < y_player1)
- {
- if (abs(y_ball - y_player1) < 20)
- {
- random_int = (rand() % 3) + 1;
- direction_of_ball = random_int;
- }
- }
- }
- //In case it touches player 2
- if (abs(x_ball - x_player2) <= 20)
- {
- if (y_ball > y_player2)
- {
- if (abs(y_ball - y_player2) < 80)
- {
- random_int = (rand() % 3) + 4;
- direction_of_ball = random_int;
- }
- }
- else if (y_ball < y_player2)
- {
- if (abs(y_ball - y_player2) < 20)
- {
- random_int = (rand() % 3) + 4;
- direction_of_ball = random_int;
- }
- }
- }
- /*
- 1 = right
- 2 = up_right
- 3 = down_right
- 4 = left
- 5 = up_left
- 6 = down_left
- */
- //Check direction and execute command
- if (direction_of_ball == 1)
- {
- x_ball += 5;
- }
- else if (direction_of_ball == 2)
- {
- x_ball += 5;
- y_ball -= 5;
- }
- else if (direction_of_ball == 3)
- {
- x_ball += 5;
- y_ball += 5;
- }
- else if (direction_of_ball == 4)
- {
- x_ball -= 5;
- }
- else if (direction_of_ball == 5)
- {
- x_ball -= 5;
- y_ball -= 5;
- }
- else if (direction_of_ball == 6)
- {
- x_ball -= 5;
- y_ball += 5;
- }
- if (x_ball < 0)
- {
- reset();
- direction_of_ball = 1;
- player2_points++;
- }
- else if (x_ball > width - 20)
- {
- reset();
- direction_of_ball = 4;
- player1_points++;
- }
- else if (direction_of_ball == 2 && y_ball == 0)
- direction_of_ball = 3;
- else if (direction_of_ball == 3 && y_ball > (height - 20))
- direction_of_ball = 2;
- else if (direction_of_ball == 5 && y_ball == 0)
- direction_of_ball = 6;
- else if (direction_of_ball == 6 && y_ball > (height - 20))
- direction_of_ball = 5;
- }
- void reset()
- {
- x_ball = width / 2;
- y_ball = height / 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement