quickly looping through form's controls collection

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
still playing with that nibbles game, i've got the snake moving around the window and getting longer when it hits the number that's randomly placed on the screen

but what i need to do is have it checking with every move (everytime the tick event fires) to see if the snake's crossed it's own path

this is what i currently have (of which it doesnt seem to work)
VB.NET:
'tmrTimer.Interval = 50   btw
Private blnSnakeCrossed As Boolean = False

Private Sub tmrTimer_Tick(...) Handles tmrTimer.Tick
	blnSnakeCrossed = CheckSnake()
	If blnSnakeCrossed = True Then tmrTimer.Enabled = False
End Sub

Private Function CheckSnake() As Boolean
	Dim intCounter As Integer
	'I do need the counter to start at 2 instead of 0 as the first two items is picDot and lblNumber
	For intCounter = 2 To Me.Controls.Count - 1
		If TypeOf (Me.Controls(intCounter)) Is PictureBox Then
			If picDot.Top = CType(Me.Controls(intCounter), PictureBox).Top And picDot.Left = CType(Me.Controls(intCounter), PictureBox).Left Then
				Return True
			Else
				Return False
			End If
		End If
	Next
End Function
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,335
Location
Sydney, Australia
Programming Experience
10+
Your code looks like it should work, assuming that your picture boxes all use the same origin for each grid square. Looks to me like you need to slow your timer down considerably and step through CheckSnake at each tick.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
i had the Return False in the wrong spot this works correctly:
VB.NET:
 Private Function CheckSnake() As Boolean
		Dim intCounter As Integer
		For intCounter = 2 To Me.Controls.Count - 2
			If TypeOf (Me.Controls(intCounter)) Is PictureBox Then
			    If picDot.Top = CType(Me.Controls(intCounter), PictureBox).Top And picDot.Left = CType(Me.Controls(intCounter), PictureBox).Left Then
					Return True
				End If
			End If
		Next intCounter
		Return False
	End Function

fyi Return statements exits the function at that spot, it'll never reach the End Function spot once it hits the Return statement, so before it was always exiting with a False and hitting a constant loop which is why my snake just stopped
 
Top Bottom