Author: T.Jackson Date Submitted: 1/27/2010 Category: Games Compatibility: .NET
This code has been accessed 13974 times.
Task: Precision regulated delay. Often enough, when people make games in Visual Basic they literally use dozens (sometimes more) -- of VB's timer. These timers have poor resolution (~ < 20mS), and they are unregulated. Plus, you usually end up with poor, unmanageable, spaghetti code (all these unpredictable routines that merge to other routines all over the place) -- very hard to follow code. This is partly why most of the games in VB are rubbish.
Here I present a solution to this problem; a regulated delay which has a resolution of ~1mS. It is regulated because it takes into account the latency of other processes and it will run at the same speed on every computer. When you make games in VB, you only need to use one delay. This can be scaled down accordingly, if you require some parts of the game to run at different speeds. A 20mS delay is a good place to start, which means that your game will run at 50fps (frames per second) In fact, this is the actual speed that most commercial games run at. From this 20mS, you can use variables to scale to 100mS by simply incrementing a variable 5 times; when it hits 5 you do something with it (perhaps animate an explosion) and then reset and continue the cycle.
Conclusion: VB's timer is unregulated and doesn't allow a game to be run at the usual 50fps, because of its poor resolution. Multiple occurrences of this timer usually result in poor, unmanageable code. Games only one require central delay with scaling variables which wrap around it to obtain varying other speeds.
Video games are usually treated as one single frame at a time:
2. Game logic (animations etc ...)
3. Render (draw to screen)
That is a 3-step protocol that is dead simple.
Code shown below is compatible with VB 2005 & 2008. initDelay should be called prior to using the delay, otherwise the resolution by default is 5mS.