Thank you very much your helpful examples, boys!
I tried Dodicat's Regulate() function and Grindstone's piece of code too on my desktop PC and both of these could regulate the game speed to 25 fps with the correct values (if I know right, 25 fps is enough for the human eyes to see smooth motion pictures):
Be aware that that piece of code will keep 1 CPU-core very busy while waiting.
Okay and thanks for the warning!
Otherwise both of those codes worked correctly on the laptop: Grindstone's code was faster a bit, because it allowed some more extra fps (approximately +5 +10 besides the given 25), while Dodicat's Regulate() function was the more precise as regards the amount of fps.
Do
timecomp = Timer + .04 '1/25 second
'
'put here the code to be executed 25 times per second
'
Do While Timer < timecomp
'waste the rest of the time...
Loop
Loop
So you don't have to care about the execution time of the code even if you change it or if its execution time isn't constant, as long as it doesn't take longer than 1/25 second.
A few comments from my experience doing space invaders, pacman and my current projects.
Think in terms of time, not frames. How long should this take in seconds? This will isolate you from different hardware speeds.
Think in terms of objects size, not pixels. How long should the shot take to cross the display, not how long should the shot take to travel 100 pixels? This will help your code handle different screen resolutions.
Do not recalculate fps at each frame. Keep a count of how many frames you rendered since the start of the game (or currentl level), and how long the game/level has been going and divide total frame count by total time to get FPS. Don't do: FPS = 1 / time to draw last frame. Letting FPS change slowly makes the game feel smoother. Recalulating based on last frame makes the game a lot more jerky.
You can start your project by keeping time values as constants. Be aware though that changing time values is an easy way to tweak difficulty, so you can play around with different values and even have change based on level. For example, aliens can cross the screen faster at higher levels of difficulty.
Things like FPS, velocity (pixels/frame), etc should be stored and calcualted as floating point. Only translate to integer at the last stage when you actually render.