既存クラスの利用
3-2 :学習手順のアドバイス


  学習手順の説明          / 3-1   3-3   3-4   3-5   3-6 
ダウンロードは完了したでしょうか?
それではプログラムの解説をしていきたいと思うのですが、ここでひとつ注意事項があります。

長いプログラムに慣れないうちにソースだけを見ると、驚きと混乱で’めまい’がするので、 今回は、『どこでどのような処理を行ったか?』に重点をおいて解説を行っていきたいと考えています。

余裕のある人はプログラムを詳しく解析してみるのもいいかもしれませんね。



学習の流れとしては、今までの節のように、3-2 から 3-5 まではソースを載せています。
3-6 からはアルゴリズム(どのような処理をしているか)についての解説です。

それでは最後の節です、みなさんがんがりましょう!



■ 手順

1: プログラムをダウンロードする
2: 3-2 から 3-5 を軽く読む。
3: 3-6 の解説をよく読む。



  MainUnit.pas
unit MainUnit;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 ExtCtrls, StdCtrls, Spin, mmsystem, ComCtrls;
                  //↑サウンド再生用
const
 CrMyCursor =1;

type
 TCat = class(TForm)

  BackGround: TImage;

  ButtonMove: TButton;
  ButtonStop: TButton;
  CloseCat: TButton;
  OCDOWN: TButton;
  OCUP: TButton;

  T1: TTimer;
  T2: TTimer;
  T3: TTimer;

  Panel1: TPanel;
  Panel2: TPanel;
  Panel3: TPanel;
  Panel4: TPanel;

  Label1: TLabel;
  Label2: TLabel;
  LabelResult: TLabel;
  Label4: TLabel;
  Label5: TLabel;
  Label6: TLabel;
  Label7: TLabel;
  LabelHIT: TLabel;
  ObjectCat: TLabel;
  LabelCount: TLabel;

  PB: TProgressBar;
  GroupBox1: TGroupBox;

  procedure ButtonMoveClick(Sender: TObject);
  procedure ButtonStopClick(Sender: TObject);
  procedure T1Timer(Sender: TObject);
  procedure FormCreate(Sender: TObject);
  procedure BackGroundClick(Sender: TObject);
  procedure OCUPClick(Sender: TObject);
  procedure OCDOWNClick(Sender: TObject);
  procedure T3Timer(Sender: TObject);
  procedure CloseCatClick(Sender: TObject);
  procedure T2Timer(Sender: TObject);

 private
  { Private 宣言 }
 public
  { Public 宣言 }
  MAXHIT : integer;
  CATS : integer;
  MD : Boolean;
  WAVFile: string;
  BMPFile: String;
  CURFile: String;

  procedure SETDLG;
  procedure ButtonVisible;
end;

var
  Cat: TCat;

implementation

uses Unit1,Unit2;

{$R *.DFM}


//『動ネコ』クリック
procedure TCat.ButtonMoveClick(Sender: TObject);
begin
 T2.Enabled := False;
 ButtonMove.Enabled := False;
 ButtonStop.Enabled := True;
 PB.Position := 0;
 CATS := STRTOINT(ObjectCat.Caption);

 //ネコの数を元にオブジェクトの起動数を決定
 case CATS of
  0:      ButtonStopClick(Sender);
  1:      CatWork1.Start;
  2: begin CatWork1.Start;
         CatWork2.Start; end;
  3: begin CatWork1.Start;
         CatWork2.Start;
         CatWork3.Start; end;
  4: begin CatWork1.Start;
         CatWork2.Start;
         CatWork3.Start;
         WhiteCatWork.Start; end;
 end;

 //本日の機嫌
 case Random(6) of
  0: begin PB.Max := 5; MAXHIT := 5; end;
  1: begin PB.Max := 8; MAXHIT := 8; end;
  2: begin PB.Max := 9; MAXHIT := 9; end;
  3: begin PB.Max := 10; MAXHIT := 10; end;
  4: begin PB.Max := 12; MAXHIT := 12; end;
  5: begin PB.Max := 15; MAXHIT := 15; end;
 end;

 //本日のクリック回数
 case Random(5) of
  0: LabelHIT.Caption := INTTOSTR(10);
  1: LabelHIT.Caption := INTTOSTR(40);
  2: LabelHIT.Caption := INTTOSTR(50);
  3: LabelHIT.Caption := INTTOSTR(50);
  4: LabelHIT.Caption := INTTOSTR(60);
 end;

 //本日の残り時間
 case Random(5) of
  0: LabelCount.Caption := INTTOSTR(20);
  1: LabelCount.Caption := INTTOSTR(50);
  2: LabelCount.Caption := INTTOSTR(60);
  3: LabelCount.Caption := INTTOSTR(70);
  4: LabelCount.Caption := INTTOSTR(80);
 end;

 //音楽設定
 case Random(3) of
  0: SndPlaySound(PChar(WAVFile + 'se6.wav'),SND_LOOP or SND_SYNK or SND_NODEFAULT);
  1: SndPlaySound(PChar(WAVFile + 'se7.wav'),SND_LOOP or SND_SYNK or SND_NODEFAULT);
  2: SndPlaySound(PChar(WAVFile + 'se8.wav'),SND_LOOP or SND_SYNK or SND_NODEFAULT);
 end;

 T1.Enabled := True;
 T3.Enabled := True;
 MD := True;
end;



//『止ネコ』クリック
procedure TCat.ButtonStopClick(Sender: TObject);
begin
 StopGame;
end;



//ゲームを終了する為の処理
procedure TCat.StopGame;
begin
 T1.Enabled := False;
 T3.Enabled := False;

 ButtonMove.Enabled := True;
 ButtonStop.Enabled := False;
end;



//メッセージダイアログ
procedure TCat.SETDLG;
 var i:integer;
begin
 LabelResult.Visible := True;
 SndPlaySound(PChar(nil),SND_NODEFAULT);

  if MD = True then begin
   i := MessageDlg('もう一回?',mtConfirmation, [mbYes, mbNo], 0);

   if i = mrYes then begin
    CatWork1.Visible := False;
    CatWork2.Visible := False;
    CatWork3.Visible := False;
    WhiteCatWork.Visible := False;

    T1.Enabled := False;
    T2.Enabled := True;
    T3.Enabled := False;

    PB.Position := 0;
    ButtonMove.Enabled := True;
    ButtonStop.Enabled := False;
   end else
    Close;
 end else
  Exit;

 MD := False;
end;



//Timer1
procedure TCat.T1Timer(Sender: TObject);
begin
 CatWork1.CatMove;
 CatWork2.CatMove;
 CatWork3.CatMove;
 WhiteCatWork.CatMove;
end;

//Timer2
procedure TCat.T2Timer(Sender: TObject);
begin
 LabelResult.Visible := False;
 LabelHit.Caption := '00';
 LabelCount.Caption := '00';
end;

//Timer3
procedure TCat.T3Timer(Sender: TObject);
 var i:Integer;
begin
 i := STRTOINT(LabelCount.Caption);
 i := i -1;
 LabelCount.Caption := INTTOSTR(i);

 if LabelCount.Caption = INTTOSTR(0) then begin
  CatWork1.GAMEOVER;
  CatWork2.GAMEOVER;
  CatWork3.GAMEOVER;
  WhiteCatWork.GAMEOVER;

  T3.Enabled := False;
  LabelCount.Caption := INTTOSTR(00);
 end;

 if LabelCount.Caption <= INTTOSTR(0) then
  LabelCount.Caption := INTTOSTR(00);
end;



//フォーム作成時の宣言
procedure TCat.FormCreate(Sender: TObject);
begin
 BMPFile := ExtractFilePath(ParamStr(0)) + 'bmp\';
 WAVFile := ExtractFilePath(ParamStr(0)) + 'wav\';
 CURFile := ExtractFilePath(ParamStr(0)) + 'cur\';

 BackGround.Picture.LoadFromFile(BMPFile + 'Haikei.bmp');
 BackGround.AutoSize := True;

 Screen.Cursors[CrMyCursor] := LoadCursorFromFile(PChar(CURFile + 'Fish.cur'));
 Screen.Cursor := crMyCursor;
end;



//背景をクリックするとネコの機嫌を損ねる
procedure TCat.BackGroundClick(Sender: TObject);
begin
 if T1.Enabled = True then begin
  PB.Position := PB.Position -1;
  LabelHIT.Caption := INTTOSTR(STRTOINT(LabelHIT.Caption)-1);

  //クリック回数がなくなったらゲームを終了させる
  if LabelHIT.Caption <= INTTOSTR(0) then begin
   CatWork1.GAMEOVER;
   CatWork2.GAMEOVER;
   CatWork3.GAMEOVER;
   WhiteCatWork.GAMEOVER;
  end;
 end;
end;



//▲ボタン
procedure TCat.OCUPClick(Sender: TObject);
begin
 CATS := CATS +1;
 ButtonVisible;
end;

//▼ボタン
procedure TCat.OCDOWNClick(Sender: TObject);
begin
 CATS := CATS -1;
 ButtonVisible;
end;

//↑上記処理
procedure TCat.ButtonVisible;
begin
 if CATS > 4 then CATS := 4
  else if CATS < 0 then CATS := 0;

 if CATS > 0 then ButtonMove.Enabled := True
  else ButtonMove.Enabled := False;

 ObjectCat.Caption := INTTOSTR(CATS);
end;



//終了処理
procedure TCat.CloseCatClick(Sender: TObject);
begin
 SndPlaySound(PChar(nil),SND_NODEFAULT);
 Close;
end;

end.
トップに戻る /  前に戻る /  次へ