1
Graphics / How to get the position of a Shape within a window !?
« on: July 02, 2011, 05:43:12 pm »
My bad. It is my first SFML program ^^ (and I appreciate the "real C++" way)
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
//Pong
int main(void)
{
const float PlayerSpeed = 2200;
sf::Vector2<float> ballMove(-500.,00);
sf::RenderWindow app(sf::VideoMode(800,600,32), "My Pong");
sf::Shape player1 = sf::Shape::Rectangle(30,250,40,350,sf::Color::White);
sf::Shape player2 = sf::Shape::Rectangle(800-40,250,800-30,350,sf::Color::White);
sf::Shape ball = sf::Shape::Rectangle(800-405,295,800-395,305,sf::Color::White);
// ball.SetCenter(5,5);
const sf::Input& in = app.GetInput();
sf::Clock clock;
while(app.IsOpened())
{
sf::Event ev;
while(app.GetEvent(ev))
{
if(ev.Type == sf::Event::Closed)
app.Close();
}
app.Clear();
//Move player1 (a & q)
if(in.IsKeyDown(sf::Key::A))
player1.Move(0,-PlayerSpeed*clock.GetElapsedTime());
if(in.IsKeyDown(sf::Key::Q))
player1.Move(0,PlayerSpeed*clock.GetElapsedTime());
//Move player2 (p & m)
if(in.IsKeyDown(sf::Key::P))
player2.Move(0,-PlayerSpeed*clock.GetElapsedTime());
if(in.IsKeyDown(sf::Key::M))
player2.Move(0,PlayerSpeed*clock.GetElapsedTime());
//Move the ball
//test collisions between ball and frame borders
//sf::Vector2<float> c = ball.GetPosition();
//std::cerr << c.x << std::endl;
//std::cerr << c.y << std::endl;
std::cerr << ball.GetPointPosition(0).y << std::endl;
if(ball.GetPointPosition(0).x <= 0. && ballMove.x < 0.)
ballMove.x -= ballMove.x;
if(ball.GetPointPosition(0).y <= 0. && ballMove.y < 0.)
ballMove.y -= ballMove.y;
ball.Move(ballMove.x*clock.GetElapsedTime(),ballMove.y*clock.GetElapsedTime());
//Draw everything
app.Draw(player1);
app.Draw(player2);
app.Draw(ball);
app.Display();
clock.Reset();
}
}