Hello,

 

I tested the tutorial programs for QuickBasic that are located on your website for tutorials, and have made a change to one of the example programs.

[EDITOR'S NOTE: Referring to Andre Van Wyk's Basic Course In Game Design, originally published in 1996. - http://www.petesqbsite.com/sections/tutorials/tutorials/basicgd.htm ]
 

I discovered that the programmer who wrote the tutorial used a very simple loop to control program timing.  Modern programs are compiled (QB45 or FreeBasic) and most compilers provide some optimization.

 

An example of this process looks like this:

 

 Timed loop #1:

 

 FOR counter = 1 TO 10000

NEXT counter

 

This process works well on interpreted programs but the following problems can be observed

 

1,.  Shortening the code by combining the two lines

 

FOR counter = 1 TO 10000: NEXT counter

 

will cause the loop to run in a much shorter time.  This time the loop will be completed faster, and therefore the end of the range (10000) would need to be much higher to achieve the same time delay.

 

If each of these loops are compiled using QB45 or FreeBasic the loops will each execute in the same amount of time.  The programmer would need to test the loop and adjust the ending value repeatedly until the desired delay is achieved.  Once compiled, the loop will execute at different speeds on different machines.  The faster the processor, the quicker the loop will execute.  To make a delay loop that is more effective I wrote a subroutine that can be included in any QuickBASIC or FreeBASIC program which provides more reliable timing delays.

 

DECLARE SUB SNOOZE(delay)

 

SUB SNOOZE(delay)

 

  StartTime = TIMER

  WHILE StartTime + delay < TIMER

  WEND

 

END SUB

 

This loop will sleep for a specific amount of time with the following statement

 

REM Sleep for ¼ second

Delay = .25

CALL SNOOZE(delay)

 

To allow for keyboard interruption of the delay loop the following can be used

 

DECLARE SUB SNOOZE(delay, keypress$)

 

SUB SNOOZE(delay, keypress$)

 

  StartTime = TIMER

  WHILE StartTime + delay < TIMER

  Keypress$ = INKEY$

  IF keypress$ <> “” THEN END SUB

  WEND

 

END SUB

 

This routine will function the same as the first example with the following differences:

 

Delay = .1

Keypress$ = “”

CALL SNOOZE(delay, keypress$)

 

When the loop completes, and program control returns to the main program from the subroutine keypress$ will contain the key that was pressed to exit the loop, or will be empty if no key was pressed before the delay time expired.

 

This technique can make it easier to program delays in programs.

 

If anyone would like to contact me about these routines please email rgreenlaw@adelphia.net

 

Roger Greenlaw
