Game 3-5-7 - Basic Functionality
This tutorial describes how to add basic functionality to the 3-5-7 game interface that was introduced in tutorial Game 3-5-7 - A Simple Interface.
The game interface and the functionality are distinctly separate entities, although they work closely together. Once you have finished this tutorial, feel free to modify the code and expand it, possibly implementing your own rules. The tutorial Game 3-5-7 - Game Logic expands the functionality of the code in this tutorial and implements the rules that underlie the operation of this game.
Open the script editor by right clicking on the game card and selecting Edit Card Script. This opens an editor as is shown in the image of this step. You can now copy and paste the following code into the editor. Once you have added the code, select the Apply button to apply the code to your game. You can now play the game on your card. Ensure that you have selected the Run (browse) tool before you try to play the game.
on mouseUp // Declare two variables. local tButtonName, tControl // Specify a delimiter that allows you // to select an item in a line of text. set the itemDelimiter to space // Get the name of the button as we // specified it in the Property Inspector. put the short name of target into tButtonName // Test if the mouse click comes // from a button or something else. // The first item of the name of a target specifies // what type of control sent the mouse message. if item 1 of the name of target is "button" then // If the reset button was not pressed then the mouse // message came from one of the 15 game buttons. if tButtonName is not "Reset" then // Make one of the 15 game buttons invisible. set the visible of control tButtonName to false else // If the reset button was pressed // then loop through all buttons on the card. repeat with tControl = 1 to the number of controls of me // Make the button visible. set the visible of control tControl to true end repeat end if end if end mouseUp
Copying and pasting a section of code may not be as challenging as writing the code itself, however, we should take a look at how the code works to appreciate what is happening.
The code implemented here provides very limited functionality and does not impose any rules on what a player may or may not do. The code hides a game button when it is selected and shows all the buttons again when the "Reset" button is selected.
In this step we take a closer look at each line of the code in order to determine how it works and what actions it takes.
on mouseUp ... end mouseUp
The mouseUp command encapsulates the code that is to be executed when a mouse click message is encountered. This means that each time you click somewhere on your game card, the following code is executed.
local tButtonName, tControl
local indicates that variables are being defined. These are used to store a value of some kind for future use.
set the itemDelimiter to space
The itemDelimiter is a property that can be set to specify a character that is used to separate expressions in a chunk of data. For example if you have a line of text, then you may want to use a space character to separate one word from an other.
put the short name of target into tButtonName
The short name of target is the name of the control that was selected when pressing the mouse. This gives us the name of the button that a player selected while playing the game. The put command assigns a value to a variable. In this case we are assigning the short name of the target to the variable tButtonName.
The next parts of the code are conditional statements that allow us to execute different paths through the code, depending on certain conditions that may occur.
if item 1 of the name of target is "button" then
The first condition we encounter gets the name of the control that was selected. Notice how this differs from the short name of a control. The first item of the name of a target is the type of control that was selected. In the game implementation, we are only interested in the buttons the players may select. So if a player has selected a button, then continue, if a player did not select a button, then go to the end of the condition and do nothing.
if tButtonName is not "Reset" then
By the time we reach the second condition, we know that a player has selected a button, we now need to find out if that button is a game button or the "Reset" button. If it is not the "Reset" button, then we know that it must be a game button.
set the visible of control tButtonName to false
When a player selects a game button, then that button has to be removed. In this case we are not physically removing a button but are hiding it. The button remains in its location, but the visibility of it is turned off.
If the player did not select a game button, but selected the "Reset" button instead, then we have to make all of the invisible buttons visible again. This is carried out in a repeat loop.
repeat with tControl = 1 to the number of controls of me
We loop through each number from 1 to the number of controls that are on the game card. This allows us to access each item that is on the card. This construct is independent of the number of buttons on the card. For example, if you wanted to write a 3-5-7-9 game, then this code would still work and loop through he additional 9 buttons.
set the visible of control tControl to true
When we have the number for a control on the card, set its visibility to true.
end repeat end if end if
The last three statements close off the repeat and if statements of the code, embracing the code that is to be executed in the particular code path.
You have now completed a basic game and can now experiment with the code in order to add further functionality. You can also follow the tutorial: Game 3-5-7 - Game Logic for an example on how to add extra gaming logic.