Qbasicnews.com
October 19, 2021, 09:42:10 AM *
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: Clearing the Keyboard Buffer Quickly  (Read 6825 times)
Aaron
Member
*
Posts: 35


« on: March 18, 2004, 01:11:56 AM »

I already posted this in the thread for my project, but I figured I would post it here as well in the hope of getting help from people who aren't reading my thread on the projects board.

Anyway, here's my problem:


For part of my game, I am having the player hit a key that corresponds to a certain sprite. The sprites are of the 4 directional arrows but that is largely irrelevant. Anyway, I have several of the arrows lined up, and after a given delay (~1/5 sec) the next one in the que starts moving across the screen. The player has to hit the key representing that sprite before it reaches a limit.

The problem is, after the key is pressed for one arrow, the keyboard buffer isn't cleared quickly enough and the computer thinks the key is still pressed (even when its not) and registers an incorrect key press.

I'm using RelLib's key handler and I've tried dumping the keyboard buffer by replacing the header with the tail (Poke &H1A, Peek (&H1C)), but it doesn't seem to work, I still get the problem.


Any Suggestions?
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #1 on: March 18, 2004, 02:16:56 AM »

Code?  I haven't encountered or any Relkey user of that sort of prob. :*(
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
Aaron
Member
*
Posts: 35


« Reply #2 on: March 18, 2004, 02:26:59 AM »

Sorry, its long and uncommented; I just started using RelKey with this sub recently, and I'm sure you could suggest better implementation...


Code:

DEFINT A-Z
SUB darkonsub
'darkon(x,0) = type of arrow
'darkon(x,1) = x-position of that arrow
'darkon(x,2) = usage of that arrow
'              -0 = not in use
'              -1 = inuse
'              -2 = moving
'              -3 = user pressed wrong key

DIM keyp%(5)

darkondata.maxhit = 0
darkondata.hitinarow = 0
'----finds first and last arrows
first% = 0
last% = 0
FOR i% = 0 TO darkondata.numberofarrows - 1
   IF darkon(i%, 2) = 0 AND i% > 0 THEN
IF darkon(i% - 1, 2) = 1 THEN last% = i%
   ELSE last% = i%
   END IF
NEXT i%
IF last% = 0 THEN GOTO quitdarkonsub
darkon(first%, 2) = 2
darkondata.timeoflast = TIMER


DO
   'dumb$ = INKEY$
   'keyp$ = INKEY$
   'IF keyp$ <> "" THEN press% = ASC(keyp$)
   '----Gets Input----
DEF SEG = &H40
POKE &H1A, PEEK(&H1C)
POKE &H1A, PEEK(&H1C)
DEF SEG

IF RelKey%(72) THEN keyp%(1) = 1  'up
IF RelKey%(80) THEN keyp%(2) = 1  'down
IF RelKey%(75) THEN keyp%(3) = 1  'left
IF RelKey%(77) THEN keyp%(4) = 1  'right
IF RelKey%(1) THEN GOTO quitdarkonsub   'esc
 
   'DO: LOOP WHILE darkondata.waiter + 250 > CLOCK&
   'darkondata.waiter = CLOCK&
 
   '----turns on next moving arrow
   IF darkondata.timeoflast + darkondata.rate < TIMER THEN
darkondata.timeoflast = TIMER
FOR i% = 0 TO last%
   IF darkon(i%, 2) = 1 THEN
 darkon(i%, 2) = 2
 darkon(i%, 1) = darkondata.startlimit + 1
 FOR j% = i% + 1 TO last%
darkon(j%, 1) = darkondata.startlimit + 13 + (j% - i%) * 12
 NEXT j%
 i% = last%
   END IF
NEXT i%
   END IF
 
   '----checks arrows to see if they have been hit or if they have passed the end limit
   FOR i% = 0 TO darkondata.numberofarrows - 1
IF darkon(i%, 2) = 2 OR darkon(i%, 2) = 3 THEN
   darkon(i%, 1) = darkon(i%, 1) - darkondata.speed
   IF darkon(i%, 1) <= darkondata.endlimit AND darkon(i%, 2) = 2 THEN
 darkon(i%, 2) = 3
 darkondata.endlimit = darkondata.endlimit + 12
 IF darkondata.hitinarow > darkondata.maxhit THEN darkondata.maxhit = darkondata.hitinarow
 darkondata.hitinarow = 0
 first% = first% + 1
 GOTO skippage
   END IF
   IF darkon(i%, 1) <= 0 THEN darkon(i%, 2) = 0
END IF
   NEXT i%
   FOR j% = 1 TO 4
'IF keyp%(j%) <> 0 AND darkon(first%, 2) = 2 AND darkondata.waiter + 750 < CLOCK& THEN    'see if key has been pressed
IF keyp%(j%) <> 0 AND darkon(first%, 2) = 2 THEN    'see if key has been pressed
   darkondata.waiter = CLOCK&
   IF j% <> darkon(first%, 0) THEN    'wrong arrow pressed
 darkon(first%, 2) = 3
 darkondata.endlimit = darkondata.endlimit + 12
 IF darkondata.hitinarow > darkondata.maxhit THEN darkondata.maxhit = darkondata.hitinarow
 darkondata.hitinarow = 0
 first% = first% + 1
 GOTO skippage
   END IF
   IF j% = darkon(first%, 0) THEN     'correct arrow pressed
 darkon(first%, 2) = 0
 darkondata.hitinarow = darkondata.hitinarow + 1
 first% = first% + 1
 GOTO skippage
   END IF
END IF
   NEXT j%
skippage:
 
   FOR j% = 0 TO 4
keyp%(j%) = 0
   NEXT j%
   FOR i% = 0 TO last%
IF darkon(i%, 2) = 1 OR darkon(i%, 2) = 2 THEN   'moving or waiting arrow
   RelSprite pageseg%, darkon(i%, 1), SCY% + 3, VARSEG(arrowgraphics%(0)), VARPTR(arrowgraphics%(arrowcnt(darkon(i%, 0))))
END IF
IF darkon(i%, 2) = 3 THEN                   'stuff for x and arrow
   RelSprite pageseg%, darkon(i%, 1), SCY% + 3, VARSEG(arrowgraphics%(0)), VARPTR(arrowgraphics%(arrowcnt(darkon(i%, 0))))
   RelSprite pageseg%, darkon(i%, 1), SCY% + 3, VARSEG(arrowgraphics%(0)), VARPTR(arrowgraphics%(arrowcnt(0)))
END IF
   NEXT i%
   LINE (darkondata.startlimit, SCY% + 2)-(darkondata.startlimit, 199), 48
   LINE (darkondata.endlimit, SCY% + 2)-(darkondata.endlimit, 199), 4
 
 
 
   WAIT &H3DA, 8
   WAIT &H3DA, 8, 8
   RelPCopy &HA000, pageseg%
   LINE (0, SCY% + 2)-(319, 199), 0, BF
   IF first% = last% AND darkon(last%, 2) <> 2 THEN GOTO sumpoints
LOOP



sumpoints:          'tally points
LINE (0, SCY% + 2)-(319, 199), 0, BF
LINE (darkondata.startlimit, SCY% + 2)-(darkondata.startlimit, 199), 48
LINE (darkondata.endlimit, SCY% + 2)-(darkondata.endlimit, 199), 4
player1.life = player1.life + 5 * darkondata.maxhit

quitdarkonsub:      'return to main game
FOR i% = 0 TO darkondata.numberofarrows - 1
   darkon(i%, 2) = 0
NEXT i%
FOR i% = 0 TO numberofshots% - 1
   IF shots(i%).who = 2 THEN shots(i%).inuse = 0
NEXT i%
'DO: LOOP WHILE INKEY$ = ""
 
END SUB
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #3 on: March 18, 2004, 02:35:21 AM »

Try erasing this:

DEF SEG = &H40
    POKE &H1A, PEEK(&H1C)
    POKE &H1A, PEEK(&H1C)
DEF SEG

Though it does not matter really. :*)



And putting

RelKeyBoardON

Do

'game stuff here including calls to RelKey



Loop

RelKeyBoardOFF

End
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
TheBigBasicQ
*/-\*
*****
Posts: 4550



WWW
« Reply #4 on: March 18, 2004, 12:42:58 PM »

This clears the keyboard buffer =).
DEF SEG = &H40
POKE &H1C, PEEK(&H1A)
DEF SEG
Logged
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #5 on: March 18, 2004, 03:05:11 PM »

Does this not do the same job?
Code:
dmy$ = INKEY$
Logged
Aaron
Member
*
Posts: 35


« Reply #6 on: March 18, 2004, 03:48:14 PM »

dark_prevail-I can't do that because I'm using RelKey

TheBigBasicQ-I'm already doing that

Rel-I have already turned RelKey on outside the sub, should I turn it on a second time? I don't want to turn it off at the end of my sub because then it won't be available for my main game loop.


All that aside, I am probably going to be changing the way this works and incoporating it into the main game engine, so it won't matter too much then.

Edit: No worries, I figured it out; the problem was in the implementation of RelKey, not in RelKey itself.
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #7 on: March 19, 2004, 12:55:16 AM »

Cool!!!!
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
barok
Na_th_an
*****
Posts: 1727


How about a tasty lead sandwich?


« Reply #8 on: March 21, 2004, 12:00:09 PM »

Aaron: yes, it will work.  when you assign a key with inkey$, it will clear the keyboard buffer.
Logged

Jumping Jahoolipers!
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!