You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.8 KiB

  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [core] example - Storage save/load 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. -- NOTE: Storage positions must start with 0, directly related to file memory layout
  12. STORAGE_SCORE = 0
  13. STORAGE_HISCORE = 1
  14. -- Initialization
  15. -------------------------------------------------------------------------------------------
  16. local screenWidth = 800
  17. local screenHeight = 450
  18. InitWindow(screenWidth, screenHeight, "raylib [core] example - storage save/load values")
  19. local score = 0
  20. local hiscore = 0
  21. local framesCounter = 0
  22. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  23. -------------------------------------------------------------------------------------------
  24. -- Main game loop
  25. while not WindowShouldClose() do -- Detect window close button or ESC key
  26. -- Update
  27. ---------------------------------------------------------------------------------------
  28. if (IsKeyPressed(KEY.R)) then
  29. score = GetRandomValue(1000, 2000)
  30. hiscore = GetRandomValue(2000, 4000)
  31. end
  32. if (IsKeyPressed(KEY.ENTER)) then
  33. StorageSaveValue(STORAGE_SCORE, score)
  34. StorageSaveValue(STORAGE_HISCORE, hiscore)
  35. elseif (IsKeyPressed(KEY.SPACE)) then
  36. -- NOTE: If requested position could not be found, value 0 is returned
  37. score = StorageLoadValue(STORAGE_SCORE)
  38. hiscore = StorageLoadValue(STORAGE_HISCORE)
  39. end
  40. framesCounter = framesCounter + 1
  41. ---------------------------------------------------------------------------------------
  42. -- Draw
  43. ---------------------------------------------------------------------------------------
  44. BeginDrawing()
  45. ClearBackground(RAYWHITE)
  46. DrawText(string.format("SCORE: %i", score), 280, 130, 40, MAROON)
  47. DrawText(string.format("HI-SCORE: %i", hiscore), 210, 200, 50, BLACK)
  48. DrawText(string.format("frames: %i", framesCounter), 10, 10, 20, LIME)
  49. DrawText("Press R to generate random numbers", 220, 40, 20, LIGHTGRAY)
  50. DrawText("Press ENTER to SAVE values", 250, 310, 20, LIGHTGRAY)
  51. DrawText("Press SPACE to LOAD values", 252, 350, 20, LIGHTGRAY)
  52. EndDrawing()
  53. ---------------------------------------------------------------------------------------
  54. end
  55. -- De-Initialization
  56. -------------------------------------------------------------------------------------------
  57. CloseWindow() -- Close window and OpenGL context
  58. -------------------------------------------------------------------------------------------