; Requires Extended mode. Windows, macOS, iOS and web. Include "GamepadConstants.bb" Graphics 960,640,32,2 SetBuffer BackBuffer() AppTitle "Controller playground" x#=480 : y#=360 : aim#=0 targetX#=700 : targetY#=360 score=0 : port=-1 : flashUntil=0 previousTime=MilliSecs() verify=Instr(CommandLine$(),"--verify")>0 frames=0 While Not KeyHit(1) now=MilliSecs() dt#=Float(now-previousTime)/1000.0 If dt>0.05 Then dt=0.05 previousTime=now If port>=0 Then If Not JoyConnected(port) Then port=-1 EndIf If port<0 Then For candidate=0 To JoyPorts()-1 If JoyGamepad(candidate) Then port=candidate GamepadDeadZone 0.15,0.05,port Exit EndIf Next EndIf dx#=KeyDown(205)-KeyDown(203) dy#=KeyDown(208)-KeyDown(200) turn#=KeyDown(32)-KeyDown(30) speed#=200 fire=KeyHit(57) If port>=0 Then dx=dx+GamepadAxis(PAD_LEFT_X,port) dy=dy+GamepadAxis(PAD_LEFT_Y,port) speed=speed*(1+GamepadAxis(PAD_RIGHT_TRIGGER,port)*2-GamepadAxis(PAD_LEFT_TRIGGER,port)*0.75) ax#=GamepadAxis(PAD_RIGHT_X,port) : ay#=GamepadAxis(PAD_RIGHT_Y,port) If ax*ax+ay*ay>0.04 Then aim=ATan2(ay,ax) fire=fire Or GamepadHit(PAD_SOUTH,port) EndIf aim=aim+turn*180*dt x=x+dx*speed*dt : y=y+dy*speed*dt If x<20 Then x=20 If x>940 Then x=940 If y<220 Then y=220 If y>620 Then y=620 If fire Then flashUntil=now+120 tx#=targetX-x : ty#=targetY-y along#=tx*Cos(aim)+ty*Sin(aim) across#=Abs(tx*Sin(aim)-ty*Cos(aim)) If along>0 And along<300 And across<24 Then score=score+1 targetX=Rand(40,920) : targetY=Rand(240,600) If port>=0 Then If JoyRumbleSupported(port) Then accepted=JoyRumble(0.4,0.7,120,port) EndIf EndIf EndIf Cls Color 235,240,255 Text 20,16,"Move: left stick / arrows. Aim: right stick / A,D. Fire: south button / Space." Text 20,40,"Left trigger: precision speed. Right trigger: boost. Esc: quit." Text 20,64,"Shoot the gold target. Score: "+score+" Connected controllers: "+JoyCount() If port>=0 Then Text 20,92,"Port "+port+": "+JoyName$(port) Text 20,116,"Battery: "+JoyBattery(port)+"% (-1 unknown/wired) Vibration: "+JoyRumbleSupported(port) Text 20,140,"Raw X,Y: "+JoyX(port)+", "+JoyY(port)+" POV: "+JoyHat(port) Else Text 20,92,"Connect a gamepad and press a button. Keyboard controls work immediately." Text 20,116,"Web: click this game first; controllers may appear only after a pad button press." EndIf Color 60,75,95 : Line 0,195,960,195 Color 255,200,65 : Oval targetX-24,targetY-24,48,48,False Color 65,205,255 : Oval x-12,y-12,24,24,True Line x,y,x+Cos(aim)*35,y+Sin(aim)*35 If now=4 Then Exit Wend If port>=0 Then accepted=JoyRumble(0,0,0,port) End