Post

 Resources 

Console

Fart Sniffer


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

  1. Private Sub Form_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
  2.    Me.Close()
  3. End Sub
  4. Private Sub Form_KeyDown(ByVal sender As Object, _
  5.                  ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
  6.    Me.Close()
  7. 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

  1. Private Sub Form_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseMove
  2.    mobjPlayer.Position = New Point(e.X, e.Y)
  3. 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

  1. Private Sub Form1_FormClosing(ByVal sender As Object, _
  2.    ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  3.    If Not e.Cancel Then Me.DoCleanUp()
  4. End Sub
  5. Private Sub DoCleanUp()
  6.    mobjTimer.Stop()
  7.    mobjTimer = Nothing
  8.    mobjStinkySpawner.Stop()
  9.    mobjStinkySpawner = Nothing
  10.    mobjFlyUpdater.Stop()
  11.    mobjFlyUpdater = Nothing
  12.    mobjScents.Clear()
  13.    mobjScents = Nothing
  14.    mobjPlayer = Nothing
  15.    mobjFlies.Clear()
  16.    mobjFlies = Nothing
  17.    mobjGraphics.Dispose()
  18.    mobjGraphics = Nothing
  19. End Sub


Finally we can add code to the forms Load event. Copy and paste the code below into Form1.

  1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.    ' Resize form so that client size is 512x512
  3.    Me.Size = New Size(512, 512) + (Me.Size - Me.ClientSize)
  4.    ' create player object
  5.    mobjPlayer = New Actor
  6.    mobjPlayer.Position = New Point(Me.ClientSize.Width \ 2, Me.ClientSize.Height \ 2)
  7.    mobjPlayer.Size = 10
  8.    ' create flies and scent collections
  9.    mobjFlies = New Generic.List(Of Fly)
  10.    mobjScents = New Generic.List(Of Scent)
  11.    ' add flies and receptors (Changing receptor values of one fly will change receptor of all flies)
  12.    Dim R As New Generic.List(Of Receptor)
  13.    R.Add(New Receptor(10, -(Math.PI / 4), 5))
  14.    R.Add(New Receptor(10, Math.PI / 4, 5))
  15.    Randomize(Now.Ticks)
  16.    For idx As Integer = 0 To NumberOfFlies - 1
  17.       Dim F As Fly
  18.       F = New Fly(New Point(CInt(Rnd() * Me.ClientSize.Width), CInt(Rnd() * Me.ClientSize.Height)), R)
  19.       F.Direction = CSng(Rnd() * (Math.PI * 2))
  20.       mobjFlies.Add(F)
  21.    Next
  22.    ' Create graphics
  23.    Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint, True)
  24.    Drawing.BufferedGraphicsManager.Current.MaximumBuffer = New Size(1, 1) + Me.ClientSize
  25.    mobjGraphics = Drawing.BufferedGraphicsManager.Current.Allocate(Me.CreateGraphics, Me.ClientRectangle)
  26.    ' setup timers
  27.    mobjTimer = New Timers.Timer
  28.    mobjTimer.Interval = 1
  29.    mobjTimer.AutoReset = False
  30.    mobjTimer.Start()
  31.    mobjStinkySpawner = New Timers.Timer
  32.    mobjStinkySpawner.Interval = SpawnStinkyInterval
  33.    mobjStinkySpawner.AutoReset = False
  34.    mobjStinkySpawner.Start()
  35.    mobjFlyUpdater = New Timers.Timer
  36.    mobjFlyUpdater.Interval = UpdateFliesInterval
  37.    mobjFlyUpdater.AutoReset = False
  38.    mobjFlyUpdater.Start()
  39. 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.



Next: Part 5 - Timer events | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11

Post a Comment


Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.
There have been 45 visitors within the last 20 minutes
RSS News Feed