Joystick position: Difference between revisions

Added Wren
(Joystick position en FreeBASIC)
(Added Wren)
Line 1,579:
joystick event eval {display [joystick event peek]}</lang>
<!-- 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.
<lang ecmascript>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)</lang>
 
=={{header|ZX Spectrum Basic}}==
9,482

edits