Joystick position: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Joystick position en FreeBASIC)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(3 intermediate revisions by 3 users not shown)
Line 22:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">BYTE lastStick=[255]
BYTE lastTrig=[255]
 
Line 89:
OD
CH=$FF
RETURN</langsyntaxhighlight>
{{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 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 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 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 383:
VDU 10,13
NEXT
ENDPROC</langsyntaxhighlight>
 
 
 
=={{header|C}}==
<langsyntaxhighlight lang="cpp">#include <stdio.h>
#include <stdlib.h>
 
Line 442:
system("clear");
return 1;
}</langsyntaxhighlight>
 
{{out}}
Line 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 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 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 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 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 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 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 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 650:
 
end.
</syntaxhighlight>
</lang>
Form resource:
<syntaxhighlight lang="delphi">
<lang Delphi>
object Form1: TForm1
Left = 0
Line 689:
end
end
</syntaxhighlight>
</lang>
{{out}}
Result preview [https://ibb.co/tc0xH3q]
Line 695:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Screen 12
 
Dim As Single x, y
Line 724:
End If
Next a
Loop</langsyntaxhighlight>
 
 
Line 730:
{{libheader|termbox-go}}
{{libheader|joystick(go)}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 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 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 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 956:
yield()
end
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb"> 'disable text window
nomainwin
 
Line 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 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 1,052:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Slider2D[Dynamic[ControllerState[{"X", "Y"}]], ImageSize -> {500, 500}]</langsyntaxhighlight>
 
=={{header|OCaml}}==
Line 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,112:
proc_event joyd
in
loop ((0.0, 0.0), [])</langsyntaxhighlight>
 
Run with the command:
Line 1,121:
 
First, joy.e:
<!--<langsyntaxhighlight Phixlang="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)
Line 1,184:
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--</langsyntaxhighlight>-->
And a test program:
<!--<langsyntaxhighlight Phixlang="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>
Line 1,222:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
Line 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,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,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,419:
# Write the display
pygame.display.flip()
clk.tick(40) # Limit to <=40 FPS</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 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,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,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,599 ⟶ 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,476

edits