#include "stdafx.h"
#include <SFML/Graphics.hpp>
int _tmain(int argc, _TCHAR* argv[])
{
sf::RenderWindow App(sf::VideoMode(800,600,32), "PONG");
App.SetFramerateLimit(60);
sf::Image RedPaddle;
sf::Image BluePaddle;
sf::Image GreenBall;
if (!RedPaddle.LoadFromFile("images/RedPaddle.png"))
return EXIT_FAILURE;
if (!BluePaddle.LoadFromFile("images/BluePaddle.png"))
return EXIT_FAILURE;
if (!GreenBall.LoadFromFile("images/GreenBall.png"))
return EXIT_FAILURE;
sf::Sprite sRedPaddle(RedPaddle);
sf::Sprite sBluePaddle(BluePaddle);
sf::Sprite sGreenBall(GreenBall);
float ballx = 100;
float bally = 100;
sf::FloatRect rGreenBall;
sf::FloatRect rBluePaddle;
sf::FloatRect rRedPaddle;
sRedPaddle.Move(350, 550);
sBluePaddle.Move(350, 25);
sGreenBall.Move(375, 300);
while (App.IsOpened())
{
sf::Event Close;
while (App.GetEvent(Close))
{
if (Close.Type == sf::Event::Closed)
App.Close();
}
float ElapsedTime = App.GetFrameTime();
sBluePaddle.Move(ballx * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Left)) sRedPaddle.Move(-200 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) sRedPaddle.Move( 200 * ElapsedTime, 0);
sGreenBall.Move(ballx * ElapsedTime, bally * ElapsedTime);
rGreenBall.Top = sGreenBall.GetPosition().y;
rGreenBall.Left = sGreenBall.GetPosition().x;
rGreenBall.Bottom = rGreenBall.Top + sGreenBall.GetSize().y;
rGreenBall.Right = rGreenBall.Left + sGreenBall.GetSize().x;
rBluePaddle.Top = sBluePaddle.GetPosition().y;
rBluePaddle.Left = sBluePaddle.GetPosition().x;
rBluePaddle.Bottom = rBluePaddle.Top + sBluePaddle.GetSize().y;
rBluePaddle.Right = rBluePaddle.Top + sBluePaddle.GetSize().x;
rRedPaddle.Top = sRedPaddle.GetPosition().y;
rRedPaddle.Left = sRedPaddle.GetPosition().x;
rRedPaddle.Bottom = rRedPaddle.Top + sRedPaddle.GetSize().y;
rRedPaddle.Right = rRedPaddle.Top + sRedPaddle.GetSize().x;
if (rGreenBall.Intersects(rRedPaddle))
{
ballx = 200;
bally = -bally;
}
else if (rGreenBall.Intersects(rBluePaddle))
{
ballx = -200;
bally = -bally;
}
App.Clear();
App.Draw(sRedPaddle);
App.Draw(sBluePaddle);
App.Draw(sGreenBall);
App.Display();
}
return 0;
}
So what do you think?