core_input_keys.c 2.4 KB

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