How can I make the ball bounce back if it reaches the end of the screen?
#include <SFML/Graphics.hpp>
int main()
{
int screen_w = 640;
int screen_h = 480;
sf::RenderWindow window(sf::VideoMode(screen_w,screen_h), "SAMPLE");
sf::Image image;
image.loadFromFile("ball.png");
image.createMaskFromColor(sf::Color(255,0,255));
sf::Texture texture;
texture.loadFromImage(image);
sf::Sprite sprite(texture);
float ballx = 0.1;
float bally = 0.0;
while(window.isOpen())
{
sprite.move(ballx,bally);
window.clear();
window.draw(sprite);
window.display();
sf::Event event;
while(window.pollEvent(event))
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
ballx = -0.1;
bally = 0.0;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
ballx = 0.1;
bally = 0.0;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
bally = 0.1;
ballx = 0.0;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
bally = -0.1;
ballx = 0.0;
}
}
}
}