This Excel VBA program demonstrates the principle of projectile in physics. At an angle and a launch velocity, the projectile can reach a certain range and certain height. The maximum range is at an angle of 45 degree. This principle can be applied in the military field where a missile can be launched at a velocity and an angle to hit a remote target. It can also be applied in other scientific and technological fields. This game provides a good training for students in their abilities in making estimation.
In this program, we use the formula v sin θ - ½gt2 as the vertical component of the displacement and v cos θ as the horizontal component of the displacement( where g is the gravitational acceleration , v the launch velocity and θ the launch angle). To enable the missile to fly, we use a combination of the Object.Move method and the object coordinate system , i.e. object. left and object.Top.
We also apply the random function Rnd to launch the objects at random positions at each new game. Besides that, we also keep track of the total score. The code to keep track of the total score is as follows:
Static Sub countscore() Dim myscore, Tscore As Integer myscore = Val(Lbl_Score.Caption) Tscore = Tscore + myscore Lbl_Total.Caption = Str(Tscore) End Sub
*Static is the declaration keyword that stores the memory even after a procedure is executed.
In the design stage, insert a Userform then insert a missle image, three objects and an explosion image. In addtion, we insert a text box for entering te velocity and another text box for entering the angle. Also, we inserted two label, one to show the current score and another one to display the total score.
The design interface is as shown below:
Private Sub Launch() Dim a As Variant Dim t As Variant Dim Y As Variant Dim w As Variant Dim i As Variant Dim score As Integer Dim left1, left2, left3, top1, top2, top3 As Variant Dim backgr As Integer Dim totalscore As Integer left1 = Int(Rnd * 200) + 100 left2 = Int(Rnd * 200) + 100 left3 = Int(Rnd * 200) + 100 top1 = Int(Rnd * 40) + 10 top2 = Int(Rnd * 40) + 10 top3 = Int(Rnd * 40) + 10 t = 0 Do v = Val(TxtVelocity.Text) a = Val(TxtAngle.Text) t = t + 1 Y = v * Sin(a * 3.141592654 / 180) * (t / 100) - 4.9 * ((t / 100) ^ 2) x = v * Cos(a * 3.141592654 / 180) * (t / 100) Image1.Move Image1.Left + x, Image1.Top - Y t = t + 1 DoEvents Loop Until t = 10 If Image4.Visible = True And (Image1.Left < left3 + 100 And Image1.Left > left3 - 100) And (Image1.Top < top3 + 300 And Image1.Top > top3 - 100) Then i = 2 Image5.Left = Image4.Left Image5.Top = Image4.Top Image5.Visible = True Image4.Visible = False Image1.Visible = False score = score + 50 ElseIf Image3.Visible = True And (Image1.Left < left2 + 100 And Image1.Left > left2 - 100) And (Image1.Top < top2 + 100 And Image1.Top > top2 - 100) Then i = 1 Image5.Left = Image3.Left Image5.Top = Image3.Top Image5.Visible = True Image3.Visible = False Image1.Visible = False score = score + 100 ElseIf Image2.Visible = True And (Image1.Left < left1 + 100 And Image1.Left > left1 - 100) And (Image1.Top < top1 + 100 And Image1.Top > top1 - 100) Then i = 0 Image5.Left = Image2.Left Image5.Top = Image2.Top Image5.Visible = True Image2.Visible = False Image1.Visible = False score = score + 200 End If Lbl_Score.Caption = Str(score) countscore End Sub Static Sub countscore() Dim myscore, Tscore As Integer myscore = Val(Lbl_Score.Caption) Tscore = Tscore + myscore Lbl_Total.Caption = Str(Tscore) End Sub Private Sub Cmd_launch_Click() Launch End Sub Private Sub Cmd_Reset_Click() Image1.Visible = True left1 = Int(Rnd * 200) + 100 left2 = Int(Rnd * 200) + 100 left3 = Int(Rnd * 200) + 100 top1 = Int(Rnd * 40) + 10 top2 = Int(Rnd * 40) + 10 top3 = Int(Rnd * 40) + 10 Image2.Left = left1 Image3.Left = left2 Image4.Left = left3 Image2.Top = top1 Image3.Top = top2 Image4.Top = top3 Image1.Left = 48 Image1.Top = 150 Image2.Visible = True Image3.Visible = True Image4.Visible = True Image5.Visible = False End Sub