Qbasicnews.com
April 03, 2020, 07:12:04 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Delay?  (Read 1884 times)
Kaz
New Member

Posts: 6


« on: February 20, 2006, 11:53:59 PM »

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.
Logged
RyanKelly
Forum Regular
**
Posts: 109



WWW
« Reply #1 on: February 21, 2006, 01:23:54 AM »

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).
Logged
Kaz
New Member

Posts: 6


« Reply #2 on: February 21, 2006, 02:24:19 AM »

I'm using Qbasic

I use sleep as
SLEEP #
Logged
Anonymous
Guest
« Reply #3 on: February 21, 2006, 03:52:01 AM »

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
Logged
Kaz
New Member

Posts: 6


« Reply #4 on: February 21, 2006, 09:01:28 PM »

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.
Logged
Torahteen
Ancient Guru
****
Posts: 744



« Reply #5 on: February 22, 2006, 03:39:11 AM »

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.
Logged

quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!