Part 4 - Form Events
Next
we will need to handle some form events. When the user clicks the mouse
on the form or presses a key it will cause the application to quit so
copy and paste the fallowing code into Form1
- Private Sub Form_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
- Me.Close()
- End Sub
- Private Sub Form_KeyDown(ByVal sender As Object, _
- ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
- Me.Close()
- End Sub
We
will also want the ability to drag a scent trail on the scene by using
the mouse. To do this, set the player position to the position of the
mouse as it moves across the form. Copy and paste the code below into
Form1
- Private Sub Form_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseMove
- mobjPlayer.Position = New Point(e.X, e.Y)
- End Sub
Next
we will need to perform a check to see if the form is closing so we can
dispose of the variables we have declared. We do this using the
FormClosing event. If the form is not being canceled we can clean up
our variables by calling the DoCleanUp method. Copy and paste the code
below into Form1
- Private Sub Form1_FormClosing(ByVal sender As Object, _
- ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
- If Not e.Cancel Then Me.DoCleanUp()
- End Sub
- Private Sub DoCleanUp()
- mobjTimer.Stop()
- mobjTimer = Nothing
- mobjStinkySpawner.Stop()
- mobjStinkySpawner = Nothing
- mobjFlyUpdater.Stop()
- mobjFlyUpdater = Nothing
- mobjScents.Clear()
- mobjScents = Nothing
- mobjPlayer = Nothing
- mobjFlies.Clear()
- mobjFlies = Nothing
- mobjGraphics.Dispose()
- mobjGraphics = Nothing
- End Sub
Finally we can add code to the forms Load event. Copy and paste the code below into Form1.
- Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- ' Resize form so that client size is 512x512
- Me.Size = New Size(512, 512) + (Me.Size - Me.ClientSize)
- ' create player object
- mobjPlayer = New Actor
- mobjPlayer.Position = New Point(Me.ClientSize.Width \ 2, Me.ClientSize.Height \ 2)
- mobjPlayer.Size = 10
- ' create flies and scent collections
- mobjFlies = New Generic.List(Of Fly)
- mobjScents = New Generic.List(Of Scent)
- ' add flies and receptors (Changing receptor values of one fly will change receptor of all flies)
- Dim R As New Generic.List(Of Receptor)
- R.Add(New Receptor(10, -(Math.PI / 4), 5))
- R.Add(New Receptor(10, Math.PI / 4, 5))
- Randomize(Now.Ticks)
- For idx As Integer = 0 To NumberOfFlies - 1
- Dim F As Fly
- F = New Fly(New Point(CInt(Rnd() * Me.ClientSize.Width), CInt(Rnd() * Me.ClientSize.Height)), R)
- F.Direction = CSng(Rnd() * (Math.PI * 2))
- mobjFlies.Add(F)
- Next
- ' Create graphics
- Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint, True)
- Drawing.BufferedGraphicsManager.Current.MaximumBuffer = New Size(1, 1) + Me.ClientSize
- mobjGraphics = Drawing.BufferedGraphicsManager.Current.Allocate(Me.CreateGraphics, Me.ClientRectangle)
- ' setup timers
- mobjTimer = New Timers.Timer
- mobjTimer.Interval = 1
- mobjTimer.AutoReset = False
- mobjTimer.Start()
- mobjStinkySpawner = New Timers.Timer
- mobjStinkySpawner.Interval = SpawnStinkyInterval
- mobjStinkySpawner.AutoReset = False
- mobjStinkySpawner.Start()
- mobjFlyUpdater = New Timers.Timer
- mobjFlyUpdater.Interval = UpdateFliesInterval
- mobjFlyUpdater.AutoReset = False
- mobjFlyUpdater.Start()
- End Sub
The
first thing we do is resize the form so the it’s client area is 512
wide by 512 high. Second we create the player and position it in the
center of the form, as well as set it’s size to 10. The next thing is
to create the flies and scent collections.
Next we create our
flies. But before we do that we create a new collection that will
contain the flies Receptor’s. The Receptor collection will have two
receptors added to it. The first receptor will be a distance of 10 from
the position of the fly, and facing -45 degrees from the direction the
fly is facing. We also specify that the receptor has a size value of 5.
The second receptor is the same as the first except that it will be +45
Degrees to the right from the direction the fly is facing. Keep in mind
that every fly in the scene will be referencing these same 2 receptors
that were declared. Each fly does not hold it’s own unique collection
of receptors.
Now that we have a collection of receptors we can
begin creating the flies. Each new fly we create will be randomly
placed on the form and given a random starting direction.
After
the flies have been created we specify that we want to control the
painting on the form by calling the SetStyle method. Second we need to
specify the maximum size of the buffer we will be drawing to. And third
we allocate a new BufferedGraphics object by calling the allocate
method and passing in a new graphics object that was created by the
form, as well as the area on the form we will be drawing to.
The
next thing to do is create the timer objects. Each timer has been setup
to begin running, and after the interval of time has elapsed will raise
it’s Elapsed event once.