Advertisement
TermSpar

Pong SpeedCoded

Jul 12th, 2016
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include "Ball.h"
  4. #include "Paddle.h"
  5.  
  6. int main() {
  7.     sf::RenderWindow window;
  8.  
  9.     sf::Vector2i centerWindow((sf::VideoMode::getDesktopMode().width / 2) - 445, (sf::VideoMode::getDesktopMode().height / 2) - 480);
  10.  
  11.     window.create(sf::VideoMode(900, 900), "SFML Project", sf::Style::Titlebar | sf::Style::Close);
  12.     window.setPosition(centerWindow);
  13.  
  14.     window.setKeyRepeatEnabled(true);
  15.  
  16.     //Paddle Objects:
  17.     Paddle paddle1({ 10, 100 });
  18.     Paddle paddle2({ 10, 100 });
  19.     paddle1.setPos({ 50, 200 });
  20.     paddle2.setPos({ 850, 200 });
  21.  
  22.     //Ball Object:
  23.     Ball ball({ 20, 20 });
  24.     ball.setPos({ 400, 200 });
  25.  
  26.     //Main Loop:
  27.     while (window.isOpen()) {
  28.  
  29.         sf::Event Event;
  30.  
  31.         const float moveSpeed = 0.2;
  32.  
  33.         //Player1 Movement:
  34.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
  35.             paddle1.move({ 0, -moveSpeed });
  36.         }
  37.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
  38.             paddle1.move({ 0, moveSpeed });
  39.         }
  40.  
  41.         //Player2 Movement:
  42.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
  43.             paddle2.move({ 0, -moveSpeed });
  44.         }
  45.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
  46.             paddle2.move({ 0, moveSpeed });
  47.         }
  48.  
  49.         //Event Loop:
  50.         while (window.pollEvent(Event)) {
  51.             switch (Event.type) {
  52.  
  53.             case sf::Event::Closed:
  54.                 window.close();
  55.             }
  56.  
  57.         }
  58.  
  59.         static bool goingLeft = true;
  60.         static bool goingRight = false;
  61.  
  62.         if (goingLeft == true && paddle1.isCollidingWith(ball) == false) {
  63.             ball.move({ -moveSpeed, 0 });
  64.         }
  65.         else if (goingLeft == true && paddle1.isCollidingWith(ball) == true) {
  66.             goingRight = true;
  67.             goingLeft = false;
  68.         }
  69.  
  70.         if (goingRight == true && paddle2.isCollidingWith(ball) == false) {
  71.             ball.move({ moveSpeed, 0 });
  72.         }
  73.         else if (goingRight == true && paddle2.isCollidingWith(ball) == true) {
  74.             goingRight = false;
  75.             goingLeft = true;
  76.         }
  77.  
  78.         window.clear();
  79.         paddle1.drawTo(window);
  80.         paddle2.drawTo(window);
  81.         ball.drawTo(window);
  82.         window.display();
  83.     }
  84. }
  85.  
  86.  
  87. #pragma once
  88.  
  89. #include <SFML\Graphics.hpp>
  90. #include "Ball.h"
  91.  
  92. class Paddle {
  93. public:
  94.     Paddle(sf::Vector2f size) {
  95.         paddle.setSize(size);
  96.     }
  97.  
  98.     void drawTo(sf::RenderWindow &window) {
  99.         window.draw(paddle);
  100.     }
  101.  
  102.     void move(sf::Vector2f distance) {
  103.         paddle.move(distance);
  104.     }
  105.  
  106.     bool isCollidingWith(Ball ball) {
  107.         if (ball.getGlobalBounds().intersects(paddle.getGlobalBounds())) {
  108.             return true;
  109.         }
  110.         return false;
  111.     }
  112.  
  113.     void setPos(sf::Vector2f newPos) {
  114.         paddle.setPosition(newPos);
  115.     }
  116. private:
  117.     sf::RectangleShape paddle;
  118. };
  119.  
  120.  
  121. #pragma once
  122.  
  123. #include <SFML\Graphics.hpp>
  124.  
  125. class Ball {
  126. public:
  127.     Ball(sf::Vector2f size) {
  128.         ball.setSize(size);
  129.     }
  130.  
  131.     void drawTo(sf::RenderWindow &window) {
  132.         window.draw(ball);
  133.     }
  134.  
  135.     void move(sf::Vector2f distance) {
  136.         ball.move(distance);
  137.     }
  138.  
  139.     sf::FloatRect getGlobalBounds() {
  140.         return ball.getGlobalBounds();
  141.     }
  142.  
  143.     void setPos(sf::Vector2f newPos) {
  144.         ball.setPosition(newPos);
  145.     }
  146. private:
  147.     sf::RectangleShape ball;
  148. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement