T IMER QUESTION

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

T IMER QUESTION

Post by Sinuvoid »

Hey again, I was wondering how to make "TIMER" a varible so when it gets to a certain timeit does something

Example:

Code: Select all

DO
oldtime = TIMER 'makes the timer count up from zero
x = 3
y = 7
enemyx = 20
enemyy = 18

PRINT "TIME: "; INT(oldtime - TIMER)


LOCATE y, x
PRINT ".8."

IF TIMER = (this is the part im having trouble with) THEN
locate enemyy, enemyx
PRINT "|8."

LOOP

thank in advance
User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Post by Stoves »

TIMER returns the number of seconds that have passed since midnight. Precision is to the hundredths of a second.

The following code sets the timer to 5.5 seconds and then prints a notice when the timer expires.

Code: Select all

DIM TimeToExpire AS DOUBLE
DIM SecondsToWait AS DOUBLE

SecondsToWait = 5.5

PRINT "Timer started..."
TimeToExpire = TIMER + SecondsToWait
DO
LOOP UNTIL TIMER >= TimeToExpire
PRINT "Time's Up! " + LTRIM$(RTRIM$(STR$(SecondsToWait))) +" seconds have passed."
User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Post by Stoves »

Here's your code revamped to use TIMER to move the enemy after a set amount of time. Change the whenToMoveEnemy variable to anything .01 seconds or more to set the time between enemy movements.

You'll notice there was a need to add another timer-related variable to make this work right. The first variable (startTime) is set only once so that the total timer displays correctly. The oldTime variable resets to TIMER whenever the enemy moves.

Code: Select all

DIM startTime AS INTEGER
DIM x AS INTEGER, y AS INTEGER
DIM enemyx AS INTEGER, enemyy AS INTEGER
DIM whenToMoveEnemy AS DOUBLE
DIM oldtime AS DOUBLE

whenToMoveEnemy = 3
x = 3 
y = 7 
enemyx = 20 
enemyy = 18 
startTime = TIMER 

DO 
LOCATE 1,1
PRINT "TIME: "; INT(TIMER - startTime)

LOCATE y, x 
PRINT ".8." 

IF (TIMER - oldtime) >= whenToMoveEnemy THEN 
  locate enemyy, enemyx 
  PRINT "   "
  enemyx = enemyx - 1
  IF enemyx < 1 then enemyx = 40
  locate enemyy, enemyx
  PRINT "|8." 
  oldTime = TIMER
END IF

LOOP
Post Reply