Advertisement
TermSpar

Pong C++

Jun 30th, 2016
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.06 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <SFML\Graphics.hpp>
  4.  
  5. class Ball{
  6. public:
  7.     Ball(){
  8.        
  9.     }
  10.  
  11.     Ball(sf::Vector2f size){
  12.         rect.setSize(size);
  13.     }
  14.  
  15.     void getColl(){
  16.         bottom = rect.getPosition().y + rect.getSize().y;
  17.         left = rect.getPosition().x;
  18.         right = rect.getPosition().x + rect.getSize().x;
  19.         top = rect.getPosition().y;
  20.     }
  21.  
  22.     void setPos(sf::Vector2f newPos){
  23.         rect.setPosition(newPos);
  24.     }
  25.  
  26.     void draw(sf::RenderWindow &window){
  27.         window.draw(rect);
  28.     }
  29.  
  30.     void moveRight(float moveSpeed, float rndHeight){
  31.         rect.move(moveSpeed, -rndHeight);
  32.     }
  33.  
  34.     void moveLeft(float moveSpeed, float rndHeight){
  35.         rect.move(-moveSpeed, rndHeight);
  36.     }
  37.  
  38.     float getRight(){
  39.         return right;
  40.     }
  41.  
  42.     float getLeft(){
  43.         return left;
  44.     }
  45.  
  46.     float getTop(){
  47.         return top;
  48.     }
  49.  
  50.     float getBottom(){
  51.         return bottom;
  52.     }
  53. protected:
  54.     sf::RectangleShape rect;
  55.     float bottom;
  56.     float top;
  57.     float right;
  58.     float left;
  59. };
  60.  
  61. ///////////////////////////////////////////////////////////////
  62.  
  63. #pragma once
  64.  
  65. #include <SFML\Graphics.hpp>
  66. #include <iostream>
  67. #include "Ball.h"
  68.  
  69. class Paddle : public Ball{
  70. public:
  71.     Paddle(){
  72.         //Default Constructor
  73.     }
  74.  
  75.     Paddle(sf::Vector2f padSize){
  76.         rect.setSize(padSize);
  77.     }
  78.  
  79.     void drawPaddle(sf::RenderWindow &window){
  80.         window.draw(rect);
  81.     }
  82.  
  83.     void setPos(sf::Vector2f newPos){
  84.         rect.setPosition(newPos);
  85.     }
  86.  
  87.     void movePaddle(char direction, double moveSpeed){
  88.         if(direction == 'u'){
  89.             rect.move(0, -moveSpeed);
  90.         }else if(direction == 'd'){
  91.             rect.move(0, moveSpeed);
  92.         }else if(direction == 'r'){
  93.             rect.move(moveSpeed, 0);
  94.         }else if(direction == 'l'){
  95.             rect.move(-moveSpeed, 0);
  96.         }
  97.     }
  98.  
  99.     void getColl(){
  100.         bottom = rect.getPosition().y + rect.getSize().y;
  101.         left = rect.getPosition().x;
  102.         right = rect.getPosition().x + rect.getSize().x;
  103.         top = rect.getPosition().y;
  104.     }
  105.  
  106.     bool checkColl(Ball ball){
  107.         if(this->right < ball.getLeft() || this->left > ball.getRight()
  108.         || this->top > ball.getBottom() || this->bottom < ball.getTop()){
  109.             return false;
  110.         }
  111.         return true;
  112.     }
  113.  
  114. protected:
  115.     sf::RectangleShape rect;
  116.     float bottom;
  117.     float top;
  118.     float right;
  119.     float left;
  120. };
  121.  
  122. ///////////////////////////////////////////////////////////////
  123.  
  124. #include <iostream>
  125. #include <SFML/Graphics.hpp>
  126. #include "Paddle.h"
  127. #include "Ball.h"
  128. #include <ctime>
  129. #include <cstdlib>
  130. #include <sstream>
  131.  
  132. int main(){
  133.     sf::RenderWindow window;
  134.    
  135.     sf::Vector2i centerWindow((sf::VideoMode::getDesktopMode().width/2)-755, (sf::VideoMode::getDesktopMode().height/2)-390);
  136.  
  137.     window.create(sf::VideoMode(1500, 700), "Pong by Ben Bollinger", sf::Style::Titlebar | sf::Style::Close);
  138.     window.setPosition(centerWindow);
  139.  
  140.     window.setKeyRepeatEnabled(true);
  141.  
  142.     //Create Game Objects:
  143.     Paddle pd1(sf::Vector2f(8, 120));
  144.     Paddle pd2(sf::Vector2f(8, 120));
  145.     Ball ball1(sf::Vector2f(20, 20));
  146.  
  147.     Paddle boundBottom(sf::Vector2f(1500, 10));
  148.     Paddle boundTop(sf::Vector2f(1500, 10));
  149.     Paddle boundLeft(sf::Vector2f(10, 700));
  150.     Paddle boundRight(sf::Vector2f(10, 700));
  151.  
  152.     //Score Settings:
  153.     int pScore1 = 0;
  154.     int pScore2 = 0;
  155.     sf::Font font;
  156.     font.loadFromFile("arial.ttf");
  157.  
  158.     sf::Text score1;
  159.     sf::Text score2;
  160.  
  161.     score1.setFont(font);
  162.     score2.setFont(font);
  163.  
  164.     score1.setCharacterSize(21);
  165.     score2.setCharacterSize(21);
  166.  
  167.     std::ostringstream sScore1;
  168.     std::ostringstream sScore2;
  169.     sScore1 << "Player1's Score: " << pScore1;
  170.     sScore2 << "Player2's Score: " << pScore2;
  171.  
  172.     score1.setString(sScore1.str());
  173.     score2.setString(sScore2.str());
  174.     score1.setColor(sf::Color::White);
  175.  
  176.     //Relocate Game Objects:
  177.     pd1.setPos(sf::Vector2f(20, 100));
  178.     ball1.setPos(sf::Vector2f(700, 350));
  179.     pd2.setPos(sf::Vector2f(1472, 100));
  180.  
  181.     boundBottom.setPos(sf::Vector2f(0, 690));
  182.     boundTop.setPos(sf::Vector2f(0, 0));
  183.     boundLeft.setPos(sf::Vector2f(0, 0));
  184.     boundRight.setPos(sf::Vector2f(1490, 0));
  185.  
  186.     score1.setPosition(sf::Vector2f(20, 7));
  187.     score2.setPosition(sf::Vector2f(1255, 7));
  188.    
  189.     float ballSpeed = 0.14;
  190.     int paddleSpeed = 16;
  191.  
  192.     //Main Loop:
  193.     while(window.isOpen()){
  194.        
  195.         sf::Event Event;
  196.  
  197.         //Event Loop:
  198.         while(window.pollEvent(Event)){
  199.             switch(Event.type){
  200.  
  201.             case sf::Event::Closed:
  202.                 window.close();
  203.             }
  204.  
  205.             //Player2 Movement
  206.             if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
  207.                 pd2.movePaddle('u', paddleSpeed);
  208.                 pd2.drawPaddle(window);
  209.             }else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
  210.                 pd2.movePaddle('d', paddleSpeed);
  211.                 pd2.drawPaddle(window);
  212.             }
  213.  
  214.             //Player1 Movement:
  215.             if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
  216.                 pd1.movePaddle('u', paddleSpeed);
  217.                 pd1.drawPaddle(window);
  218.             }else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
  219.                 pd1.movePaddle('d', paddleSpeed);
  220.                 pd1.drawPaddle(window);
  221.             }
  222.         }
  223.         window.clear(); //Update window
  224.  
  225.         pd1.getColl(); //Get collision info
  226.         pd2.getColl(); //Get collision info
  227.         ball1.getColl(); //Get collision info
  228.  
  229.         static bool goLeft = true;
  230.  
  231.         srand(static_cast<unsigned int>(time(0))); //Seed random number with current time.
  232.         int rndNum1 = rand() % 3 + 1;
  233.         float upSpeed;
  234.  
  235.         //Random number handler:
  236.         if(rndNum1 == 1){
  237.             upSpeed = 0.01;
  238.         }else if(rndNum1 == 2){
  239.             upSpeed = 0.02;
  240.         }else{
  241.             upSpeed = -0.01;
  242.         }
  243.  
  244.         /* If the ball is not colliding with paddle1 and it's going
  245.         left, then move it left. But if it is colliding with paddle1 stop
  246.         going left*/
  247.         if(!pd1.checkColl(ball1) && goLeft == true){
  248.             ball1.moveLeft(ballSpeed, upSpeed);
  249.         }else if(pd1.checkColl(ball1)){
  250.             goLeft = false;
  251.         }
  252.  
  253.         srand(static_cast<unsigned int>(time(0))); //Seed random number with current time.
  254.         int rndNum2 = rand() % 3 + 1;
  255.         float upSpeed2;
  256.  
  257.         //Random number handler:
  258.         if(rndNum2 == 1){
  259.             upSpeed2 = 0.01;
  260.         }else if(rndNum2 == 2){
  261.             upSpeed2 = 0.02;
  262.         }else{
  263.             upSpeed2 = -0.01;
  264.         }
  265.  
  266.         /* If the ball is not colliding with paddle2 and it's not going
  267.         left, then move it right. But if it is colliding with paddle2 stop
  268.         going right*/
  269.         if(!pd2.checkColl(ball1) && goLeft == false){
  270.             ball1.moveRight(ballSpeed, upSpeed2);
  271.         }else if(pd2.checkColl(ball1)){
  272.             goLeft = true;
  273.         }
  274.  
  275.         //Wall Collisions:
  276.         boundLeft.getColl();
  277.         if(boundLeft.checkColl(ball1)){
  278.             //Reset ball location:
  279.             ball1.setPos(sf::Vector2f(700, 350));
  280.  
  281.             //Increment score and redraw:
  282.             pScore2++;
  283.             sScore2.str("");
  284.             sScore2 << "Player2's Score: " << pScore2;
  285.             score2.setString(sScore2.str());
  286.         }
  287.         boundRight.getColl();
  288.         if(boundRight.checkColl(ball1)){
  289.             //Reset ball location:
  290.             ball1.setPos(sf::Vector2f(700, 350));
  291.  
  292.             //Increment score and redraw:
  293.             pScore1++;
  294.             sScore1.str("");
  295.             sScore1 << "Player1's Score: " << pScore1;
  296.             score1.setString(sScore1.str());
  297.         }
  298.  
  299.         ball1.getColl();
  300.  
  301.         //Draw Bounds:
  302.         boundBottom.drawPaddle(window);
  303.         boundTop.drawPaddle(window);
  304.         boundLeft.drawPaddle(window);
  305.         boundRight.drawPaddle(window);
  306.  
  307.         //Draw Scores:
  308.         window.draw(score1);
  309.         window.draw(score2);
  310.  
  311.         pd2.drawPaddle(window); //Draw Player2
  312.         ball1.draw(window); //Draw Ball
  313.         pd1.drawPaddle(window); //Draw Player1
  314.         window.display(); //Display Window
  315.     }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement