それではここからは、実際にクラスの作成に入ります。まず、出現するもぐらですが、普通のもぐらと特殊なもぐらの 2 種類がこのゲームに登場しますので TBaseMog という、もぐらの基本要素をもつクラスを、ここでは作成します。
この TBaseMog には、出現するもぐらの表示時間(叩かれた絵と叩かれていない絵の 2 つ)を表すフィールドと、そのもぐらの絵(ビットマップ)を表すフィールドを持たせます。
このクラスを宣言するユニットは、Form1 を宣言してあるユニットとは別のユニットにしたいと思いますので、[ ファイル | 新規作成 | ユニット ] を選択し、新しいユニットを表示させてください(Unit2 が表示されると思います)。出来ましたら、以下のようにタイプします。(このページに記載されているコードは、全て Unit2 に書きます。)
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls;
type
TBaseMog = class(TObject)
private
fLiveTime: Integer; // 叩かれていない絵を表示する時間
fDeadTime: Integer; // 叩かれた絵を表示する時間
fLiveBmp: TBitmap; // 叩かれていないビットマップ
fDeadBmp: TBitmap; // 叩かれたビットマップ
fScore: Integer; // 叩いた時に得られるスコア
public
constructor Create(LiveTime, DeadTime: Integer;
LiveBmp, DeadBmp: string; Score: Integer);
destructor Destroy; override;
property LiveTime: Integer read fLiveTime;
property DeadTime: Integer read fDeadTime;
property LiveBmp: TBitmap read fLiveBmp;
property DeadBmp: TBitmap read fDeadBmp;
property Score: Integer read fScore;
end;
TBaseMog クラスで fLiveTime, fDeadTime, fLiveBmp, fDeadBmp, fScore を宣言しました。実際にインスタンスを生成する際には、この TBaseMog クラスのインスタンスを 2 つ(普通のもぐらと黄色いもぐら)生成します。生成する際、コンストラクタに、表示時間、ビットマップ、もぐらを叩く事で獲得するスコアを指定し、それぞれをフィールドに代入してあげます。
{ TBaseMog }
constructor TBaseMog.Create(LiveTime, DeadTime: Integer;
LiveBmp, DeadBmp: string; Score: Integer);
begin
// 初期化
fLiveTime := LiveTime;
fDeadTime := DeadTime;
fScore := Score;
fLiveBmp := TBitmap.Create;
fDeadBmp := TBitmap.Create;
fLiveBmp.LoadFromResourceName(hInstance, LiveBmp);
fDeadBmp.LoadFromResourceName(hInstance, DeadBmp);
end;
destructor TBaseMog.Destroy;
begin
fLiveBmp.Free;
fDeadBmp.Free;
inherited;
end;
TBaseMog クラスを作成した主な理由は、このクラスから生成されるインスタンスから、そのクラス、つまりもぐらの表示時間や、表示すべき絵(ビットマップ)、獲得するスコアなどの問い合わせをするためです。つまり、単なる「問い合わせ」の為に使用します。
次に、もぐらが出現する穴のクラス THole を作成します。
THole = class(TObject) private fExistence: Boolean; // 穴からモグラが出ているか fIsDead: Boolean; // 表示しているモグラが、既に叩かれている状態か fHolePos: TPoint; // 穴の位置 fMogura: TBaseMog; // その穴から出ているモグラ(の種類) fLimitTime: Integer; // モグラを表示していられる時間 public constructor Create(HolePos: TPoint); procedure DecLimitTime; // 出現しているモグラの表示時間を減らす procedure DeleteMog; // 表示しているモグラを消します function HoleIsFull: Boolean; // 穴からモグラが出現しているか property LimitTime: Integer read fLimitTime write fLimitTime; property IsDead: Boolean read fIsDead write fIsDead; property Existence: Boolean read fExistence write fExistence; property HolePos: TPoint read fHolePos; property Mogura: TBaseMog read fMogura write fMogura; end;
THole クラスには主に以下の役目を負っています。
constructor THole.Create(HolePos: TPoint);
begin
fHolePos := HolePos;
fLimitTime := -1;
fExistence := false;
end;
procedure THole.DecLimitTime;
begin
if fLimitTime > 0 then // 表示しているモグラがいたら
begin
Dec(fLimitTime, 100); // 表示時間を減らす
if fLimitTime = 0 then DeleteMog; // 表示時間を過ぎたら消去
end;
end;
procedure THole.DeleteMog;
begin
fExistence := false; // 穴が空きました。
// 次に、その穴から出現するモグラは、まだ叩かれていないを設定
fIsDead := false;
// もぐらを消します
Form1.Canvas.FillRect(Rect(HolePos.X,
HolePos.Y,
HolePos.X+fMogura.LiveBmp.Width,
HolePos.Y+fMogura.LiveBmp.Height));
end;
function THole.HoleIsFull: Boolean;
begin
Result := Existence;
end;
今回、もぐらが出現できる箇所は 5 つありますので、THole クラスのインスタンスも 5 つ作成することになります。