Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Joystick Detection and Interaction
#1
A small little demo to help highlight how one would work with the joystick in QB64:

Code: (Select All)
D = _DEVICES 'Find the number of devices on someone's system
'1 is the keyboard
'2 is the mouse
'3 is the joystick
'unless someone has a strange setup with multiple mice/keyboards/ect...
'In that case, you can use _DEVICE$(i) to look for "KEYBOARD", "MOUSE", "JOYSTICK", if necessary.
'I've never actually found it necessary, but I figure it's worth mentioning, just in case...


DIM Button(_LASTBUTTON(3)) ' number of buttons on the joystick
DIM Axis(_LASTAXIS(3)) 'number of axis on the joystick

DO


    DO

        'This following little segment of code gets the joystick status for us
        'The reason this is inside a DO...LOOP structure as I've created it,
        'is so that my joystick's axis won't generate any form of lag for
        'my program as I scroll them around to generate positive/negative values.

        IF _DEVICEINPUT = 3 THEN 'this says we only care about joystick input values
            FOR i = 1 TO _LASTBUTTON(3) 'this is a loop to check all the buttons
                IF _BUTTONCHANGE(i) THEN Button(i) = NOT Button(i) 'and this changes my button array to indicate if a button is up or down currently.
            NEXT
            FOR i = 1 TO _LASTAXIS(3) 'this loop checks all my axis
                'I like to give a little "jiggle" resistance to my controls, as I have an old joystick
                'which is prone to always give minute values and never really center on true 0.
                'A value of 1 means my axis is pushed fully in one direction.
                'A value greater than 0.1 means it's been partially pushed in a direction (such as at a 45 degree diagional angle).
                'A value of less than 0.1 means we count it as being centered. (As if it was 0.)
                IF ABS(_AXIS(i)) <= 1 AND ABS(_AXIS(i)) >= .1 THEN Axis(i) = _AXIS(i) ELSE Axis(i) = 0
            NEXT
        ELSE EXIT DO
        END IF
    LOOP

    'And below here is just the simple display routine which displays our values.
    'If this was for a game, I'd choose something like Axis(1) = -1 for a left arrow style input,
    'Axis(1) = 1 for a right arrow style input, rather than just using _KEYHIT or INKEY$.


    CLS
    FOR i = 1 TO _LASTBUTTON(3) 'A loop for each button
        PRINT "BUTTON "; i; ": "; Button(i) 'display their status to the screen
    NEXT
    FOR i = 1 TO _LASTAXIS(3) 'A loop for each axis
        PRINT "Axis "; i; ": "; Axis(i) 'display their status to the screen
    NEXT

    _LIMIT 30
LOOP UNTIL _KEYHIT = 27 'ESCAPE to quit

SYSTEM 'And that's all it is to it!
Reply




Users browsing this thread: 1 Guest(s)