Public Class MainFrm
'Background Controls
Friend WithEvents tmr As New Timer
'Data
Public gScore As Double = 0
Private FallingObjs As New List(Of PictureBox)
Private HandledObjs As New List(Of PictureBox) 'storing which obj has arrived the line. And remove them.
'But in this demo, it's no need to use it as there will be only one object arrived the goal in a single detection.
'Unless, you give each objects a different velocity or acceleration.
Private Sub Project_OnLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Initialize the Timer
tmr.Interval = 120 'It indicates the falling speed of all objects.
tmr.Enabled = False
'Initialize Setting - These code will be generated by VB.Net.
'You may create these three controls yourself.
'Friend WithEvents pnlObjContainer As New Panel
'Friend WithEvents pnlHandlingLine As New Panel
'Friend WithEvents picPicker As New PictureBox
'And discomment the code below
'Me.BackColor = System.Drawing.Color.White
'Me.KeyPreview = True
'pnlObjContainer.BorderStyle = BorderStyle.FixedSingle
'pnlObjContainer.Size = New Size(700, 600)
'Me.Controls.Add(pnlObjContainer)
'pnlHandlingLine.Location = New Point(0, 500)
'pnlHandlingLine.Size = New Size(700, 100)
'pnlHandlingLine.BackColor = Color.Transparent
'pnlObjContainer.Controls.Add(pnlHandlingLine)
'picPicker.BackColor = Color.Black
'picPicker.Size = New Size(100, 82)
'picPicker.Location = New Point(300, 0)
'pnlHandlingLine.Controls.Add(picPicker)
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
'Handling Form.KeyDown of Command-Keys including arrow keys.
Select Case keyData
Case Keys.Left
If picPicker.Left > 0 Then picPicker.Left -= 20 'Move the objPicker
Case Keys.Right
If picPicker.Left < 600 Then picPicker.Left += 20 'Move the objPicker
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
If tmr.Enabled Then 'If (True) Then
tmr.Stop()
sender.text = "Start"
Else
tmr.Start()
sender.text = "Stop"
End If
End Sub
Private Sub FallingCheck(ByVal sender As Timer, ByVal e As System.EventArgs) Handles tmr.Tick
For Each Obj As PictureBox In FallingObjs
Obj.Top += 8 'Falling velocity
'Check
If Obj.Top > pnlHandlingLine.Top Then 'If the object has
'Marking
Dim Pos As UShort = Obj.Left + (Obj.Width / 2)
If (Pos > picPicker.Left) And (Pos < (picPicker.Left + picPicker.Width)) Then 'Detect the
gScore += 2
Else
gScore -= 1
End If
'Display Mark
lbScoreDisplay.Text = gScore
'Ready to remove the object
HandledObjs.Add(Obj)
End If
Next
'Remove Handled Objects
For Each Obj As PictureBox In HandledObjs
FallingObjs.Remove(Obj)
Obj.Dispose()
'Memory Manage
GC.Collect(GC.GetGeneration(Obj))
Next
HandledObjs.Clear()
'If FillingObjs.Count > 30 Then Return 'Exit Sub 'Limit the number of FallingObject to advoid hanging.
Randomize()
Select Case Rnd()
Case Is < 0.1 '1% Do this
Dim nwFallingObj As New PictureBox
With nwFallingObj
'.ImageLocation = "/img/fobj" & Fix(Rnd * 10) & ".png" '
'.SizeMode = PictureBoxSizeMode.AutoSize
'REM this two lines if you have images
.Size = New System.Drawing.Size(20, 20)
.BorderStyle = BorderStyle.FixedSingle
'you must set the size before you set the location as the location should be set based on the size of the object.
.Location = New Point(Fix(Rnd() * (pnlObjContainer.Width - nwFallingObj.Width)), -10)
End With
FallingObjs.Add(nwFallingObj)
pnlObjContainer.Controls.Add(nwFallingObj)
End Select
End Sub
'Run
End Class
★Kit↘..