キャッチゲームの制作 VB6
 (7) リンゴ キャッチの判定 前へ 目次へ 次へ 

 リンゴを網で受け取れたかどうかを判定する。キャッチの判定は、「リンゴの中心座標が網の画像内にあれば」キャッチされたこととする。

 まず、リンゴの中心座標(applex, appley)を計算する。

 リンゴの中心のx座標applexは、画像のLeftプロパティの値に画像の幅であるWidthプロパティの値の1/2を加算した値となる。画像とは、ここではリンゴの絵、すなわちimgApple(n)オブジェクトのことである。計算式は次のようになる。

applex = imgApple(n).Left + imgApple(n).Width / 2

 同じように、リンゴの中心のy座標appleyは、画像のTopプロパティの値に画像の高さであるHeightプロパティの値の1/2を加算した値となる。計算式は次のようになる。

appley = imgApple(n).Top + imgApple(n).Height / 2


リンゴの中心のx座標applexが網のx座標の範囲内にあり、かつ、リンゴ中心のy座標applyが網のy座標の範囲内にあれば、キャッチされたと判定する。

 網のx座標の範囲は、

  imgApple(n).Left 〜 imgApple(n).Left + imgApple(n).Width

 網のy座標の範囲は、

  imgApple(n).Top 〜 imgApple(n).Top + imgApple(n).Height

となる。

 リンゴがキャッチされたときは、@得点を加算してA得点を表示し、Bリンゴを非表示にする。

 これらの一連の処理は、リンゴを移動する処理の次に行うことになる。

・プログラムリスト(追加)

Private Sub Timer1_Timer()
    Dim n As Integer
    Dim applex As Integer, appley As Integer    'リンゴの中心座標用

    'Apple発生
      … 省略 …
    'Apple移動等
    For n = 1 To MaxApple
        If imgApple(n).Visible = True Then    '表示のリンゴだけ
            'Apple移動
            imgApple(n).Top = imgApple(n).Top + Speed(n)
            imgApple(n).Move imgApple(n).Left, imgApple(n).Top
            'Apple中心座標(applex, appley)計算
            appley = imgApple(n).Top + imgApple(n).Height / 2
            applex = imgApple(n).Left + imgApple(n).Width / 2
            'Appleキャッチ判定
            If appley > imgNet.Top And appley < imgNet.Top + imgNet.Height And _
              applex > imgNet.Left And applex < imgNet.Left + imgNet.Width Then
                Tensuu = Tensuu + 10            '点数加算 @
                lblTokuten.Caption = Tensuu    '点数表示 A
                imgApple(n).Visible = False    'リンゴ非表示 B
            End If
            'Apple地面に到達
            If imgApple(n).Top > Form1.ScaleHeight Then
                imgApple(n).Visible = False    'リンゴ非表示
            End If
        End If
    Next n
End Sub

 点数用の変数Tensuuをグローバル変数とするため (General) (Declarations)に宣言を追加する。

Dim Tensuu As Integer

 Form_Loadプロシージャで点数を0で初期化する。Randomizeの次の行にでも追加しておく。

Tensuu = 0


 実行すると、リンゴが落下し、リンゴをキャッチすると得点が10点ずつ加算される。ただし、プログラムは終了することがない。


 (7) リンゴ キャッチの判定 前へ 目次へ 次へ 
Copyright © 2001,2002 Hiroshi Masuda 

 

 

inserted by FC2 system