66 rader
2.5 KiB

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