#include "stdafx.h"
#include <SFML/Graphics.hpp>
int _tmain(int argc, _TCHAR* argv[])
{
const float PI = 3.14159f;
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 ballspeed = 400.f;
float ballx = 0;
float bally = 100;
int x = 3;
sf::FloatRect rGreenBall;
sf::FloatRect rBluePaddle;
sf::FloatRect rRedPaddle;
while (App.IsOpened())
{
sf::Event Close;
while (App.GetEvent(Close))
{
if (Close.Type == sf::Event::Closed)
App.Close();
}
float gElapsedTime = App.GetFrameTime();
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;
sGreenBall.Move(ballx * gElapsedTime, bally * gElapsedTime);
if (rGreenBall.Intersects(rRedPaddle))
{
ballx = -ballx;
bally = -bally;
}
if (rGreenBall.Intersects(rBluePaddle))
{
ballx = -ballx;
bally = -bally;
}
float ElapsedTime = App.GetFrameTime();
if (App.GetInput().IsKeyDown(sf::Key::Left)) sRedPaddle.Move(-200 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) sRedPaddle.Move( 200 * ElapsedTime, 0);
App.Clear();
App.Draw(sRedPaddle);
App.Draw(sBluePaddle);
App.Draw(sGreenBall);
App.Display();
}
return 0;
}
It doesn't work, even though the sprite should start moving the other way whenever the ball hits the paddle, but it doesn't work.