Qbasic game controls
Moderators:Administrator, Global Moderator
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?
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.
[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.
-
- QBasic God
- Posts:166
- Joined:Tue Mar 25, 2003 12:45 am
- Location:U.S.A.
- Contact:
Keyboard buffer clearout
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?
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.
-
- QBasic God
- Posts:166
- Joined:Tue Mar 25, 2003 12:45 am
- Location:U.S.A.
- Contact:
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:
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:
Works fine!
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
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
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.
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:
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.
Code: Select all
K = INP(96)
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.