Joystick position: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
No edit summary
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 6 users not shown)
Line 20:
For the purpose of this task, we assume that the joystick is calibrated and that the first joystick is being used. <br>
The task implementer could at their option provide a solution that includes a joystick selection facility, enabling the user to choose which joystick is to be used for this task.
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE lastStick=[255]
BYTE lastTrig=[255]
 
PROC DrawCross(BYTE s)
BYTE size=[5]
CARD x
BYTE y
 
IF s>=9 AND s<=11 THEN
x=size
ELSEIF s>=5 AND s<=7 THEN
x=159-size
ELSE
x=79
FI
 
IF s=6 OR s=10 OR s=14 THEN
y=size
ELSEIF s=5 OR s=9 OR s=13 THEN
y=79-size
ELSE
y=39
FI
Plot(x-size,y)
DrawTo(x+size,y)
Plot(x,y-size)
DrawTo(x,y+size)
RETURN
 
PROC UpdateStatus(BYTE currStick,currTrig)
IF currStick#lastStick THEN
Color=0 DrawCross(lastStick)
Color=1 DrawCross(currStick)
lastStick=currStick
FI
 
IF currTrig#lastTrig THEN
Print("Button pressed: ")
IF currTrig THEN
PrintE("no ")
ELSE
PrintE("yes")
FI
Put(28) ;move cursor up
lastTrig=currTrig
FI
RETURN
 
PROC Main()
BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6,
CRSINH=$02F0 ;Controls visibility of cursor
BYTE currStick,currTrig
 
Graphics(7)
Color=1
COLOR1=$0C
COLOR2=$02
CRSINH=1 ;hide cursor
 
DO
currStick=Stick(0)
currTrig=STrig(0)
UpdateStatus(currStick,currTrig)
UNTIL CH#$FF
OD
CH=$FF
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Joystick_position.png Screenshot from Atari 8-bit computer]
 
=={{header|Applesoft BASIC}}==
<langsyntaxhighlight ApplesoftBasiclang="applesoftbasic">100 GOSUB 400
110 P2 = PDL(2) : IF P2 <> O2 THEN O2 = P2 : VTAB 23 : HTAB 33 : PRINT P2; TAB(37);
120 B2 = FNB(2) : IF B2 <> N2 THEN N2 = B2 : VTAB 24 : HTAB 15 : PRINT P$(B2);
Line 59 ⟶ 131:
550 ROT = 0 : SCALE = 7 : XDRAW 1 AT X1, Y1
560 O0 = -1 : O1 = O0 : O2 = O0 : O3 = O0
570 RETURN</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 67 ⟶ 139:
 
[http://i.imgur.com/KsbrpK4.png Image link]
<langsyntaxhighlight AutoHotkeylang="autohotkey">; Uncomment if Gdip.ahk is not in your standard library
; #Include, Gdip.ahk
 
Line 285 ⟶ 357:
Exit:
Gdip_Shutdown(pToken)
ExitApp</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> VDU 23,22,512;512;8,16,16,0
VDU 5
Line 311 ⟶ 383:
VDU 10,13
NEXT
ENDPROC</langsyntaxhighlight>
 
 
 
=={{header|C}}==
<langsyntaxhighlight lang="cpp">#include <stdio.h>
#include <stdlib.h>
 
Line 370 ⟶ 442:
system("clear");
return 1;
}</langsyntaxhighlight>
 
{{out}}
Line 385 ⟶ 457:
This program maps the values from both registers to bits 0 through 4 of a single byte so that logic is evaluated identical to the C-64 example below.
 
<langsyntaxhighlight lang="commodorebasicv2">5 rem joystick - commodore vic-20 (expanded)
6 rem for rosetta code
10 print chr$(147);:poke 37154,peek(37154) and 127
Line 408 ⟶ 480:
80 if (ox=x) and (oy=y) then goto 100
85 poke sc+ox+oy*22,32
100 goto 25</langsyntaxhighlight>
 
===Commodore 64===
Line 414 ⟶ 486:
On the Commodore 64, both joystick ports can be read from registers on the CIA#1 6526 chip. This will report binary input from the joystick control ports in bits 0 through 4 (four directions plus a single fire or action button.) Control Port 1 is read from location 56321 and Control Port 2 is read from location 56320.
 
<langsyntaxhighlight lang="commodorebasicv2">5 rem joystick - commodore 64
6 rem for rosetta code
8 rem black bkg, white text, dk. grey border
Line 437 ⟶ 509:
85 poke sc+x+y*40,c:if (ox=x) and (oy=y) then goto 100
90 poke sc+ox+oy*40,32
100 goto 20</langsyntaxhighlight>
 
===Commodore Plus/4===
Line 443 ⟶ 515:
The Commodore Plus/4 has the JOY(''n'') function available in BASIC v3.5 to read the position of a joystick plugged into control port ''n''. The return value is a number 1 through 8 corresponding to each unique direction in a clockwise order, beginning with "up" returning a value of "1". Bit 7 of the return value will report the status of the fire button.
 
<langsyntaxhighlight lang="gwbasic">5 rem joystick - commodore plus/4
6 rem for rosetta code
8 rem black bkg, white text, dk. grey border
Line 469 ⟶ 541:
85 poke sc+x+y*40,c:if (ox=x) and (oy=y) then goto 100
90 poke sc+ox+oy*40,32
100 goto 20</langsyntaxhighlight>
 
===Commodore 128===
Line 475 ⟶ 547:
BASIC 7.0 retains the same functionality of BASIC 3.5 on the Plus/4 for reading the joystick port(s). The C128 can take advantage of dual screen output.
 
<langsyntaxhighlight lang="gwbasic">5 rem joystick - commodore 128
6 rem for rosetta code
8 rem black bkg, white text, dk. grey border
Line 501 ⟶ 573:
85 poke sc+x+y*40,c:if (ox=x) and (oy=y)then goto 100
90 poke sc+ox+oy*40,32
100 goto 20</langsyntaxhighlight>
=={{header|Delphi}}==
{{libheader|mmSystem}}
Form application version.
<syntaxhighlight lang="delphi">
<lang Delphi>
unit uMain;
 
Line 578 ⟶ 650:
 
end.
</syntaxhighlight>
</lang>
Form resource:
<syntaxhighlight lang="delphi">
<lang Delphi>
object Form1: TForm1
Left = 0
Line 617 ⟶ 689:
end
end
</syntaxhighlight>
</lang>
{{out}}
Result preview [https://ibb.co/tc0xH3q]
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Screen 12
 
Dim As Single x, y
Dim As Integer buttons, result
 
Const JoystickID = 0
 
'This line checks to see if the joystick is ok.
If Getjoystick(JoystickID, buttons, x, y) Then
Print "Joystick doesn't exist or joystick error."
Print !"\nPress any key to continue."
Sleep
End
End If
 
Do
result = Getjoystick(JoystickID, buttons, x, y)
Locate 1,1
Print ; "result:"; result; " x:"; x; " y:"; y; " Buttons:"; buttons, "", "", ""
'This tests to see which buttons from 1 to 27 are pressed.
For a As Integer = 0 To 26
If (buttons And (1 Shl a)) Then
Print "Button "; a; " pressed. "
Else
Print "Button "; a; " not pressed."
End If
Next a
Loop</syntaxhighlight>
 
 
Line 625 ⟶ 730:
{{libheader|termbox-go}}
{{libheader|joystick(go)}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 727 ⟶ 832:
}
}
}</langsyntaxhighlight>
 
=={{header|GUISS}}==
Graphical User Interface Support Script only makes use of installed applications. So for this task, we use the joystick calibration routine, which shows a joystick position indicator:
 
<langsyntaxhighlight lang="guiss">Start,Control Panel, Game Controllers, List:installed controllers,Click:Joystick,
Button:Properties,Button:Test</langsyntaxhighlight>
 
=={{header|Haskell}}==
Half-solution of the problem, exhibits X and Y coordinates of the joystick; works on Windows (Haskell Platform):
 
<langsyntaxhighlight Haskelllang="haskell">import qualified Graphics.UI.GLFW as GLFW -- cabal install GLFW-b
import Graphics.Win32.Key
import Control.Monad.RWS.Strict (liftIO)
Line 760 ⟶ 865:
(Just (x:y:_)) -> (-y, x)
_ -> ( 0, 0)
</syntaxhighlight>
</lang>
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 CLEAR SCREEN
110 DO
120 LET J=JOY(0) OR JOY(1) OR JOY(2)
Line 772 ⟶ 877:
170 IF J BAND 8 THEN PRINT "up ";
180 IF J BAND 16 THEN PRINT "fire ";
190 LOOP</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using CSFML.LibCSFML, Gtk.ShortNames, Colors, Graphics, Cairo
 
#------------ input code ----------------------#
Line 851 ⟶ 956:
yield()
end
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb"> 'disable text window
nomainwin
 
Line 925 ⟶ 1,030:
close #m
unloadbmp "plus"
end</langsyntaxhighlight>
 
=={{header|Locomotive Basic}}==
<langsyntaxhighlight lang="locobasic">10 MODE 1:BORDER 14:x=320:y=200:d=1
20 a=JOY(0) ' read state of first joystick
30 IF d THEN q$="*" ELSE q$=" "
Line 940 ⟶ 1,045:
110 IF a<16 THEN LOCATE 1,1:PRINT " ":PRINT " "
120 MOVE x-8,y+8:TAG:PRINT "X";:TAGOFF
130 GOTO 20</langsyntaxhighlight>
 
Output (this version supports drawing with the cursor):
Line 947 ⟶ 1,052:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Slider2D[Dynamic[ControllerState[{"X", "Y"}]], ImageSize -> {500, 500}]</langsyntaxhighlight>
 
=={{header|OCaml}}==
Line 954 ⟶ 1,059:
{{libheader|ocaml-sfml}}
 
<langsyntaxhighlight lang="ocaml">let remove x = List.filter (fun y -> y <> x)
let buttons_string b =
String.concat " " (List.map string_of_int b)
Line 1,007 ⟶ 1,112:
proc_event joyd
in
loop ((0.0, 0.0), [])</langsyntaxhighlight>
 
Run with the command:
Line 1,013 ⟶ 1,118:
 
=={{header|Phix}}==
Windows only, taken from the [http://phix.x10.mx/pmwiki/pmwiki.php?n=Main.JoystickPadLibrary JoystickPadLibrary on PCAN]
 
First, joy.e:
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>-- Joystick library for Euphoria (Windows)
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
-- /Mic, 2002
<span style="color: #000080;font-style:italic;">-- Joystick library for Euphoria (Windows)
--
-- /Mic, 2002
-- integer joy_init()
--
-- returns the number of joysticks attached to the computer
-- integer joy_init()
--
-- returns the number of joysticks attached to the computer
-- sequence joy_get_state(integer joy_num)
--
-- returns the current state of joystick #joy_num (can be either 1 or 2).
-- sequence joy_get_state(integer joy_num)
-- the format of the return sequence is:
-- returns the current state of joystick #joy_num (can be either 1 or 2).
-- {X_direction, Y_direction, Z_direction, buttons}
-- the format of the return sequence is:
-- the X,Y and Z directions have 3 possible values; 0 (negative), 32768 (neutral) or 65535 (positive)
-- {X_direction, Y_direction, Z_direction, buttons}
-- the buttons' status are represented by a single bit (button up, button down). e.g. to get the status
-- the X,Y and Z directions have 3 possible values; 0 (negative), 32768 (neutral) or 65535 (positive)
-- of button #3 on joystick #1 you'd use:
-- the buttons' status are represented by a single bit (button up, button down). e.g. to get the status
-- sequence state
-- of button #3 on joystick #1 you'd use:
-- state = joy_get_state(1)
-- ifsequence and_bits(state[4],4) then ... end if
-- state = joy_get_state(1)
--
-- if and_bits(state[4],4) then ... end if
include dll.e
--</span>
include machine.e
<span style="color: #008080;">include</span> <span style="color: #000000;">dll</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
 
<span style="color: #008080;">include</span> <span style="color: #000000;">machine</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
constant joyinfo = allocate(32)
atom winmm
<span style="color: #008080;">constant</span> <span style="color: #000000;">joyinfo</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">allocate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">32</span><span style="color: #0000FF;">)</span>
integer joyGetNumDevs,joyGetPos
<span style="color: #004080;">atom</span> <span style="color: #000000;">winmm</span>
 
<span style="color: #004080;">integer</span> <span style="color: #000000;">joyGetNumDevs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">joyGetPos</span>
winmm = open_dll("winmm.dll")
if (winmm <= 0) then
<span style="color: #000000;">winmm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">open_dll</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"winmm.dll"</span><span style="color: #0000FF;">)</span>
puts(1,"Unable to open winmm.dll")
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">winmm</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
abort(0)
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Unable to open winmm.dll"</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #7060A8;">abort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
joyGetNumDevs = define_c_func(winmm,"joyGetNumDevs",{},C_UINT)
joyGetPos = define_c_func(winmm,"joyGetPos",{C_INT,C_POINTER},C_INT)
<span style="color: #000000;">joyGetNumDevs</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">winmm</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joyGetNumDevs"</span><span style="color: #0000FF;">,{},</span><span style="color: #000000;">C_UINT</span><span style="color: #0000FF;">)</span>
if (joyGetNumDevs<0) or (joyGetPos<0) then
<span style="color: #000000;">joyGetPos</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">define_c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">winmm</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joyGetPos"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">C_INT</span><span style="color: #0000FF;">,</span><span style="color: #000000;">C_POINTER</span><span style="color: #0000FF;">},</span><span style="color: #000000;">C_INT</span><span style="color: #0000FF;">)</span>
puts(1,"Unable to link functions")
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">joyGetNumDevs</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">joyGetPos</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
abort(0)
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Unable to link functions"</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #7060A8;">abort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
global function joy_init()
integer joy1Attached,joy2Attached
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">joy_init</span><span style="color: #0000FF;">()</span>
integer numDevs = c_func(joyGetNumDevs,{})
<span style="color: #004080;">integer</span> <span style="color: #000000;">joy1Attached</span><span style="color: #0000FF;">,</span><span style="color: #000000;">joy2Attached</span>
if numDevs=0 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">numDevs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">joyGetNumDevs</span><span style="color: #0000FF;">,{})</span>
return 0
<span style="color: #008080;">if</span> <span style="color: #000000;">numDevs</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
joy1Attached = (c_func(joyGetPos,{0,joyinfo}) != 167)
joy2Attached = (numDevs=2) and (c_func(joyGetPos,{1,joyinfo}) != 167)
<span style="color: #000000;">joy1Attached</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">joyGetPos</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">joyinfo</span><span style="color: #0000FF;">})</span> <span style="color: #0000FF;">!=</span> <span style="color: #000000;">167</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">joy2Attached</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">numDevs</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">joyGetPos</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">joyinfo</span><span style="color: #0000FF;">})</span> <span style="color: #0000FF;">!=</span> <span style="color: #000000;">167</span><span style="color: #0000FF;">)</span>
return joy1Attached + (joy2Attached*2)
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">joy1Attached</span> <span style="color: #0000FF;">+</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">joy2Attached</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
global function joy_get_state(integer joy_num)
if joy_num=1 or joy_num=2 then
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">joy_get_state</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">joy_num</span><span style="color: #0000FF;">)</span>
joy_num -= 1
<span style="color: #008080;">if</span> <span style="color: #000000;">joy_num</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">or</span> <span style="color: #000000;">joy_num</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
if c_func(joyGetPos,{joy_num,joyinfo+(joy_num*16)}) then
<span style="color: #000000;">joy_num</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
-- ERROR
<span style="color: #008080;">if</span> <span style="color: #7060A8;">c_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">joyGetPos</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">joy_num</span><span style="color: #0000FF;">,</span><span style="color: #000000;">joyinfo</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">joy_num</span><span style="color: #0000FF;">*</span><span style="color: #000000;">16</span><span style="color: #0000FF;">)})</span> <span style="color: #008080;">then</span>
return {}
<span style="color: #000080;font-style:italic;">-- ERROR</span>
end if
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{}</span>
return peek4u({joyinfo+(joy_num*16),4})
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">return</span> <span style="color: #7060A8;">peek4u</span><span style="color: #0000FF;">({</span><span style="color: #000000;">joyinfo</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">joy_num</span><span style="color: #0000FF;">*</span><span style="color: #000000;">16</span><span style="color: #0000FF;">),</span><span style="color: #000000;">4</span><span style="color: #0000FF;">})</span>
return {}
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function</lang>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--</syntaxhighlight>-->
And a test program:
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>include joy.ew
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
 
<span style="color: #008080;">include</span> <span style="color: #000000;">joy</span><span style="color: #0000FF;">.</span><span style="color: #000000;">ew</span>
if joy_init()=0 then
puts(1,"No joystick(s) attached!")
<span style="color: #008080;">if</span> <span style="color: #000000;">joy_init</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
abort(0)
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"No joystick(s) attached!"</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #7060A8;">abort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
sequence joy_info = {}, s
integer button_mask
<span style="color: #004080;">sequence</span> <span style="color: #000000;">joy_info</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">s</span>
 
<span style="color: #004080;">integer</span> <span style="color: #000000;">button_mask</span>
puts(1,"Joystick test\nEntering input loop. Press a key to exit..\n\n")
 
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Joystick test\nEntering input loop. Press a key to exit..\n\n"</span><span style="color: #0000FF;">)</span>
while get_key()=-1 do
-- Get the state of joystick #1
<span style="color: #008080;">while</span> <span style="color: #7060A8;">get_key</span><span style="color: #0000FF;">()=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
s = joy_get_state(1)
<span style="color: #000080;font-style:italic;">-- Get the state of joystick #1</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">joy_get_state</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
-- Do not print info unless the state has changed
if not equal(s,joy_info) then
<span style="color: #000080;font-style:italic;">-- Do not print info unless the state has changed</span>
joy_info = s
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">equal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">joy_info</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
 
<span style="color: #000000;">joy_info</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span>
printf(1,"X = %d, Y= %d ",{floor((s[1]-32767)/32768),floor((s[2]-32767)/32768)})
 
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"X = %d, Y= %d "</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">32767</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">32768</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">32767</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">32768</span><span style="color: #0000FF;">)})</span>
button_mask = 1
for i=1 to 8 do
<span style="color: #000000;">button_mask</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
if and_bits(s[4],button_mask) then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">8</span> <span style="color: #008080;">do</span>
printf(1,"BTN%d ",i)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">4</span><span style="color: #0000FF;">],</span><span style="color: #000000;">button_mask</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
else
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"BTN%d "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
puts(1," ")
end if<span style="color: #008080;">else</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)</span>
button_mask *= 2
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #000000;">button_mask</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">2</span>
puts(1,"\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
end while</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</syntaxhighlight>-->
 
=={{header|PicoLisp}}==
Line 1,117 ⟶ 1,228:
{{libheader|GLUT}}
Note: The code is not yet tested with a real joystick (I don't have one), it was just simulated with dummy functions. Can somebody having a joystick please test it, and remove this message?
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/openGl.l")
 
(setq *JoyX 0.0 *JoyY 0.0)
Line 1,156 ⟶ 1,267:
# Exit upon mouse click
(mouseFunc '((Btn State X Y) (bye)))
(glutMainLoop)</langsyntaxhighlight>
 
=={{header|PureBasic}}==
This is limited to only digital joysticks.
<langsyntaxhighlight PureBasiclang="purebasic">If InitJoystick() = 0
MessageRequester("Error!", "Need to connect a joystick", #PB_MessageRequester_Ok)
End
Line 1,231 ⟶ 1,342:
Delay(10)
Until event = #PB_Event_CloseWindow
EndIf</langsyntaxhighlight>
[[File:JoystickPosition-PureBasic.png]]
 
=={{header|Python}}==
{{libheader|Pygame}}
<langsyntaxhighlight Pythonlang="python">import sys
import pygame
 
Line 1,308 ⟶ 1,419:
# Write the display
pygame.display.flip()
clk.tick(40) # Limit to <=40 FPS</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 1,315 ⟶ 1,426:
Linux only terminal based joystick testing utility. Reads events from the joystick asynchronously, allows a main processing loop if desired. This uses the main loop to check for and compensate for a resized terminal but not really anything else. Hit control-c to exit; needs one extra event from the the joystick to exit completely. Only shows the first 3 axes, no matter how many are available. Tested with an Logitech extreme joystick and an Xbox controller.
 
<syntaxhighlight lang="raku" perl6line>use experimental :pack;
 
# Joysticks generally show up in the /dev/input/ directory as js(n) where n is
Line 1,419 ⟶ 1,530:
($rows, $cols) = qx/stty size/.words;
}
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
{{libheader|Tk}}
{{libheader|mkLibsdl}} <!-- from http://mkextensions.sourceforge.net/ -->
<langsyntaxhighlight lang="tcl">package require Tk 8.6
package require mkLibsdl
 
Line 1,466 ⟶ 1,577:
.c create line {120 115 120 125} -tags xhairV
.c create line {115 120 125 120} -tags xhairH
joystick event eval {display [joystick event peek]}</langsyntaxhighlight>
<!-- I think it can also be done with Tcl3d as that has SDL Joystick bindings, but damned if I can figure out the API in a reasonable amount of effort. It's definitely not Tclish (but what would you expect with something mashed together with SWIG? Once a stinky C API, always a stinky C API…) -->
 
=={{header|Wren}}==
{{libheader|DOME}}
I'm not sure whether DOME can detect digital joysticks as I don't have one to test. However, it can certainly detect gamepads which nowadays usually include two analog sticks (a form of joystick) so we use the left of these to detect movement and then move the cross-hair (a '+' symbol) accordingly.
 
We also allow movement of the cross-hair using the keyboard direction keys for those who don't have a suitable device.
<syntaxhighlight lang="wren">import "input" for Keyboard, GamePad
import "graphics" for Canvas, Color
import "dome" for Window
 
var Buttons = [
"left", "right", "up", "down", "A", "B", "X", "Y",
"back", "start", "guide", "leftshoulder", "rightshoulder"
]
 
var Symbols = ["L", "R", "U", "D", "A", "B", "X", "Y", "BK", "S", "G", "LS", "RS"]
 
class Main {
construct new(width, height) {
Window.resize(width, height)
Canvas.resize(width, height)
Window.title = "Joystick position"
_w = width
_h = height
_dx = (width/100).floor
_dy = (height/100).floor
_gpd = GamePad.next
}
 
init() {
// start in center
_x = _w / 2
_y = _h / 2
Canvas.cls(Color.yellow)
showButtonStatus()
Canvas.print("+", _x, _y, Color.red)
}
 
update() {
var dx = 0
var dy = 0
if (Keyboard.isKeyDown("left") || _gpd.getAnalogStick("left").x < -0.25) {
dx = -_dx
} else if (Keyboard.isKeyDown("right") || _gpd.getAnalogStick("left").x > 0.25) {
dx = _dx
} else if (Keyboard.isKeyDown("up") || _gpd.getAnalogStick("left").y < -0.25) {
dy = -_dy
} else if (Keyboard.isKeyDown("down") || _gpd.getAnalogStick("left").y > 0.25) {
dy = _dy
}
moveCrossHair(dx, dy)
}
 
moveCrossHair(dx, dy) {
_x = _x + dx
_y = _y + dy
if (_x < 0) _x = 0
if (_x > _w - 5) _x = _w - 5
if (_y < 0) _y = 0
if (_y > _h * 0.96 - 5) _y = _h * 0.96 - 5
}
 
// show whether other gamepad keys are pressed by printing the corresponding symbol if they are.
showButtonStatus() {
Canvas.rectfill(0, _h * 0.96, _w, _h, Color.blue)
var s = ""
for (i in 0...Buttons.count) {
var button = Buttons[i]
if (_gpd.isButtonPressed(button)) s = s + " " + Symbols[i]
}
Canvas.print(s, 0, _h * 0.98, Color.white)
}
 
draw(alpha) {
Canvas.cls(Color.yellow)
showButtonStatus()
Canvas.print("+", _x, _y, Color.red)
}
}
 
var Game = Main.new(600, 600)</syntaxhighlight>
 
=={{header|ZX Spectrum Basic}}==
Assuming a Kempston joystick interface, fairly standard at the time. Rewriting for the Sinclair Interface 2 involves changing line 80 to 80 LET t=IN 61438, replacing all numbers in the next five lines with 191-said number and reversing each pair of directions.
 
<langsyntaxhighlight lang="zxbasic">10 DIM o$(5)
20 LET xm=0: LET xx=31: REM screen limits
30 LET ym=0: LET yx=18
Line 1,488 ⟶ 1,680:
150 PRINT AT yc,xc;"+"
160 LET o$=" ": REM five spaces to blank output line again
170 GO TO 60</langsyntaxhighlight>
 
{{Out}}
9,482

edits