Back in the late 1970's, Parker Brothers introduced a great handheld electronic game machine called Merlin. This week we continue with Part 2 of our Merlin Simulator project and begin adding functionality to the simulator.
Interface
If you'd like to start where we left off last week, download the project. Then, launch REALbasic and open Window1. Drag a NotePlayer control from the toolbar and add it to you Window1 interface. It doesn't matter where you drag it to in the interface, since it will be invisible when the application executes. Feel free to adjust the Instrument property. If you leave it set to its default value (1) it will play back as a piano. The remaining instrument numbers are displayed in the Online Reference for the NotePlayer control (press Cmd-1).
Add the Code
Before you get started writing any code, open the Code Editor for Window1 and add the following properties (by selecting Edit-New Property):
| Property Declaration |
| CurrentNote as Integer |
| GameNumber as Integer |
| NewGame as Boolean |
| NoteArray(0) as Integer |
Close Window1 for now. We will return to it in a second. But, first we need to make some alterations to our custom Canvas classes. Open the NumButtonClass and create two new events, by selecting Edit-New Event.
| Event name | Parameters: | Return Type: |
| MouseDown | x as integer, y as integer | |
| MouseUp |
Navigate to the MouseDown and MouseUp events of the NumButtonClass and change the code for each to look like the following:
Now that you have the hang of this, open up the UtilButtonClass and do the same, creating two new events, MouseDown and MouseUp like before.
Then, navigate to the MouseDown and MouseUp events of the UtilButtonClass and change the code for each to look like the following:
What we have done here is to give our Window1 controls NumButton and UtilButton the chance to override the default functionality of the class. We will use these new events to respond to actions taken during the game, all from the code of Window1.
Before we jump back to coding the games in Window1, create two new methods in the NumButtonClass by selecting Edit-New Method. Name them as follows:
| Add these Methods to NumButton |
| ButtonOn |
| ButtonOff |
Add the appropriate code to each method:

The custom classes are now complete, so return to the Code Editor of Window1. Create two new methods, by selecting Edit-New Method.
| Method Name | Parameters: |
| MusicMachinePlayNote | merlinnote as integer, lightson as boolean |
| MusicMachinePlaySong |
The code for the MusicMachinePlaySong method is:
Since the code for the MusicMachinePlayNote method is so lengthy, I present it to you here in a text box.:
As you may have guessed, these two methods play either a note or a song (which we are storing in the NoteArray). The final step for this week is to add some code to the MouseUp events of the NumButton and UtilButton controls. Open the MouseUp for each and add the appropriate code:
These two methods tie in all the other parts we have worked on so far. The utilButton.MouseUp event sits and waits for you to start a new game. When you do, it sets a flag (newgame = TRUE). Since newgame = TRUE, the number keys respond to a new game inquiry. By pressing the "2" key, a new game called "Music Machine" begins play.
In this game, you are allowed to enter musical notes by pressing the number keys. As you play them, the notes will sound and be remembered by the Merlin Simulator. When you have completed the song, press "COMP TURN" to play the song back. For orientation purposes, the number button all the way at the top of the machine is "0", while the bottom number button is "10". The actual Merlin machine has numbers molded into the plastic adjacent to each button. Our interface is more simplistic. If you have any problems, be sure to review the Merlin manual.
Conclusion
Well, that's it for this week. Select Debug-Run to test your work. If you encounter any difficulties with the code or don't feel like entering it all, you can download the completed project and applications. See you next week!