1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#include <SFML/Graphics.hpp>
#include <iostream>
#define BOARD sf::Shape::Rectangle(0.f, 0.f,10.f, 80.f, sf::Color::Blue)
int dir;
int paddleTouch;
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Pong Revision 3");
sf::Vector2f ballCenter(1.f, 1.f);
sf::Shape Ball = sf::Shape::Circle(ballCenter, 5.f, sf::Color::Blue);
sf::Shape Board1 = BOARD;
sf::Shape Board2 = BOARD;
sf::Vector2f ballPosition(400.f,300.f);
Ball.SetPosition(ballPosition);
sf::Vector2f boardPosition(0.f, 260.f);
Board1.SetPosition(boardPosition);
sf::Vector2f boardPosition2(790.f, 260.f);
Board2.SetPosition(boardPosition2);
while(App.IsOpened())
{
sf::Event ev1;
while(App.GetEvent(ev1))
if (ev1.Type == sf::Event::Closed)
App.Close();
float elapsedTime = App.GetFrameTime();
if (dir == 0 && int(Ball.GetPosition().x) == int(Board1.GetPosition().x + 10) && int(Ball.GetPosition().y) > int(Board1.GetPosition().y) && int(Ball.GetPosition().y) < int(Board1.GetPosition().y + 80))
{
dir=3;
paddleTouch = 1;
}
if (dir == 1 && int(Ball.GetPosition().x) == int(Board1.GetPosition().x + 10) && int(Ball.GetPosition().y) > int(Board1.GetPosition().y) && int(Ball.GetPosition().y) < int(Board1.GetPosition().y + 80))
{
dir = 2;
paddleTouch = 1;
}
if (dir == 2 && int(Ball.GetPosition().x) == int(Board2.GetPosition().x -10) && int(Ball.GetPosition().y) > int(Board2.GetPosition().y) && int(Ball.GetPosition().y) < int(Board2.GetPosition().y + 80))
{
dir = 1;
paddleTouch =1;
}
if (dir == 3 && int(Ball.GetPosition().x) == int(Board2.GetPosition().x -10) && int(Ball.GetPosition().y) > int(Board2.GetPosition().y) && int(Ball.GetPosition().y) < int(Board2.GetPosition().y + 80))
{
dir = 0;
paddleTouch =1;
}
//Doesn't hit paddle
if(dir == 0 && int(Ball.GetPosition().y) == 595 && paddleTouch == 0)
dir = 1;
if(dir == 1 && int(Ball.GetPosition().y) == 5 && paddleTouch == 0)
dir = 0;
if(dir == 2 && int(Ball.GetPosition().y) == 5 && paddleTouch ==0)
dir = 3;
if(dir == 3 && int(Ball.GetPosition().y) == 595 && paddleTouch ==0)
dir = 2;
if( dir == 0 && Ball.GetPosition().x > 0 && Ball.GetPosition().y < 600)
Ball.Move(-.1f, .1f);
if(dir == 1 && Ball.GetPosition().x > 0 && Ball.GetPosition().y > 0)
Ball.Move(-.1f, -.1f);
if(dir == 2 && Ball.GetPosition().x < 800 && Ball.GetPosition().y > 0)
Ball.Move(.1f, -.1f);
if(dir == 3 && Ball.GetPosition().x < 800 && Ball.GetPosition().y < 600)
Ball.Move(.1f, .1f);
if (App.GetInput().IsKeyDown(sf::Key::Up) && Board1.GetPosition().y > 0.f)
Board1.Move(0, -2000 * elapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down) && Board1.GetPosition().y < 520.f) Board1.Move(0, 2000 * elapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::W) && Board2.GetPosition().y > 0.f)
Board2.Move(0, -2000 *elapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::S) && Board2.GetPosition().y < 520.f)
Board2.Move(0, 2000 * elapsedTime);
App.Clear();
App.Draw(Board1);
App.Draw(Board2);
App.Draw(Ball);
App.Display();
paddleTouch = 0;
}
return EXIT_SUCCESS;
}
|