Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joystick's in QBasic

Status
Not open for further replies.

sandmann999

Programmer
Apr 28, 2001
52
US
Does anyone have some simple code that can allow me to use a joystick to control my game in QBasic? I know that's rather vague, but the whole idea isn't very complex so it doesn't really require a lot of explaining. Thanks for any help.
 
Here is some sample code I made when I first learned to use the joystick in QB

CLS
SCREEN 12
10 'Begining
A% = INP(&H201)
LOCATE 1, 1: PRINT "Current:"
LOCATE 1, 9: PRINT A%
'OUT &H201, B%
LOCATE 2, 1: PRINT "Base:"
LOCATE 2, 6: PRINT PEEK(0)
X = PEEK(1)
IF PEEK(1) = 1 THEN F = PEEK(0) - A%
LOCATE 3, 1: PRINT "Final:"
LOCATE 3, 7: PRINT F
IF F = 16 THEN LOCATE 4, 1: PRINT "Button#1"
IF F = 32 THEN LOCATE 4, 1: PRINT "Button#2"
IF F = 64 THEN LOCATE 4, 1: PRINT "Button#3"
IF F = 48 THEN LOCATE 4, 1: PRINT "Button#1 & Button#2"
IF F = 80 THEN LOCATE 4, 1: PRINT "Button#1 & Button#3"
IF F = 96 THEN LOCATE 4, 1: PRINT "Button#2 & Button#3"
IF F = 112 THEN LOCATE 4, 1: PRINT "Button#1 & Button#2 & Button#3"
IF F = 128 THEN LOCATE 4, 1: PRINT "Button#4"
IF F = 144 THEN LOCATE 4, 1: PRINT "Button#1 & Button#4"
IF F = 160 THEN LOCATE 4, 1: PRINT "Button#2 & Button#4"
IF F = 176 THEN LOCATE 4, 1: PRINT "Button#1 & Button#2 & Button#4"
IF F = 192 THEN LOCATE 4, 1: PRINT "Button#3 & Button#4"
IF F = 208 THEN LOCATE 4, 1: PRINT "Button#1 & Button#3 & Button#4"
IF F = 224 THEN LOCATE 4, 1: PRINT "Button#2 & Button#3 & Button#4"
IF F = 240 THEN LOCATE 4, 1: PRINT "Button#1 & Button#2 & Button#3 &Button#4"
PRINT " "; SPACE$(40)
IF X = 0 THEN GOTO 20
IF X = 1 THEN GOTO 10
20 'Store A% Value
POKE 0, A%
POKE 1, 1
GOTO 10
 
Hrmm...gave it a try but it didn't do anything. Is there anything special I have to do with the joystick settings to get it to work?

 
The joystick you are using is it plugged into the game port or usb - I've used this code with three different joysticks and it works

(all joysticks I used run on the game port)


%, 2004
 
I'm using one on the game port, (it configured in the windows setup) and then I have two joysticks wired in through the printer port. None of them do anything. I'll give it another go in the morning, but I'm not sure what to try. Are you using Windows XP? Or should that not make any difference?
 
Since you have a game port it should work. Of course, qb has this [screen shot] in it's HELP Files:


Code:
+------------------ HELP: STICK Function QuickSCREEN
¦  QuickSCREEN      Details      Example   Contents
¦---------------------------------------------------
¦STICK - a device I/O function that returns the x 
         and y coordinates of the two joysticks
¦                
¦Syntax          
¦  STICK(n)    
¦    > n, a numeric expression that has an unsigned 
       integer value between 0 and 3, tells the STICK
       function which one of four possible joystick
       coordinate values to return (see Details)    
¦         
+-------------------------------- Untitled 
¦stick      
¦

Under the EXAMPLE link it has this:

Code:
STICK Function Programming 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)
Does this help?
--MiggyD
 
Sorry, forgot to get back to you. No, unfortunately, none of the code did anything. The STICK function as well as the other code didn't work with any of my joysticks in any port. Could it be Windows XP? If not, any other ideas? Thanks.
 
You could always try visual basic - the code is mostly the same - you just have to get used to using controls which is easy once you get the hang of - one of the first apps I made in vb was one that you controlled the mouse with the joystick - I made it on a windows 98 machine - and it still works on my xp machine - I think you would really enjoy vb comared to qb

%, 2004
 
Oh. XP. Yep, that could be a problem. Have you looked inside your system to see if it (the game card) is pnp?

If so, the port addressing may differ than what QB was ment for.

Sorry I can't help you out any further. If you have a friend that has DOS or Win 9x/ME you can try the stick function. Here's an example you can paste into a bas to try to determine if it's working or not:

Code:
CLS
PRINT "joystick A: "
PRINT "joystick B: "
PRINT : PRINT "Press any key to exit"
DO WHILE T$ = ""
    LOCATE 1, 13
    PRINT STICK(0), STICK(1)
    LOCATE 2, 13
    PRINT STICK(2), STICK(3)
    IF INKEY$ <> "" THEN T$ = "x"
LOOP
END

Good Luck,
--MiggyD
 
Poke & Peek are memory commands - the command peeks into a location in the bios to check the status of the joystick - with xp this is not allowed - xp disallows this - is it a security feature I beleive it is the same on 2k also

with vb the way this is done is through the driver interface - using what is called a windows api (Application Protocal Interface) the api commands are built into dll file(s) - so if you ever wondered the main reason for dll(s) this is it - with the windows api's they for the most part stay they same - the api used to get the joysticks input is in NT,95,98,2K,ME,XP so this is one excellent reason to use a language that can interface with the windows api(s) - examples are:

Visual Basic
Visual C++



%, 2004
 
Got it to work in Windows 98. (I have both 98 and XP on my computer). It worked great, got a little dot to "fly" around the screen and shoot little bullets when I pressed the buttons. The analogue feature of the STICK command is pretty cool. Unfortunately, it doesn't work in XP and I'm not sure how to get it to. Still cool though.
 
And some game pads get tricky and use both stick and strig 1 & 2 for a single controller...

I had a 6 button controll pad that did that...

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Sandmann,

I'm on winME and in the control panel there's an joy stick icon called "Game Options". Have you looked in there? (control panel?)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top