Q(uick)BASIC Function: STICK

Quick View

STICK

A device I/O function that returns the x and y coordinates of the two joysticks

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • STICK(n%)
Description/Parameter(s)
n% Indicates the coordinate returned:
n%Returns
0x coordinate of joystick A
1y coordinate of joystick A
2x coordinate of joystick B
3y coordinate of joystick B
  • You must call STICK(0) before STICK(1), STICK (2), or STICK(3). STICK(0) records the current coordinates.
Example
Temp% = STICK(0) PRINT STICK(2), STICK(3)
Syntax
  • STICK(n)
Description/Parameter(s)

The argument n is a numeric expression whose value is an unsigned integer in the range 0 to 3:

Argument Value Returned
0 The x coordinate of joystick A.
1 The y coordinate of joystick A when STICK(0) was last called.
2 The x coordinate of joystick B when STICK(0) was last called.
3 The y coordinate of joystick B when STICK(0) was last called.

The x and y coordinates have a range of 1 to 200. You must use STICK(0) before you use STICK(1), STICK(2), or STICK(3). STICK(0) not only returns the x coordinate of joystick A, it also records the other joystick coordinates. These recorded coordinates are returned by calling STICK(1)-STICK(3).

Example

The following program prints the coordinates of joystick B:

'*** Programming example for STICK function *** ' Do not attempt to run this program unless a joystick is installed TEMP = STICK(0) PRINT STICK(2), STICK(3)
Syntax
  • STICK(n%)
Description/Parameter(s)
n% A numeric expression whose value is an unsigned integer from 0 to 3 that indicates which joystick coordinate value to return:
n%Returns
0indicates the joystick A x coordinate.
1indicates the joystick A y coordinate.
2indicates the joystick B x coordinate.
3indicates the joystick B y coordinate.

Usage Notes

  • You must use STICK(0) before you use STICK(1), STICK(2), or STICK(3) because STICK(0) not only returns the x coordinate of joystick A, but also records the other joystick coordinates. These recorded coordinates are returned by calling STICK(1), STICK(2), or STICK(3).
Example

This example uses the EVENT and STRIG statements to enable joystick event trapping for two buttons on each of two joysticks. The ON STRIG statement passes control to an event-handling routine when a button is pressed. The STRIG function is used to determine which button was pressed, and the STICK function returns the position of both joysticks.

CLS 'Turn on event processing. EVENT ON 'Enable the upper and lower buttons on both joysticks. STRIG(0) ON 'Lower button, joystick A STRIG(2) ON 'Lower button, joystick B STRIG(4) ON 'Upper button, joystick A STRIG(6) ON 'Upper button, joystick B 'Set up joystick event processing for each button. ON STRIG(0) GOSUB JoyButtonHandler ON STRIG(2) GOSUB JoyButtonHandler ON STRIG(4) GOSUB JoyButtonHandler ON STRIG(6) GOSUB JoyButtonHandler LOCATE 22, 6 PRINT "Press a button on either joystick to see a complete joystick report." LOCATE 23, 20 PRINT "Press ANY keyboard key to end the program." 'Infinite loop waiting for a Joystick event or keyboard key input. DO LOOP WHILE INKEY$ = "" WrapItUp: CLS STRIG(0) OFF STRIG(2) OFF STRIG(4) OFF STRIG(6) OFF END JoyButtonHandler: 'Print a label on screen. LOCATE 10, 14: PRINT "Joystick A" LOCATE 10, 45: PRINT "Joystick B" LOCATE 22, 1: PRINT STRING$(80, 32) DO 'Button 1, Joystick A. IF STRIG(1) THEN ButtonStatus$ = "DOWN" ELSE ButtonStatus$ = "UP " END IF EVENT OFF LOCATE 16, 10: PRINT "Button 1 is "; ButtonStatus$ EVENT ON 'Button 1, Joystick B. IF STRIG(3) THEN ButtonStatus$ = "DOWN" ELSE ButtonStatus$ = "UP " END IF EVENT OFF LOCATE 16, 42: PRINT "Button 1 is "; ButtonStatus$ EVENT ON 'Button 2, Joystick A. IF STRIG(5) THEN ButtonStatus$ = "DOWN" ELSE ButtonStatus$ = "UP " END IF EVENT OFF LOCATE 18, 10: PRINT "Button 2 is "; ButtonStatus$ EVENT ON 'Button 2, Joystick B. IF STRIG(7) THEN ButtonStatus$ = "DOWN" ELSE ButtonStatus$ = "UP " END IF EVENT OFF LOCATE 18, 42: PRINT "Button 2 is "; ButtonStatus$ EVENT ON GOSUB UpdateXY LOOP WHILE INKEY$ = "" RETURN WrapItUp UpdateXY: EVENT OFF LOCATE 12, 10: PRINT USING "X Position = ###"; STICK(0) LOCATE 14, 10: PRINT USING "Y Position = ###"; STICK(1) LOCATE 12, 42: PRINT USING "X Position = ###"; STICK(2) LOCATE 14, 42: PRINT USING "Y Position = ###"; STICK(3) EVENT ON RETURN