Sorry for dont reply quickly... btw... I have removed the paddle to focus on ball movement... so now its working... so you can check and study the code and put the paddle...
there goes:
#include <SFML/Graphics.hpp>
#include <random>
#include <functional>
#include <cstdlib>
#include <cmath>
int main(int argc, char* argv[])
{
const int window_width = 800;
const int window_height = 600;
const float ball_radius = 16.f;
sf::RenderWindow window(sf::VideoMode(window_width, window_height), "SFML Pong");
window.setVerticalSyncEnabled(true);
std::random_device seed_device;
std::default_random_engine engine(seed_device());
std::uniform_int_distribution<int> distribution(-16, 16);
auto random = std::bind(distribution, std::ref(engine));
sf::Vector2f direction(random(), random());
const float velocity = 15;
sf::CircleShape ball(ball_radius - 4);
ball.setOutlineThickness(4);
ball.setOutlineColor(sf::Color::Black);
ball.setFillColor(sf::Color::White);
ball.setOrigin(ball.getRadius(), ball.getRadius());
ball.setPosition(window_width / 2, window_height / 2);
sf::Clock clock;
sf::Time elapsed = clock.restart();
const sf::Time update_ms = sf::seconds(1.f / 30.f);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if ((event.type == sf::Event::Closed) || ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
{
window.close();
break;
}
}
elapsed += clock.restart();
while (elapsed >= update_ms)
{
const auto pos = ball.getPosition();
const auto delta = update_ms.asSeconds() * velocity;
sf::Vector2f ball_pos(pos.x + direction.x * delta * 2, pos.y + direction.y * delta * 2);
if (new_pos.x - ball_radius < 0)
{ // left window edge
direction.x *= -1;
ball_pos.x = 0 + ball_radius;
}
else if (new_pos.x + ball_radius >= window_width)
{ // right window edge
direction.x *= -1;
ball_pos.x = window_width - ball_radius;
}
else if (new_pos.y - ball_radius < 0)
{ // top of window
direction.y *= -1;
ball_pos.y = 0 + ball_radius;
}
else if (new_pos.y + ball_radius >= window_height)
{ // bottom of window
direction.y *= -1;
ball_pos.y = window_height - ball_radius;
}
ball.setPosition(ball_pos);
elapsed -= update_ms;
}
window.clear(sf::Color(50, 200, 50));
window.draw(ball);
window.display();
}
return EXIT_SUCCESS;
}