core_input_gamepad.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [core] example - Gamepad input
  4. --
  5. -- NOTE: This example requires a Gamepad connected to the system
  6. -- raylib is configured to work with Xbox 360 gamepad, check raylib.h for buttons configuration
  7. --
  8. -- This example has been created using raylib 1.6 (www.raylib.com)
  9. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. --
  11. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  12. --
  13. -------------------------------------------------------------------------------------------
  14. -- Initialization
  15. -------------------------------------------------------------------------------------------
  16. local screenWidth = 800
  17. local screenHeight = 450
  18. InitWindow(screenWidth, screenHeight, "raylib [core] example - gamepad input")
  19. local ballPosition = Vector2(screenWidth/2, screenHeight/2)
  20. local gamepadMovement = Vector2(0, 0)
  21. SetTargetFPS(60) -- Set target frames-per-second
  22. -------------------------------------------------------------------------------------------
  23. -- Main game loop
  24. while not WindowShouldClose() do -- Detect window close button or ESC key
  25. -- Update
  26. ---------------------------------------------------------------------------------------
  27. if (IsGamepadAvailable(GAMEPAD.PLAYER1)) then
  28. gamepadMovement.x = GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LEFT_X)
  29. gamepadMovement.y = GetGamepadAxisMovement(GAMEPAD.PLAYER1, GAMEPAD.XBOX_AXIS_LEFT_Y)
  30. ballPosition.x = ballPosition.x + gamepadMovement.x
  31. ballPosition.y = ballPosition.y - gamepadMovement.y
  32. if (IsGamepadButtonPressed(GAMEPAD.PLAYER1, GAMEPAD.BUTTON_A)) then
  33. ballPosition.x = screenWidth/2
  34. ballPosition.y = screenHeight/2
  35. end
  36. end
  37. ---------------------------------------------------------------------------------------
  38. -- Draw
  39. ---------------------------------------------------------------------------------------
  40. BeginDrawing()
  41. ClearBackground(RAYWHITE)
  42. DrawText("move the ball with gamepad", 10, 10, 20, DARKGRAY)
  43. DrawCircleV(ballPosition, 50, MAROON)
  44. EndDrawing()
  45. ---------------------------------------------------------------------------------------
  46. end
  47. -- De-Initialization
  48. -------------------------------------------------------------------------------------------
  49. CloseWindow() -- Close window and OpenGL context
  50. -------------------------------------------------------------------------------------------