Speed

Game development specific discussions.
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: Speed

Post by ITomi »

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):

Code: Select all

sleep Regulate(25,fps),1
and:

Code: Select all

waiting=timer+.025
do while timer<waiting
loop
But I will try these on my old, slower laptop, too. That will be the moment of truth! :D
And special thanks to Paul Doe for the useful links!
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Speed

Post by badidea »

ITomi wrote: Mar 08, 2023 13:28

Code: Select all

waiting=timer+.025
do while timer<waiting
loop
Be aware that that piece of code will keep 1 CPU-core very busy while waiting.
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: Speed

Post by ITomi »

badidea wrote: Mar 08, 2023 23:28
ITomi wrote: Mar 08, 2023 13:28

Code: Select all

waiting=timer+.025
do while timer<waiting
loop
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.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Speed

Post by grindstone »

To get a loop executed 25 times a second I would recommend this:

Code: Select all

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.
ixiz9
Posts: 14
Joined: Jan 01, 2023 4:50

Re: Speed

Post by ixiz9 »

A few comments from my experience doing space invaders, pacman and my current projects.
  1. Think in terms of time, not frames. How long should this take in seconds? This will isolate you from different hardware speeds.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
Good luck and have fun!
Post Reply