Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

108 Zeilen
4.2 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - following eyes
  4. *
  5. * Example originally created with raylib 2.5, last time updated with raylib 2.5
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include <math.h> // Required for: atan2f()
  15. //------------------------------------------------------------------------------------
  16. // Program main entry point
  17. //------------------------------------------------------------------------------------
  18. int main(void)
  19. {
  20. // Initialization
  21. //--------------------------------------------------------------------------------------
  22. const int screenWidth = 800;
  23. const int screenHeight = 450;
  24. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - following eyes");
  25. Vector2 scleraLeftPosition = { GetScreenWidth()/2.0f - 100.0f, GetScreenHeight()/2.0f };
  26. Vector2 scleraRightPosition = { GetScreenWidth()/2.0f + 100.0f, GetScreenHeight()/2.0f };
  27. float scleraRadius = 80;
  28. Vector2 irisLeftPosition = { GetScreenWidth()/2.0f - 100.0f, GetScreenHeight()/2.0f };
  29. Vector2 irisRightPosition = { GetScreenWidth()/2.0f + 100.0f, GetScreenHeight()/2.0f };
  30. float irisRadius = 24;
  31. float angle = 0.0f;
  32. float dx = 0.0f, dy = 0.0f, dxx = 0.0f, dyy = 0.0f;
  33. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  34. //--------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. irisLeftPosition = GetMousePosition();
  41. irisRightPosition = GetMousePosition();
  42. // Check not inside the left eye sclera
  43. if (!CheckCollisionPointCircle(irisLeftPosition, scleraLeftPosition, scleraRadius - irisRadius))
  44. {
  45. dx = irisLeftPosition.x - scleraLeftPosition.x;
  46. dy = irisLeftPosition.y - scleraLeftPosition.y;
  47. angle = atan2f(dy, dx);
  48. dxx = (scleraRadius - irisRadius)*cosf(angle);
  49. dyy = (scleraRadius - irisRadius)*sinf(angle);
  50. irisLeftPosition.x = scleraLeftPosition.x + dxx;
  51. irisLeftPosition.y = scleraLeftPosition.y + dyy;
  52. }
  53. // Check not inside the right eye sclera
  54. if (!CheckCollisionPointCircle(irisRightPosition, scleraRightPosition, scleraRadius - irisRadius))
  55. {
  56. dx = irisRightPosition.x - scleraRightPosition.x;
  57. dy = irisRightPosition.y - scleraRightPosition.y;
  58. angle = atan2f(dy, dx);
  59. dxx = (scleraRadius - irisRadius)*cosf(angle);
  60. dyy = (scleraRadius - irisRadius)*sinf(angle);
  61. irisRightPosition.x = scleraRightPosition.x + dxx;
  62. irisRightPosition.y = scleraRightPosition.y + dyy;
  63. }
  64. //----------------------------------------------------------------------------------
  65. // Draw
  66. //----------------------------------------------------------------------------------
  67. BeginDrawing();
  68. ClearBackground(RAYWHITE);
  69. DrawCircleV(scleraLeftPosition, scleraRadius, LIGHTGRAY);
  70. DrawCircleV(irisLeftPosition, irisRadius, BROWN);
  71. DrawCircleV(irisLeftPosition, 10, BLACK);
  72. DrawCircleV(scleraRightPosition, scleraRadius, LIGHTGRAY);
  73. DrawCircleV(irisRightPosition, irisRadius, DARKGREEN);
  74. DrawCircleV(irisRightPosition, 10, BLACK);
  75. DrawFPS(10, 10);
  76. EndDrawing();
  77. //----------------------------------------------------------------------------------
  78. }
  79. // De-Initialization
  80. //--------------------------------------------------------------------------------------
  81. CloseWindow(); // Close window and OpenGL context
  82. //--------------------------------------------------------------------------------------
  83. return 0;
  84. }