How to control the speed of objects in my simple game

I am making a game like space invaders in C++ using the win32 api. I am a beginner. when I increase same speed across the screen no matter what the FPS is.

I use the and library to determine how many times the main apkqad games loops per second and use sleep_for() to add a delay to meet the desired FPS:

std::this_thread::sleep_for(std::chrono::milliseconds(targetFrameTime - elapsedTime));

I move objects in the main loop like this:

if (userInput.get_right_key_down()) {
     rect_x = rect_x + 5;
}

I would rather not use thread if possible. I don’t really understand it and don’t know if I need it.

You’re better off updating the position with rect_x = rect_x + 300 * TimeDelta() (make 300 how ever many pixels you want the object to move per second).

TimeDelta() will return the fraction of a second that has passed since the last event, generally 0.016 or 1/60th of a second, and after each second will have moved 300 pixels. On slower computers or at slower frame rated TimeDelta() will be a larger value.

But no matter what the frame rate is, approximately every 1 second (or about 60 frames), rect_x will have increased by 300.