|
|
Title | Make a game where you control a cannon ball's angle and speed to try to hit a house |
Description | This example shows how to make a game where you control a cannon ball's angle and speed to try to hit a house in VB 6. |
Keywords | cannon, simulation, gravity, animation |
Categories | Algorithms, Graphics |
|
|
The program randomly places a target. Then when you click the Fire button, it gets angle and speed information for the cannon and enables a timer. To keep the graphics and timing simple, the program uses a scale of 1 pixel = 1 meter.
If you set the compile-time variable DEBUGGING to True, then the program draws the position where the cannon ball should ideally cross the Y coordinate where it was launched. If the ball started at ground level and the ground were flat, this is where the ball would land. It calculates this position using the formula:
Distance = 2 * V^2 * Sin(T) * Cos(T) / g
Here g is the acceleration due to gravity, which is 9.8 meters per second. The program redraws TICKS_PER_SECOND times per second so the acceleration per tick is 9.8/(TicksPerSecond^2).
|
|
Private Sub cmdFire_Click()
Dim speed As Single
' Redraw.
DrawField
' Get the speed.
On Error Resume Next
speed = CSng(txtSpeed.Text)
If Err.Number <> 0 Then
MsgBox "Invalid speed", vbExclamation, "Invalid " & _
"Speed"
Exit Sub
End If
On Error GoTo 0
If speed < 1 Then
MsgBox "Speed must be at least 1 mps", _
vbExclamation, "Invalid Speed"
Exit Sub
End If
' Get the speed components in meters per tick.
m_Vx = speed * Cos(m_Theta) / TICKS_PER_SECOND
m_Vy = -speed * Sin(m_Theta) / TICKS_PER_SECOND '
' Negative to go up.
' Disable UI elements.
cmdFire.Enabled = False
txtDegrees.Enabled = False
txtSpeed.Enabled = False
Screen.MousePointer = vbHourglass
DoEvents
#If DEBUGGING Then
' Draw the location where the cannon ball
' should pass the Y position where it started.
' Distance = 2 * V^2 * Sin(T) * Cos(T) / g = V^2 *
' Sin(2*T) / g
picCanvas.FillColor = vbBlue
picCanvas.Circle (m_BulletX + 2 * speed ^ 2 * _
Sin(m_Theta) * Cos(m_Theta) / 9.8, m_BulletY), _
CANNON_HGT / 2, vbBlue
#End If
' Start moving the cannon ball.
tmrMoveShot.Enabled = True
End Sub
|
|
The program keeps track of the cannon ball's position and velocity with the variables m_BulletX, m_BulletY, m_Vx, and m_Vy. When the timer event occurs, the program moves the cannon ball. It uses the velocity components to update the balls position and then redraws the ball.
|
|
' Acceleration in meters per tick squared.
Private Const m_YAcceleration As Single = 9.8 / _
TICKS_PER_SECOND ^ 2
Private m_BulletX As Single
Private m_BulletY As Single
Private m_Vx As Single
Private m_Vy As Single
Private Sub tmrMoveShot_Timer()
' Erase the cannon ball's previous position.
picCanvas.FillColor = picCanvas.BackColor
picCanvas.Circle (m_BulletX, m_BulletY), CANNON_HGT / _
2, picCanvas.BackColor
' Move the cannon ball.
m_Vy = m_Vy + m_YAcceleration
m_BulletX = m_BulletX + m_Vx
m_BulletY = m_BulletY + m_Vy
' Draw the new cannon ball.
picCanvas.Circle (m_BulletX, m_BulletY), CANNON_HGT / _
2, vbBlack
' See if we should stop.
If (m_BulletY > picCanvas.ScaleHeight) Or (m_BulletX > _
picCanvas.ScaleWidth) Then _
' Stop running.
tmrMoveShot.Enabled = False
' Re-enable UI elements.
cmdFire.Enabled = True
txtDegrees.Enabled = True
txtSpeed.Enabled = True
Screen.MousePointer = vbDefault
End If
End Sub
|
|
Some improvements you could make include hit detection to know when the ball hits the target and drawing a nice irregular ground.
|
|
|
|
|
|