Qbasicnews.com

Full Version: Delay?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I make a program wait a second or two before moving on.

I'm fairly new to QB, I wrote a basic game that has a town with 4 options, shop, sleep at the inn, view status, or battle.

I have a stat system setup, each stat affecting the damage in combat.

I would like things like
PRINT "You Attack!"
[wait 1 second]
(Process here)
PRINT "You Deal 15 Damage!"

[wait 1 second]
PRINT "Enemy Dies!"
[wait 1 second]
CLS
PRINT"Level Up!"

etc.

The only way I can think of doing a delay is

DO UNTIL Inkey <> " "
LOOP

I've seen the sleep command being used, but I get an error "Error: Expected end-of-statement" when I use it.
Post a short example of how you are using the SLEEP command, and let us know which version of QB you are using (Qbasic or QuickBasic).
I'm using Qbasic

I use sleep as
SLEEP #

Anonymous

don't use sleep in QB, it sucked.



Code:
Declare Sub wait_a_second()
Declare Sub wait_seconds( s As Double )

Print "You Attack!..."
wait_a_second
Print "You Deal 15 Damage!"
wait_seconds( 3 )
Print "Three seconds later, you realized this test is over!"



Sub wait_a_second()

  Dim t As Double
  t = Timer + 1
  
  Do
  Loop While t > Timer
  
End Sub

Sub wait_seconds( s As Double )

  Dim t As Double
  t = Timer + s
  
  Do
  Loop While t > Timer
  
End Sub
Thankyou, that worked perfectly for what I needed.

Could you explain to me a bit how that works though? I don't like to blindly use code without fully understanding it... and being the newb that I am I don't quite understand that code.
Hey there Kaz, and welcome. Don't feel bad about being a newbie. We all were at some point right? Anyway...

The code that chaos posted is based off of the Timer function in QB.

First of all, let's few the "wait_a_second" sub. This will pause for one second. The first two lines in the sub are...


Code:
Dim t As Double
  t = Timer + 1

Timer returns the current system time (which I believe is the number of seconds since a specific date or something like that). t is assigned the current system time plus one second, or one second into the future. Now...

Code:
Do
  Loop While t > Timer

What this does, is go into an infinite loop until t (which is one second in the future remember?) is less than the current time, which would indicate that it has been one second since you started the sub. Make sense?

Now the other sub takes in a short value, the number of seconds you want to wait. Note that because it is a short, it can be a decimal number. The sub is almost identical to the first one, except for this line...

Code:
t = Timer + s

This makes t equal s seconds into the future, instead of one. Does this make sense? Ask if you have trouble.