core_input_keys.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Keyboard input
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 1.0, last time updated with raylib 1.0
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2014-2024 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. int main(void)
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. const int screenWidth = 800;
  24. const int screenHeight = 450;
  25. InitWindow(screenWidth, screenHeight, "raylib [core] example - keyboard input");
  26. Vector2 ballPosition = { (float)screenWidth/2, (float)screenHeight/2 };
  27. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  28. //--------------------------------------------------------------------------------------
  29. // Main game loop
  30. while (!WindowShouldClose()) // Detect window close button or ESC key
  31. {
  32. // Update
  33. //----------------------------------------------------------------------------------
  34. if (IsKeyDown(KEY_RIGHT)) ballPosition.x += 2.0f;
  35. if (IsKeyDown(KEY_LEFT)) ballPosition.x -= 2.0f;
  36. if (IsKeyDown(KEY_UP)) ballPosition.y -= 2.0f;
  37. if (IsKeyDown(KEY_DOWN)) ballPosition.y += 2.0f;
  38. //----------------------------------------------------------------------------------
  39. // Draw
  40. //----------------------------------------------------------------------------------
  41. BeginDrawing();
  42. ClearBackground(RAYWHITE);
  43. DrawText("move the ball with arrow keys", 10, 10, 20, DARKGRAY);
  44. DrawCircleV(ballPosition, 50, MAROON);
  45. EndDrawing();
  46. //----------------------------------------------------------------------------------
  47. }
  48. // De-Initialization
  49. //--------------------------------------------------------------------------------------
  50. CloseWindow(); // Close window and OpenGL context
  51. //--------------------------------------------------------------------------------------
  52. return 0;
  53. }