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}}==
<
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</
=={{header|AutoHotkey}}==
Line 67 ⟶ 139:
[http://i.imgur.com/KsbrpK4.png Image link]
<
; #Include, Gdip.ahk
Line 285 ⟶ 357:
Exit:
Gdip_Shutdown(pToken)
ExitApp</
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
VDU 5
Line 311 ⟶ 383:
VDU 10,13
NEXT
ENDPROC</
=={{header|C}}==
<
#include <stdlib.h>
Line 370 ⟶ 442:
system("clear");
return 1;
}</
{{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.
<
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</
===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.
<
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</
===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.
<
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</
===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.
<
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</
=={{header|Delphi}}==
{{libheader|mmSystem}}
Form application version.
<syntaxhighlight lang="delphi">
unit uMain;
Line 578 ⟶ 650:
end.
</syntaxhighlight>
Form resource:
<syntaxhighlight lang="delphi">
object Form1: TForm1
Left = 0
Line 617 ⟶ 689:
end
end
</syntaxhighlight>
{{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)}}
<
import (
Line 727 ⟶ 832:
}
}
}</
=={{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:
<
Button:Properties,Button:Test</
=={{header|Haskell}}==
Half-solution of the problem, exhibits X and Y coordinates of the joystick; works on Windows (Haskell Platform):
<
import Graphics.Win32.Key
import Control.Monad.RWS.Strict (liftIO)
Line 760 ⟶ 865:
(Just (x:y:_)) -> (-y, x)
_ -> ( 0, 0)
</syntaxhighlight>
=={{header|IS-BASIC}}==
<
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</
=={{header|Julia}}==
<
#------------ input code ----------------------#
Line 851 ⟶ 956:
yield()
end
</syntaxhighlight>
=={{header|Liberty BASIC}}==
<
nomainwin
Line 925 ⟶ 1,030:
close #m
unloadbmp "plus"
end</
=={{header|Locomotive Basic}}==
<
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</
Output (this version supports drawing with the cursor):
Line 947 ⟶ 1,052:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
=={{header|OCaml}}==
Line 954 ⟶ 1,059:
{{libheader|ocaml-sfml}}
<
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), [])</
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)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
<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
--
-- sequence joy_get_state(integer joy_num)
-- returns the current state of joystick #joy_num (can be either 1 or 2).
-- the format of the return sequence is:
-- {X_direction, Y_direction, Z_direction, buttons}
-- the X,Y and Z directions have 3 possible values; 0 (negative), 32768 (neutral) or 65535 (positive)
-- the buttons' status are represented by a single bit (button up, button down). e.g. to get the status
-- of button #3 on joystick #1 you'd use:
--
-- state = joy_get_state(1)
-- if and_bits(state[4],4) then ... end if
--</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">joy_init</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">joy1Attached</span><span style="color: #0000FF;">,</span><span style="color: #000000;">joy2Attached</span>
<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>
<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>
<span style="color: #008080;">return</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<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>
<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>
<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>
<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>
<span style="color: #000000;">joy_num</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<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>
<span style="color: #000080;font-style:italic;">-- ERROR</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<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)-->
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span style="color: #000080;font-style:italic;">-- Do not print info unless the state has changed</span>
<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>
<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>
<span style="color: #000000;">button_mask</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">button_mask</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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;">"\n"</span><span style="color: #0000FF;">)</span>
<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?
<
(setq *JoyX 0.0 *JoyY 0.0)
Line 1,156 ⟶ 1,267:
# Exit upon mouse click
(mouseFunc '((Btn State X Y) (bye)))
(glutMainLoop)</
=={{header|PureBasic}}==
This is limited to only digital joysticks.
<
MessageRequester("Error!", "Need to connect a joystick", #PB_MessageRequester_Ok)
End
Line 1,231 ⟶ 1,342:
Delay(10)
Until event = #PB_Event_CloseWindow
EndIf</
[[File:JoystickPosition-PureBasic.png]]
=={{header|Python}}==
{{libheader|Pygame}}
<
import pygame
Line 1,308 ⟶ 1,419:
# Write the display
pygame.display.flip()
clk.tick(40) # Limit to <=40 FPS</
=={{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"
# 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>
=={{header|Tcl}}==
{{libheader|Tk}}
{{libheader|mkLibsdl}} <!-- from http://mkextensions.sourceforge.net/ -->
<
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]}</
<!-- 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
<
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</
{{Out}}
|