#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
using namespace sf;
int main()
{
RenderWindow window(VideoMode(800, 600), "Lolyan", Style::Default);
Event event;
CircleShape circle(30.0f, 30.0f);
circle.setFillColor(Color::Blue);
circle.setPosition(40, 530);
window.setFramerateLimit(200);
window.draw(circle);
window.display();
int velocityX = 0, velocityY = 0;
Vector2i mousePosition;
while (window.isOpen()) {
circle.move(velocityX / 2, velocityY / 2);
window.draw(circle);
window.display();
while (window.pollEvent(event)) {
window.clear(Color::Black);
if (event.type == Event::Closed) {
window.close();
}
else if (event.type == Event::KeyPressed ) {
cout << velocityX << endl;
switch (event.key.code) {
case Keyboard::A:
velocityX = -5;
break;
case Keyboard::D:
velocityX = 5;
break;
case Keyboard::S:
velocityY = 5;
break;
case Keyboard::W:
velocityY = -5;
break;
}
}
else if (event.type == Event::MouseButtonPressed) {
circle.setFillColor(Color::Red);
}
else if (event.type == Event::MouseButtonReleased) {
circle.setFillColor(Color::Blue);
}
else if (event.type == Event::MouseMoved) {
mousePosition = Mouse::getPosition(window);
circle.setPosition(mousePosition.x - 30, mousePosition.y - 30);
}
else if (event.type == Event::KeyReleased) {
velocityX = 0;
velocityY = 0;
}
window.display();
}
}
return 0;
}
So this is not the first method I tried to move the ball. The first method is every event when a key is pressed the x or y changed, but with this method the movement wasn't as smooth as I want so I tried this method where when example: I press w the velocity (velocityY) becomes 5 and every frame of the game the position changes. And it works smoothly but a glitchy contrail runs before the ball disapering and reappearing sometimes. I found out to that when i move the mouse in the window (knowing that I implemented an event that takes mouse movement) the ball moves very smoothly.
window.display()
in the event loop and also outside. You should call that only once.windoe.clear(); /* draw window stuff */ window.display();
. - Galik