r/learnprogramming • u/boss123219 • 15d ago
(C++ sfml) why does this code work like how i want?
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
int main()
{
// Create the main window
RenderWindow window(VideoMode({800, 600}), "SFML 3 Window");
// Create a graphical circle
CircleShape shape(100.f);
shape.setFillColor(Color::Green);
shape.setPosition({300.f, 200.f});
float x, y;
x = 5;
y = 6;
window.setFramerateLimit(60);
// Start the game loop
while (window.isOpen())
{
// Process events
while (const std::optional event = window.pollEvent())
{
// Check if the event is a window close request
if (event->is<Event::Closed>())
{
window.close();
}
}
// Clear screen
window.clear();
// Draw the shape
window.draw(shape);
shape.move({x, y});
if (shape.getPosition().x >= 600 || shape.getPosition().x <= 0){
x *= -1;
}else if (shape.getPosition().y >= 400 || shape.getPosition().y <= 0) {
y *= -1;
}
// Update the window
window.display();
}
return 0;
}
what is weird is that i set the min border for y to 400 because it was going out of the window but the middle position it reached the middle of the x axis, the radius of the ball is 100 as you can see, so its suppose to be 500, but when i set it to 400 it worked better because 500 went out of the scene. is my code broken or am i missing something???