core_random_values.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Generate random values
  4. *
  5. * This example has been created using raylib 1.1 (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 - generate random values");
  22. // SetRandomSeed(0xaabbccff); // Set a custom random seed if desired, by default: "time(NULL)"
  23. int randValue = GetRandomValue(-8, 5); // Get a random integer number between -8 and 5 (both included)
  24. int framesCounter = 0; // Variable used to count frames
  25. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  26. //--------------------------------------------------------------------------------------
  27. // Main game loop
  28. while (!WindowShouldClose()) // Detect window close button or ESC key
  29. {
  30. // Update
  31. //----------------------------------------------------------------------------------
  32. framesCounter++;
  33. // Every two seconds (120 frames) a new random value is generated
  34. if (((framesCounter/120)%2) == 1)
  35. {
  36. randValue = GetRandomValue(-8, 5);
  37. framesCounter = 0;
  38. }
  39. //----------------------------------------------------------------------------------
  40. // Draw
  41. //----------------------------------------------------------------------------------
  42. BeginDrawing();
  43. ClearBackground(RAYWHITE);
  44. DrawText("Every 2 seconds a new random value is generated:", 130, 100, 20, MAROON);
  45. DrawText(TextFormat("%i", randValue), 360, 180, 80, LIGHTGRAY);
  46. EndDrawing();
  47. //----------------------------------------------------------------------------------
  48. }
  49. // De-Initialization
  50. //--------------------------------------------------------------------------------------
  51. CloseWindow(); // Close window and OpenGL context
  52. //--------------------------------------------------------------------------------------
  53. return 0;
  54. }