Setup Code Bases
|
|
Before you start programming logics of the Simon® game, there are actually some basics code setups to do just like in any other script. Let's start by adding the following variables at the begenning of script: |
|
-- Set "constants" for Simon game |
|
These variables will actually be used as if they were constants since the concept of constants does not really exist in Lua. This means that our code should never modify their content. Next, we will add four "actual" variables (their content will probably change during the execution of the script) to control the game's environment: |
|
-- Game handling variables |
|
The GameState variable is a boolean which is worth false if the game is over or true if not. MainSequence is a table wich contains the whole sequence since the beginning of the new game. The variable SequenceCount is an integer containing the actual total sequence's length. Finally, UserSequenceCount is an integer containing the user's sequence length so far. Next, we will need to add four functions in wich we will add some code later. Those functions will be nested into the the table simon which will be created by the game engine (simon.dll) when the script will initialize through the Initializer function: |
|
function simon:OnButtonClick(ButtonIndex) |
|
The OnClick(ButtonIndex) function will be called every time the user will click on a button. In this function we will validate the clicked button in the current sequence. The functions simon:AddSequence(Sequence), simon:PlaySequence(Sequence) and simon:Initialize() are functions that are going to be called by our own script to handle different steps during the game. One last thing remains before the end of this step: creating the game's frame. To do so, a main loop at the end of the script will be required as follows: |
|
simon.Create() |
|
This loop will handle the game from the beginning to the end. An iteration in this loop will be executed many times per second when there is no need for it. That's why the game engine (simon.dll) has exported a function called Sleep() which tells the processor to sleep for a certain amount of time in miliseconds. In other words, by using this function we will ensure that the game isn't using 100% of the processor's resources since we don't need it in this particular case. Also, as you probably noticed already, this code snippet includes two function calls from the game engine: simon.Create() and simon.Destroy(). The simon.Create() function create the Simon® game interface to interact with the user when the simon.Destroy() function destroys this interface. In the next step, we will add some code into the previously added functions. |
|
Hints:
|
|
|