Qbasic game controls

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
sk8miniramps
Qbasic game controls

Post by sk8miniramps » Wed Sep 29, 2004 6:59 pm

I have made a couple tile based QBasic games that use the arrow keys to move the character. But if you hold down a key for a second and then let go the character keeps moving for another second or two. Is there a way to make him move while you hold down a key, but stop right when you let go?

Dr. D'nar
Newbie
Posts:6
Joined:Tue Sep 28, 2004 10:49 pm

Post by Dr. D'nar » Wed Sep 29, 2004 11:03 pm

It keeps on going because when you hold down a key, the key simply keeps sending the siginal that it's held down over and over. The BIOS on your computer has a buffer that holds recently pushed keys. What's happening is that the program is reading those buffered key strokes. Like so:
[down key]x3 -> BIOS buffer
The BIOS buffer then looks like this:
d d d
The program then reads one down key and does what it's supposed to do. The BIOS knows that one key stroke has been read and removes that key stroke from it's buffer. So the key buffer then looks like this:
d d
Then after the program is finished doing what it's doing it then reads the next stroke and so forth.

Bottem line: Make your program faster than the repete rate of the keyboard and it will work just fine. Other wise your out of luck.

Dr_Davenstein
QBasic God
Posts:166
Joined:Tue Mar 25, 2003 12:45 am
Location:U.S.A.
Contact:

Post by Dr_Davenstein » Sat Oct 02, 2004 8:00 pm

You can also use a library, or write your own keyboard routine. ;)

Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

Keyboard buffer clearout

Post by Ralph » Sun Feb 11, 2007 12:42 am

On October of 2004, you answered a question regarding the subject matter, but didn't give the manner of solution, at this site:
http://qbasic-forum.qbcafe.net/viewtopi ... t=keyboard

I have that problem, today, and wonder if you could give me a helping hand?
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

Dr_Davenstein
QBasic God
Posts:166
Joined:Tue Mar 25, 2003 12:45 am
Location:U.S.A.
Contact:

Post by Dr_Davenstein » Sun Feb 11, 2007 2:04 am

Ralph, that link leads to this thread. Was that an accident?
Come check out [url=http://www.freebasic.net/forum/index.php]FreeBASIC[/url]. The syntax is based on QuickBasic, but it expands to use pointers, operator overloading, etc... The list goes on and on!

Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

Post by Ralph » Sun Feb 11, 2007 2:40 am

No, Dr_V. The accident was that I posted here, thinking I was sending you a PM, which I later did.

But, I believe I have solved the problem very simply, even though I'm not sure I understand the logic behind my solution. Here it is. Simply enter my "keyboard buffer emptier:

Code: Select all

FOR I = 1 TO 15
  K$ = INKEY$
NEXT I
If you can explain why it works, I would appreciate it.

EDITED SUNDAY, FEB 11, 2007 10:30 AM Central Time
I got another, better solution for the above, from red_Marvin:

Code: Select all

do while inkey$ <>"":loop
Works fine!
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

Post by Ralph » Tue Feb 13, 2007 3:14 am

I am now more knowledgeable in moving figures or sprites in QuickBASIC. I have learned that using the INKEY$() function to trap a keypress has some overhead problems, as clearing out the keyboard buffer takes a measurable time! Instead, I have learned that one can use:

Code: Select all

K = INP(96)
to capture the pressed key's scan code, and use that, instead of the value of INKEY$(). An added gain is that we do away with the slight hesitation, or jump, that we see in the sprite when we first hold down a key to move it.
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

Post Reply