Link to Gordian's home page The Black Sea of Technology
Link to New TechnologyLink to White Papers


White Papers - Articles

"Creating Synergistic Components with Delphi"

James Callan - KOG Webzine (Page 13)

Simon Says
Do you remember the game simon says? If not, perhaps you can remember the more recent electronic memory game that helps children remember colors? Using sounds and colors it generates patterns for children to repeat, thus building hand-eye coordination and memory. We will construct the first half of a computer-based game that permits the child to play the part of simon. It, in similar fashion, helps in building memory and learning the colors.

Create a new project and lay out a form according to Figure 10. The form, which you should save as SIMNSAYS.PAS, contains sixteen TPanels and sixteen TButton controls. Center each button in its own panel. Set all the panels’ outer bevels to bvLowered. Then, give each panel a different color from Delphi’s predefined color palette. Make the upper left panel Black, followed by Maroon, Green, Olive, then continue to the second row with Navy, Purple, Teal and Gray. Continue coloring the panels down through White. Name each button based on its corresponding color. Thus, the button over the Black panel is named and captioned "Black." The button over the Yellow panel is called "Yellow," etc.



Next, drop a TMainMenu and a TMacroRecorder on the form. Name the macro recorder "Macro1." Then, set up the menu as shown in Figure 11.


Figure 11


Next, add a second form, SHOWCOLR.PAS, to the project, and center a TMemo control on the second form as shown in Figure 12.


Figure 12


Change the memo’s Visible property to False, its Color property to clActiveCaption, its Font height to 20, and its Font.Color to clWhite. This helps to color coordinate the memo with the form caption. Finally, define menu and button click events as indicated in the listings of Figures 13 and 14. Run the new form.

unit SimnSays;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Menus, Macrorec;
const
Title = 'Simon Says "Learn the Colors"';
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
File1: TMenuItem;
Exit1: TMenuItem;
SimonSays1: TMenuItem;
BeginRemembering1: TMenuItem;
EndRemembering1: TMenuItem;
N1: TMenuItem;
TestMemory1: TMenuItem;
Panel1: TPanel;
Black: TButton;
Panel2: TPanel;
Maroon: TButton;
Panel3: TPanel;
Green: TButton;
Panel4: TPanel;
Olive: TButton;
Panel5: TPanel;
Navy: TButton;
Panel6: TPanel;
Purple: TButton;
Panel7: TPanel;
Teal: TButton;
Panel8: TPanel;
Gray: TButton;
Panel9: TPanel;
Silver: TButton;
Panel10: TPanel;
Red: TButton;
Panel11: TPanel;
Lime: TButton;
Panel12: TPanel;
Yellow: TButton;
Panel13: TPanel;
Blue: TButton;
Panel14: TPanel;
Fuchsia: TButton;
Panel15: TPanel;
Aqua: TButton;
Panel16: TPanel;
White: TButton;
Help1: TMenuItem;
Showmethelist1: TMenuItem;
Macro1: TMacroRecorder;
procedure BlackClick(Sender: TObject);
procedure BeginRemembering1Click(Sender: TObject);
procedure EndRemembering1Click(Sender: TObject);
procedure TestMemory1Click(Sender: TObject);
procedure Showmethelist1Click(Sender: TObject);
procedure Exit1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
Uses ShowColr;
procedure TForm1.BlackClick(Sender: TObject);
begin
Form2.Color := TPanel(TControl(Sender).Parent).Color;
Form2.Left := Left;
Form2.Top := Top;
Form2.Width := Width;
Form2.Height := Height;
Form2.Caption := TButton(Sender).Name;
Form2.ShowModal;
end;
procedure TForm1.BeginRemembering1Click(Sender: TObject);
begin
Caption := Title+' - (Remembering)';
Macro1.BeginMacro
end;
procedure TForm1.EndRemembering1Click(Sender: TObject);
begin
Caption := Title;
Macro1.EndMacro
end;
procedure TForm1.TestMemory1Click(Sender: TObject);
begin
Macro1.Playback
end;
procedure TForm1.Showmethelist1Click(Sender: TObject);
begin
Form2.Caption := 'Colors in order of appearance';
Form2.Color := clBtnFace;
Form2.Memo1.Text := Macro1.Macro;
Form2.Memo1.Visible := True;
Form2.ShowModal;
Form2.Memo1.Visible := False;
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;
end.

Figure 13



Like the first example, you can turn remembering "on" and "off" and can click a few buttons to create a macro. Use the Help menu to display the remembered macros, and choose "Test Memory" to try your skill at remembering which color will appear next. The game could be easily extended to randomly create macro patterns by making the Macro property of TMacroRecorder writable. As is however, the application does exhibit some nice complexity, made simple through the link associations between our TButton and TCheckBox components and our TMacroRecorder.

Previous PageNext Page 

Signature Block of Gordian Solutions