I've seen a few code samples that simply read the Xbox 360 controller, but none provide practical applications. There are a lot of unanswered questions on how to apply these values to an actual game.
I've included a code snippet that uses the Xbox 360 controller to move a circle around the screen in all directions. It may be a better starting point in deciphering this controller.
I noticed that my controller's x,y values on the left stick are never the same when in an idle position, so I had to come up with a range of x and y idle values (xlo, xhi, ylo, yhi). You may need to adjust these values for your controller. It would be great if someone would add a calibration routine.
gosub _init
REM **** Main Loop ****
do
gosub _xboxinput
gosub _dobtn
gosub _movecircle
sync
loop
REM **** End Main Loop ****
_init:
sync on
hide mouse
randomize timer()
rdius = 10 : oldrdius = 10
incr = 1
cspd = 1
mxrdius = 50
mnrdius = 10
x = 400 : y = 300
px = x : py = y
rgb1 = 255 : rgb2 = 255
rem control idle position ranges (calibration routine necessary for other controllers?)
xlo = -350 : xhi = 20
ylo = -300 : yhi = 135
strt1 = timer() : intvl = 15
trigger = -1
bumperlf = 0
bumperrt = 0
return
_movecircle:
ink 0,0
if (xj < xlo or xj > xhi) or (yj < ylo or yj > yhi) ' if stick moved
if xth then x = x + xth * cspd else if yth then y = y + yth * cspd else x = x + cspd * xs : y =y + cspd * ys
endif
circle px,py,oldrdius
if timer() > strt1 + intvl and trigger = -1 and bumperlf = 0 and bumperrt = 0
rdius = rdius + incr * 2
if (rdius > mxrdius) or (rdius < mnrdius)
incr = -incr
if (rdius > mxrdius)
rdius = mxrdius
else
rdius = mnrdius
endif
endif
strt1 = timer()
endif
ink rgb(0,rgb1,rgb2), rgb(0,rgb1,rgb2)
CIRCLE x,y,rdius
px = x : py = y : oldrdius = rdius
return
_xboxinput:
xj = JOYSTICK X() : yj = JOYSTICK Y()
xs = Sgn(xj) : ys = Sgn(yj) ' indicates x and y direction
xth = int(xj / 1000) : yth = int(yj / 1000) ' indicates if moving straight up/down (y) or left/right (x)
abtn = JOYSTICK FIRE A() : bbtn = JOYSTICK FIRE B()
tnew = sgn(JOYSTICK Z()) : bumperlf = JOYSTICK FIRE X(4) : bumperrt = JOYSTICK FIRE X(5)
remstart Unimplemented buttons in the program
xjt = JOYSTICK TWIST X() ' Right Stick X
yjt = JOYSTICK TWIST Y() ' Right Stick Y
lfire = JOYSTICK FIRE X(8) ' Left stick click
rfire = JOYSTICK FIRE X(8) ' Right stick click
strtbtn = JOYSTICK FIRE X(7) ' Start Button
bckbtn = JOYSTICK FIRE X(6) ' Back Button
xbtn = JOYSTICK FIRE C() ' X (Blue) Button
ybtn = JOYSTICK FIRE D() ' Y (Yellow) Button
dpad = JOYSTICK HAT ANGLE(0) ' D-Pad
remend
return
_dobtn:
if abtn = 1 then rgb1 = int(rnd(255)) + 1 : rgb2 = int(rnd(255)) + 1 ' A button - Randomly change circle's color
if bbtn = 1 then goto _endprg ' B button - End program
if tnew = 1 then trigger = 1 ' tnew = 1 = Left Trigger - pulse off
if tnew = -1 then trigger = -1 ' tnew = -1 = Right Trigger - pulse on
if bumperlf then rdius = mnrdius ' Left Bumper - minimizes circle
if bumperrt then rdius = mxrdius ' Right Bumper - maximizes circle
return
Function Sgn( NumValue as Float )
ValueSign as integer
if NumValue = 0
ValueSign = 0
else
if NumValue < 0
ValueSign = -1
else
ValueSign = 1
endif
endif
EndFunction ValueSign
_endprg:
end
No media required.
CSL