|
Timer | ||
CurrentMillisecond = Timer() | ||
Parameters: NONE | ||
Returns: CurrentMillisecond = current timer value |
||
Timer will return the internal system time in Milliseconds. There are 1000 milliseconds per second. This is the same on every computer, regardless of how fast it is. Hence in computer world, 1000 milliseconds makes up one second of our time, 500 milliseconds would be 1/2 a second and 250 milliseconds is a 1/4 of a second. Most games try to update the display at 50fps to 60fps (frames per second) (See FPS). For a 50 frames per second refresh rate, our game must update in (1000/50)=20 milliseconds. Where at a 60FPS refresh, the game would need to update in (1000/60)=16.67 milliseconds. Given there's a lot of different systems and configurations of systems out there, this can be difficult part of programming your own games, Especially when trying to keep everything moving smooth and consistently on different systems. One strategy you can use is often called "Timer Based Movement" (Search forums for some tutorials) which is just another way of controlling movements. Which just means that rather than thinking of motions in pixels per frame, we can convert our motions to pixels per millisecond. So if we had a game running at 50 frames per second and a character is moving 2 pixels per frame. Then ideally every second it's going to move (50*2)=100 pixels. Now we're assuming the computer can keep the refresh rate constant though, if it slows down (too much stuff on screen) to say 25 updates per second, then the object is only going to have moved (25*2)=50 pixels in that second. These kinds of performances bumps can really affect how the game plays for your users. There's a few ways to counter this, but one way is to measure the time past since the object started moving in it's current direction. We then multiply that time by the number pixels it should move in a millisecond. This will let us compute where the object should be at any given time. Check out the second example bellow and don't forget to search the forums for some tutorials on timer based movement also. FACTS: * Note: Timer() returns the current millisecond. This value is not from the start of your application though, it's the time since the computer the program is running upon was last reset. Moreover since timer() is a 32bit value and it's ticking over at 1000 units per second, the value can wrap (overflow back to zero) while your program is running. Mini Tutorial #1: This example uses the timer() to make the program update at interval of every 200 milliseconds (1/5 of a second).
Mini Tutorial #2: Frame & Timer Base Movement Example This example shows frame based and timer base movements side by side. The idea of this short program, is that it moves two circles at a rate of 120 pixels per second. The top BLUE circle is moving 2 pixels every time through the main loop, where the bottom RED circle is moving at an equivalent rate of 2 pixels per millisecond. So the bottom objects position is actually calculated by multiplying the time past (the time since the demo started) and pixels per millisecond. This means no matter how fast or slow the loop is executed, the objects motion will be consistent. However, the top object will appear to speed up and slow down if the frame rate is altered. To help demonstrate the difference, you can control the speed of the main loop by pressing the function keys 1 to 6 in the demo code bellow. Changing the speed to say 10FPS, will show the top object slowing down but the bottom object will keep moving at the same rate, the only difference is going to be the smoothness of the bottom objects motion.
|
Related Info: | CurrentDate$ | CurrentTime$ | Fps | GetFps | SetFps | Wait : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |