Simon.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. --[[
  2. This file manage the Simon game by randomizing sequences and validating
  3. results. LuaEdit's initializer logic has been implemented in Simon.dll
  4. to allow debugging.
  5. ]]--
  6. -- Set "constant" for Simon game
  7. local SIMON_NONE = 0
  8. local SIMON_RED = 1
  9. local SIMON_BLUE = 2
  10. local SIMON_YELLOW = 3
  11. local SIMON_GREEN = 4
  12. -- Game handling variables
  13. local GameState = true
  14. local MainSequence = {}
  15. local SequenceCount = 0
  16. local UserSequenceCount = 0
  17. -- Event handler called by simon.dll when any of the colored buttons are clicked
  18. function simon:OnButtonClick(ButtonIndex)
  19. local Result = 0
  20. UserSequenceCount = UserSequenceCount + 1
  21. if MainSequence[UserSequenceCount] == ButtonIndex then
  22. simon.SetScore(simon.GetScore() + 10)
  23. Result = 1
  24. end
  25. return Result
  26. end
  27. -- Add one more part to the game's sequence
  28. function simon:AddSequence()
  29. SequenceCount = SequenceCount + 1
  30. MainSequence[SequenceCount] = math.random(4)
  31. end
  32. -- Play the game's sequence
  33. function simon:PlaySequence(Sequence)
  34. local v = nil
  35. local i = nil
  36. -- Lock controls from user to make sure no conflicts happened while palying sequence
  37. simon.LockControls()
  38. -- Play all sequence
  39. repeat
  40. simon.SetLight(SIMON_NONE)
  41. Sleep(300)
  42. i, v = next(Sequence, i)
  43. simon.SetLight(v)
  44. if i ~= nil then
  45. Sleep(500)
  46. end
  47. until i == nil
  48. simon.UnlockControls()
  49. simon.SetLight(SIMON_NONE)
  50. end
  51. -- Initialize the game
  52. function simon:Initialize()
  53. -- Initalize variables
  54. simon:SetScore(0)
  55. GameState = true
  56. UserSequenceCount = 0
  57. SequenceCount = 0
  58. MainSequence = {}
  59. -- Initialize random engine
  60. math.randomseed(os.time())
  61. math.random()
  62. math.random()
  63. math.random()
  64. end
  65. -- Show Simon game main form
  66. simon.SetMediaPath("C:\\Prog\\Delphi\\MNet\\Bin\\Medias")
  67. simon.Create()
  68. -- Main processing loop
  69. while simon.GetPowerStatus() == 1 do
  70. if simon.GetPlayStatus() == 1 then
  71. simon:Initialize()
  72. -- Game processing loop
  73. while GameState do
  74. UserSequenceCount = 0
  75. simon:AddSequence()
  76. simon:DisplayMessage("Simon's Turn!", "clRed", 750)
  77. simon:PlaySequence(MainSequence)
  78. simon:DisplayMessage("Your Turn!", "clLime", 1000)
  79. GameState = simon.GetUserSequence(SequenceCount, 2000)
  80. if simon:GetScore() > 1000 then
  81. simon:DisplayMessage("You Win!", "clAqua", -1)
  82. GameState = false
  83. end
  84. end
  85. end
  86. -- Make sure the processor doesn't runs for no reason
  87. Sleep(10)
  88. end
  89. simon.Destroy()