Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Key control in QB is so slow!!!
#1
Hi!
I am trying to rotate 4 pixels, but when I press any rotation key, it doesn't work allways, its like wait about 1/2 sec. Same when I try to make 3D rotation controlled by keys! Here is the code:

Code:
DECLARE SUB DrawCube (rot)
SCREEN 13
        angle = 0

        rotate = 180
        DO
        K$ = INKEY$

                SELECT CASE K$

        CASE "a":
                rotate = rotate + 1
                CALL DrawCube(rotate)
        CASE "b":
                rotate = rotate - 1
                        CALL DrawCube(rotate)
                        END SELECT
          LOOP UNTIL INKEY$ = CHR$(27)

SUB DrawCube (rot)
CLS
DIM c!(360), s!(360)
DIM x(3), y(3), x2(3), y2(3)
FOR i = 1 TO 360
c!(i) = COS(i * 3.14 / 180)
s!(i) = SIN(i * 3.14 / 180)
NEXT

        x(0) = 20
        y(0) = 20

        x(1) = -20
        y(1) = 20

        x(2) = 20
        y(2) = -20

        x(3) = -20
        y(3) = -20

        FOR i = 0 TO 3
        x2(i) = x(i) * c!(rot) + y(i) * s!(rot)
        y2(i) = y(i) * c!(rot) - x(i) * s!(rot)
          
           IF rot = 360 THEN rot = 0
         '  IF rot = 0 THEN rot = 359
           CIRCLE (x2(i) + 100, y2(i) + 100), 3, 10

                

        NEXT

END SUB

What is wrong, it works so slooow!!! Please help me! Thank you![/u]
Reply
#2
I tried running your program but saw just a black screen. Does this code run ok on your computer?

[syntax="QBASIC"]'SQUARE ROTATION PROGRAM. THIS CODE DRAWS A SQUARE AT THE MIDDLE OF THE
'SCREEN WHICH CAN BE ROTATED USING THE , AND . KEYS
'
' --Written 11/10/2004 by mb

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Subroutine declarations.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DECLARE SUB DrawSquare (Vertex() AS ANY, Angle!, DrawColor%)

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'A vertex is an x-coordinate and a y-coordinate.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
TYPE VertexType
x AS SINGLE
y AS SINGLE
END TYPE

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Create an array of four vertices and read some data into them for a square.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DIM Vertex(1 TO 4) AS VertexType
FOR i% = 1 TO 4
READ Vertex(i%).x, Vertex(i%).y
NEXT i%

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Set graphics screen mode 320x200.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SCREEN 13

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Set the original angle of rotation to zero.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Angle! = 0

DO
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Draw the square in white.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DrawSquare Vertex(), Angle!, 15

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Get a keystroke
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DO
k$ = UCASE$(INKEY$)
LOOP UNTIL k$ <> ""

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Draw the square in black.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DrawSquare Vertex(), Angle!, 0

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'React to the keystroke
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SELECT CASE k$
CASE ","
Angle! = Angle! + 1
CASE "."
Angle! = Angle! - 1
END SELECT

LOOP UNTIL k$ = CHR$(27)

SYSTEM

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'here is the data for our square.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
' x-coord, y-coord
DATA -20, -20
DATA 20, -20
DATA 20, 20
DATA -20, 20

SUB DrawSquare (Vertex() AS VertexType, Angle!, DrawColor%)

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Convert the Angle! into radians so we can perform SIN() and COS()
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Radians! = Angle! * (3.14159# / 180)

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Create a temporary array to hold the rotated vertices.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DIM RotVtx(1 TO 4) AS VertexType

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Fill the RotVtx() array with the rotated coordinates.
'Use (160,100) as your central value since it is the screen center.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
FOR i% = 1 TO 4
RotVtx(i%).x = 160 + (Vertex(i%).x * COS(Radians!) - Vertex(i%).y * SIN(Radians!))
RotVtx(i%).y = 100 - (Vertex(i%).y * COS(Radians!) + Vertex(i%).x * SIN(Radians!))
NEXT i%

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Draw lines connecting the four rotated vertices.
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
LINE (RotVtx(1).x, RotVtx(1).y)-(RotVtx(2).x, RotVtx(2).y), DrawColor%
LINE (RotVtx(2).x, RotVtx(2).y)-(RotVtx(3).x, RotVtx(3).y), DrawColor%
LINE (RotVtx(3).x, RotVtx(3).y)-(RotVtx(4).x, RotVtx(4).y), DrawColor%
LINE (RotVtx(4).x, RotVtx(4).y)-(RotVtx(1).x, RotVtx(1).y), DrawColor%

END SUB[/syntax]
Reply
#3
That's not a bug, that's a feature. Wink Really. Notice how when you hold a key down, it pauses for a second before repeating that letter over and over? That's there to prevent typists from repeating letters accidentally.

There's ways to change the 'wait time', but 1) that would alter your use of other programs and 2) I don't know how to do it in DOS/QBASIC. I suggest finding another keyhandler; an ASM one, preferably. A quick google search with qbasic, asm, keyhandler in the query should turn something up. Milo Sedlacek, the creator of Monospace, is famed for making one that allows multiple keypresses (Multikey) as well.

--pr0gger
size=9]"To announce that there must be no criticism of the president, or that we are to stand by the president, right or wrong, is not only unpatriotic and servile, but is morally treasonable to the American public." -- Theodore Roosevelt[/size]
Reply
#4
Milo's magic at http://faq.qbasicnews.com/?blast=MiloSed...ardHandler
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#5
Here's my pure QB, multi-key handler...
I wrote this routine a whilr back, and just now added alot
of comments to it. You can kinda call it a, code-tutorial Smile


Cya,

Nemesis


P.S...

Code:
'''
' PQBMKEYS v1.1, a pure QBASIC, multi-keyboard handling routine!
'
' (C)opyright 2004, Pure QB Innovations
'
' If you have any questions or comments concerning these routines,
' then please contact me at... cmlarosa24@aol.com
'
' THIS PROGRAM MAY BE DISTRIBUTED FREELY AS FREEWARE SOFTWARE
' AS LONG AS ANY PART OF THIS FILE IS NOT ALTERED IN ANY WAY.
' IF YOU DO WISH TO USE THESE ROUTINES IN YOUR OWN PROGRAMS
' THEN PLEASE GIVE CREDIT TO THE AUTHOR -=Mario LaRosa=-
'
'''
'
'...Define all Variables as Integers unless assigned otherwise...
'
DEFINT A-Z
'
'...Dimension KBD Variable Array for storing Keyboard Data...
'
DIM KBD(127)
'
'...Assign Variables for Box Coordinates and Box Color...
'
BoxX = 159: BoxY = 99: BoxColour = 1
'
'...Initialize VGA Mode; (320x200x256)...
'
SCREEN 13
'
'...Set Segment Address...
'
DEF SEG = &H0
'
'...Main Program Loop...
'
DO
'
'...Reset Keyboard Buffer...
'
POKE &H41C, PEEK(&H41A)
'
'...Numeric Key Lock off...
'
POKE &H417, 0
'
'...Assign KBP Variable to Keyboard Port Status...
'
KBP = INP(&H60)
'
'...Assign KBD Array according to Keyboard Key States...
'
IF (KBP AND &H80) THEN
  KBD(KBP XOR &H80) = FALSE
ELSE
  KBD(KBP) = NOT FALSE
END IF
'
'...Manipulate Box Variables according to KBD Array Data...
'
IF ABS(TIMER - T!) >= (1 / 8) THEN      'Execute every 1/8 seconds.
  T! = TIMER                             'Assign T! Variable to TIMERS value.
  IF KBD(57) THEN                        'Check if Space Bar pressed.
   BoxColour = BoxColour + 1             'Increment BoxColour Variable.
   IF BoxColour > 15 THEN BoxColour = 1  'Keep BoxColour between 1 and 15.
  END IF
END IF
'
IF KBD(80) THEN                'Check if Down Arrow pressed
  BoxY = BoxY + 1               'Update Box Cordinates.
  IF BoxY > 199 THEN BoxY = 199 'Keep Box on Screen.
END IF
'
IF KBD(75) THEN                'Check if Left Arrow pressed.
  BoxX = BoxX - 1               'Update Box Cordinates.
  IF BoxX < -11 THEN BoxX = -11 'Keep Box on Screen.
END IF
'
IF KBD(77) THEN                'Check if Right Arrow pressed.
  BoxX = BoxX + 1               'Update Box Cordinates.
  IF BoxX > 319 THEN BoxX = 319 'Keep Box on Screen.
END IF
'
IF KBD(72) THEN                'Check if Up Arrow pressed.
  BoxY = BoxY - 1               'Update Box Cordinates.
  IF BoxY < -11 THEN BoxY = -11 'Keep Box on Screen.
END IF
'
'...Display Status of Arrow Key States...
'
LOCATE 1, 1: PRINT "Up:   "; KBD(72)
LOCATE 2, 1: PRINT "Down: "; KBD(80)
LOCATE 3, 1: PRINT "Right:"; KBD(77)
LOCATE 4, 1: PRINT "Left: "; KBD(75)
LOCATE 5, 1: PRINT "Space:"; KBD(57)
'
'...Erase 14x14 area around Box...
'
LINE (BoxX - 1, BoxY - 1)-(BoxX + 12, BoxY + 12), 0, B
'
'...Place 12x12 Box at Box Variable Cordinates...
'
LINE (BoxX, BoxY)-(BoxX + 11, BoxY + 11), BoxColour, BF
'
'...Wait for Monitor Vsync...
'
WAIT &H3DA, 8
'
'...Loop until Escape Key pressed...
'
LOOP UNTIL KBD(1)
'
'...Quit Program after Escape Key pressed...
'
SYSTEM
'
Reply
#6
I note that if you double-tap an arrow key, it can lock the value at -1.

*peace*

Meg.
Reply
#7
This is not a multi key handler but a simple one Big Grin.
Code:
CLS

DO
   KeyPress$ = INKEY$

   IF LEN(KeyPress$) = 2 THEN
      ScanCode% = ASC(RIGHT$(KeyPress$, 1))
   END IF

   IF KeyPress$ = CHR$(27) THEN LOCATE 1, 1: PRINT "You hit the ESC key!": END

   SELECT CASE ScanCode%
   CASE 72
      LOCATE 1, 1: PRINT "UP ARROW    "
   CASE 75
      LOCATE 1, 1: PRINT "LEFT ARROW  "
   CASE 77
      LOCATE 1, 1: PRINT "RIGHT ARROW "
   CASE 80
      LOCATE 1, 1: PRINT "DOWN ARROW  "
   CASE 59
      LOCATE 1, 1: PRINT "F1 Key      "
   END SELECT
   ScanCode% = 0
LOOP
Reply
#8
hmn... this is what I used before I used multi-key handlers:
Code:
DO
   K$ = INKEY$
   KeyPress% = 0
   IF K$ <> "" THEN KeyPress% = CVI(K$ + CHR$(0))
  
   IF KeyPress% <> 0 THEN PRINT "You pressed key"; KeyPress%
LOOP UNTIL KeyPress% = 27
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)