langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
54
Task/Joystick-position/OCaml/joystick-position.ocaml
Normal file
54
Task/Joystick-position/OCaml/joystick-position.ocaml
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
let remove x = List.filter (fun y -> y <> x)
|
||||
let buttons_string b =
|
||||
String.concat " " (List.map string_of_int b)
|
||||
|
||||
let position app x y =
|
||||
let view = SFRenderWindow.getView app in
|
||||
let width, height = SFView.getSize view in
|
||||
let hw = width /. 2.0 in
|
||||
let hh = height /. 2.0 in
|
||||
(hw +. ((x /. 100.0) *. hw),
|
||||
hh +. ((y /. 100.0) *. hh))
|
||||
|
||||
let cross =
|
||||
[| 1.0, 1.0; 10.0, 1.0; 10.0, -1.0; 1.0, -1.0;
|
||||
1.0, -10.0; -1.0, -10.0; -1.0, -1.0; -10.0, -1.0;
|
||||
-10.0, 1.0; -1.0, 1.0; -1.0, 10.0; 1.0, 10.0; |]
|
||||
|
||||
let () =
|
||||
let app = SFRenderWindow.make (800, 600) "Joystick Position" in
|
||||
let text = SFText.make "" in
|
||||
let shape = SFShape.create cross in
|
||||
SFShape.setFillColor shape SFColor.white;
|
||||
SFShape.setOutlineColor shape SFColor.white;
|
||||
SFShape.setOutlineThickness shape 1.0;
|
||||
let rec display ((x, y), b) =
|
||||
SFText.setString text (buttons_string b);
|
||||
let x, y = position app x y in
|
||||
SFShape.setPosition shape x y;
|
||||
SFRenderWindow.clear app SFColor.black;
|
||||
SFRenderWindow.drawText app text ();
|
||||
SFRenderWindow.drawShape app shape ();
|
||||
SFRenderWindow.display app;
|
||||
and loop joyd =
|
||||
let get_joystick (((x, y), b) as joyd) = function
|
||||
| SFEvent.JoystickButtonPressed (0, button) -> ((x, y), button::b)
|
||||
| SFEvent.JoystickButtonReleased (0, button) -> ((x, y), remove button b)
|
||||
| SFEvent.JoystickMoved (0, SFEvent.JoystickX, av) -> ((av, y), b)
|
||||
| SFEvent.JoystickMoved (0, SFEvent.JoystickY, av) -> ((x, av), b)
|
||||
| _ -> joyd
|
||||
in
|
||||
let rec proc_event joyd =
|
||||
match SFRenderWindow.pollEvent app with
|
||||
| Some SFEvent.KeyPressed (SFKey.Escape,_,_,_,_)
|
||||
| Some SFEvent.Closed -> ()
|
||||
| Some event ->
|
||||
let joyd = get_joystick joyd event in
|
||||
proc_event joyd
|
||||
| None ->
|
||||
display joyd;
|
||||
loop joyd
|
||||
in
|
||||
proc_event joyd
|
||||
in
|
||||
loop ((0.0, 0.0), [])
|
||||
72
Task/Joystick-position/PureBasic/joystick-position.purebasic
Normal file
72
Task/Joystick-position/PureBasic/joystick-position.purebasic
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
If InitJoystick() = 0
|
||||
MessageRequester("Error!", "Need to connect a joystick", #PB_MessageRequester_Ok)
|
||||
End
|
||||
EndIf
|
||||
|
||||
;some constants for Window positioning
|
||||
#WindowW = 100: #WindowH = 100
|
||||
#CrossW = 10
|
||||
#p1 = (#WindowW - #CrossW) / 2
|
||||
#p2 = (#WindowW / 2 - #CrossW)
|
||||
|
||||
If OpenWindow(0, 0, 0, #WindowW * 2 + 10, #WindowH, "Joystick Position", #PB_Window_SystemMenu)
|
||||
CreateImage(0, #WindowW, #WindowW)
|
||||
ImageGadget(0, 0, 0, 0, 0, ImageID(0))
|
||||
TextGadget(2, #WindowW + 5, 10, #WindowW, 20, "Buttons Pressed:")
|
||||
CreateImage(1, #WindowW, 40)
|
||||
ImageGadget(1, #WindowW + 5, 30, 0, 0, ImageID(1))
|
||||
|
||||
AddKeyboardShortcut(0, #PB_Shortcut_Escape, 0)
|
||||
Define event, x_movement, y_movement
|
||||
Repeat
|
||||
Repeat
|
||||
event = WindowEvent()
|
||||
Select event
|
||||
Case #PB_Event_Menu
|
||||
If EventMenu() = 0
|
||||
End
|
||||
EndIf
|
||||
Case #PB_Event_CloseWindow
|
||||
End
|
||||
EndSelect
|
||||
Until event = 0
|
||||
|
||||
Define pressed.s, buttonNum, buttonX, buttonY, buttonText.s, buttonColor
|
||||
pressed.s = ""
|
||||
If ExamineJoystick(0)
|
||||
x_movement = JoystickAxisX(0)
|
||||
y_movement = JoystickAxisY(0)
|
||||
|
||||
StartDrawing(ImageOutput(1))
|
||||
DrawingMode(#PB_2DDrawing_Transparent)
|
||||
Box(0, 0, #WindowW, 50, RGB($D4, $D0, $C8)) ;a Gray
|
||||
; check to see if any of the buttons have been pressed
|
||||
For buttonNum = 1 To 10
|
||||
buttonX = ((buttonNum - 1) * 20 + 10) % #WindowW
|
||||
buttonY = ((buttonNum - 1) / 5) * 20 + 10
|
||||
If JoystickButton(0, buttonNum)
|
||||
buttonColor = RGB($FF, 0, 0) ;Red
|
||||
Else
|
||||
buttonColor = RGB($80, $80, $80) ;Gray
|
||||
EndIf
|
||||
Circle(buttonX, buttonY, 9, buttonColor)
|
||||
buttonText = Str(buttonNum)
|
||||
DrawText(buttonX - TextWidth(buttonText) / 2, buttonY - TextHeight(buttonText) / 2, buttonText, RGB($FF, $FF, $FF)) ;White
|
||||
Next
|
||||
StopDrawing()
|
||||
|
||||
SetGadgetState(1, ImageID(1))
|
||||
EndIf
|
||||
|
||||
|
||||
StartDrawing(ImageOutput(0))
|
||||
Box(0,0, #WindowW, #WindowW, RGB($FF, $FF, $FF)) ;White
|
||||
Line(#p1 + x_movement * #p2, #WindowW / 2 + y_movement * #p2, #CrossW, 1, RGB($FF, 0, 0)) ;Red
|
||||
Line(#WindowW / 2 + x_movement * #p2, #p1 + y_movement * #p2, 1, #CrossW, RGB($FF, 0, 0)) ;Red
|
||||
StopDrawing()
|
||||
|
||||
SetGadgetState(0, ImageID(0))
|
||||
|
||||
Delay(10)
|
||||
Until event = #PB_Event_CloseWindow
|
||||
EndIf
|
||||
Loading…
Add table
Add a link
Reference in a new issue