Bah par exemple ma balle touche le bas au lieu de repartir vers le haut elle continue la ligne et elle sort de l'écran
image explicative(en fichier joint) (la fleche bleu indique le sens de la balle avant de sortir de l'ecran)
mon code apres une petite modif
#include <SFML\Graphics.hpp>
#include <iostream>
using namespace std;
sf::CircleShape ball(10);
sf::RectangleShape paddle1;
sf::RectangleShape paddle2;
sf::Image icon;
int screenWidth = 800;
int screenHeight = 600;
float PI = 3.14;
void gestion_clavierP1();
void gestion_clavierP2();
sf::Vector2f ballSpeed(0.1, 0.1);
int main()
{
sf::RenderWindow window(sf::VideoMode(screenWidth, screenHeight, 32), "Pong"); //Width Heigth
ball.setPosition(sf::Vector2f(screenWidth / 2 - 10, screenHeight / 2 - 10));
ball.setFillColor(sf::Color::Black);
paddle1.setPosition(sf::Vector2f(10, screenHeight / 2 - 100));
paddle1.setSize(sf::Vector2f(10, 100));
paddle1.setFillColor(sf::Color::Black);
paddle2.setPosition(sf::Vector2f(780, screenHeight / 2 - 100));
paddle2.setSize(sf::Vector2f(10, 100));
paddle2.setFillColor(sf::Color::Black);
/**ICON**/
if (!icon.loadFromFile("icon.png"))
{
cout << "L'icon n'a pas charger" << endl;
EXIT_FAILURE;
}
else
{
window.setIcon(16, 16, icon.getPixelsPtr());
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == event.Closed)
{
window.close();
}
gestion_clavierP1();
gestion_clavierP2();
}
/**Faire bouger la balle au demarrage**/
ball.move(ballSpeed.x, ballSpeed.y);
/*if ((paddle1.getPosition().y - 100) == (ball.getPosition().y - 10) || (paddle1.getPosition().x - 10) == (ball.getPosition().x - 10))
{
cout << "COLLISION" << endl;
ball.move(-ballSpeed.x, ballSpeed.y);
}*/
/**Collision de la balle avec le mur**/
if (ball.getPosition().y <= 0)
{
cout << "COLLISION Y 1" << endl;
ball.move(ballSpeed.x, ballSpeed.y);
}
if (ball.getPosition().y >= screenHeight - 20)
{
cout << "COLLISION Y 2" << endl;
ball.move(ballSpeed.x, -ballSpeed.y);
}
if (ball.getPosition().x <= 0)
{
cout << "COLLISION X 1" << endl;
ball.move(ballSpeed.x, 0);
}
if (ball.getPosition().x >= screenWidth - 20)
{
cout << "COLLISION X 2" << endl;
ball.move(-ballSpeed.x, 0);
}
/**Colission paddle 1 avec le mur**/
if (paddle1.getPosition().y <= 0)
{
paddle1.setPosition(10, 0);
}
if (paddle1.getPosition().y >= screenHeight - 100)
{
paddle1.setPosition(10, screenHeight - 100);
}
/**Colission paddle 2 avec le mur**/
if (paddle2.getPosition().y <= 0)
{
paddle2.setPosition(780, 0);
}
if (paddle2.getPosition().y >= screenHeight - 100)
{
paddle2.setPosition(780, screenHeight - 100);
}
window.clear(sf::Color::White);
window.draw(ball);
window.draw(paddle1);
window.draw(paddle2);
window.display();
}
}
void gestion_clavierP1()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
paddle1.move(0, -1.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
paddle1.move(0, 1.0f);
}
}
void gestion_clavierP2()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
paddle2.move(0, -1.0f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
paddle2.move(0, 1.0f);
}
}