Beginners_-_cos_and_sin.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "raylib.h"
  2. #include <math.h> // needed for cos and sin.
  3. int main(void)
  4. {
  5. // Initialization
  6. //--------------------------------------------------------------------------------------
  7. const int screenWidth = 800;
  8. const int screenHeight = 450;
  9. InitWindow(screenWidth, screenHeight, "raylib example.");
  10. Vector2 position;
  11. position.x = screenWidth/2;
  12. position.y = screenHeight/2;
  13. float angle=GetRandomValue(0,359);
  14. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  15. //--------------------------------------------------------------------------------------
  16. // Main game loop
  17. while (!WindowShouldClose()) // Detect window close button or ESC key
  18. {
  19. // Update
  20. //----------------------------------------------------------------------------------
  21. // cos and sin here.
  22. position.x += (float)cos(angle)*4;
  23. position.y += (float)sin(angle)*4;
  24. if(GetRandomValue(0,100)<5)angle=GetRandomValue(0,359);
  25. if(position.x>screenWidth || position.x<0 || position.y<0 || position.y>screenHeight){
  26. position.x=screenWidth/2;
  27. position.y=screenHeight/2;
  28. }
  29. //----------------------------------------------------------------------------------
  30. // Draw
  31. //----------------------------------------------------------------------------------
  32. BeginDrawing();
  33. ClearBackground(RAYWHITE);
  34. DrawCircle(position.x,position.y,20,RED);
  35. EndDrawing();
  36. //----------------------------------------------------------------------------------
  37. }
  38. // De-Initialization
  39. //--------------------------------------------------------------------------------------
  40. CloseWindow(); // Close window and OpenGL context
  41. //--------------------------------------------------------------------------------------
  42. return 0;
  43. }