core_random_values.lua 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [core] example - Generate random values
  4. --
  5. -- This example has been created using raylib 1.6 (www.raylib.com)
  6. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. --
  8. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  9. --
  10. -------------------------------------------------------------------------------------------
  11. -- Initialization
  12. -------------------------------------------------------------------------------------------
  13. local screenWidth = 800
  14. local screenHeight = 450
  15. InitWindow(screenWidth, screenHeight, "raylib [core] example - generate random values")
  16. local framesCounter = 0 -- Variable used to count frames
  17. local randValue = GetRandomValue(-8, 5) -- Get a random integer number between -8 and 5 (both included)
  18. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  19. ----------------------------------------------------------------------------------------
  20. -- Main game loop
  21. while not WindowShouldClose() do -- Detect window close button or ESC key
  22. -- Update
  23. ------------------------------------------------------------------------------------
  24. framesCounter = framesCounter + 1
  25. -- Every two seconds (120 frames) a new random value is generated
  26. if (((framesCounter/120)%2) == 1) then
  27. randValue = GetRandomValue(-8, 5)
  28. framesCounter = 0
  29. end
  30. ------------------------------------------------------------------------------------
  31. -- Draw
  32. ------------------------------------------------------------------------------------
  33. BeginDrawing()
  34. ClearBackground(RAYWHITE)
  35. DrawText("Every 2 seconds a new random value is generated:", 130, 100, 20, MAROON)
  36. DrawText(string.format("%i", randValue), 360, 180, 80, LIGHTGRAY)
  37. EndDrawing()
  38. ------------------------------------------------------------------------------------
  39. end
  40. -- De-Initialization
  41. ----------------------------------------------------------------------------------------
  42. CloseWindow() -- Close window and OpenGL context
  43. ----------------------------------------------------------------------------------------