Hello, I Have a Lag Problem With my Code My Snake Game
It Will Move With Timer Tick 100 interval
And Each Timer It Move
It Will Use A Lot Of Code In Procedure It LAGGY
My Touch Code Use This

Function Touched(ByVal obj1 As String, ByVal obj2 As String) As Boolean
        If Me.Controls(obj1).Location = Me.Controls(obj2).Location Then
            Touched = True
        End If
        If Me.Controls(obj1).Location.X > Me.Controls(obj2).Location.X - Me.Controls(obj1).Size.Width And Me.Controls(obj1).Location.X < _
        Me.Controls(obj2).Location.X + Me.Controls(obj2).Size.Width Then
            If Me.Controls(obj1).Location.Y > Me.Controls(obj2).Location.Y - Me.Controls(obj1).Size.Height And Me.Controls(obj1).Location.Y < _
            Me.Controls(obj2).Location.Y + Me.Controls(obj2).Size.Height Then
                Touched = True
            End If
        End If
        Return Touched
    End Function

It NorMally Work But My Code is So long . Cuz My Snake Game Have A Lot Of Thing
Each Move I Use This Code

Function moved()
        If Touched("pictureBox1", "stone1") = True Or Touched("pictureBox1", "stone2") = True Or Touched("pictureBox1", "stone3") = True Then
            DIE()
        End If
        If Touched("food", "stone1") = True Or Touched("food", "stone2") = True Or Touched("food", "stone3") = True Then
            FoodReLoc()
        End If
        If Touched("PictureBox1", "Food") = True Then
            Eated()
        End If
        If PillAp = False Then
            If Touched("PictureBox1", "Pill") = True Then
                Eated_Pill(1)
            End If
            If Touched("pictureBox1", "Pill2") Then
                Eated_Pill(2)
            End If
            EXPBARRESIZE()
        End If
        LEVEL_UP()
        Length = Collen
        Do While Length > 1
            Me.Controls("PictureBox" & Length).Location = Me.Controls("PictureBox" & Length - 1).Location
            Length -= 1
        Loop
        Length = Collen
        Select Case D
            Case 1
                PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 10)
            Case 2
                PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 10)
            Case 3
                PictureBox1.Location = New Point(PictureBox1.Location.X - 10, PictureBox1.Location.Y)
            Case 4
                PictureBox1.Location = New Point(PictureBox1.Location.X + 10, PictureBox1.Location.Y)
        End Select
        If PictureBox1.Location.X >= Area.Location.X + Area.Size.Width Then
            PictureBox1.Location = New Point(Area.Location.X, PictureBox1.Location.Y)
        ElseIf PictureBox1.Location.X < Area.Location.X Then
            PictureBox1.Location = New Point(Area.Location.X + Area.Size.Width - PictureBox1.Width, PictureBox1.Location.Y)
        End If
        If PictureBox1.Location.Y >= Area.Location.Y + Area.Size.Height Then
            PictureBox1.Location = New Point(PictureBox1.Location.X, Area.Location.Y)
        ElseIf PictureBox1.Location.Y < Area.Location.Y Then
            PictureBox1.Location = New Point(PictureBox1.Location.X, Area.Location.Y + Area.Size.Height - PictureBox1.Height)
        End If
        Do While Length > 1
            If PictureBox1.Location = Me.Controls("PictureBox" & Length).Location Then
                DIE()
                Return 0
                Exit Function
            End If
            Length -= 1
        Loop

        Length = Collen
        Return 0
    End Function

It Have A Lot Of Thing In My Game SKill Pill Stone and Mapsize
Each Time It detect Touch Of Every Thing Pill Stone And Get Go Throught Map Will go to other size


Thanks For Help And Advice Me ...

Dani AI

Generated

Good points from . The main sources of the lag in the shown code are the repeated string lookups through the Controls collection, many per-tick allocations/concatenations, and running many separate collision calls each timer tick. A faster, clearer design is to build typed collections once (stones, pills, body parts) and then run a single, simple collision pass that uses rectangle intersection checks.

A compact pattern to follow (built once at startup, then used each tick):

' build lists once (Form_Load)
stonesList = New List(Of PictureBox) From {stone1, stone2, stone3}
pillsList  = New List(Of PictureBox) From {pill1, pill2}
' on timer tick, compute head rectangle and test
Dim headRect As Rectangle = New Rectangle(headPicture.Location, headPicture.Size)
For Each s As PictureBox In stonesList
    If headRect.IntersectsWith(s.Bounds) Then
        Die()
        Exit Sub
    End If
Next

This removes repeated string creation and collection lookups and uses the fast Rectangle.IntersectsWith test. Build and maintain the body-parts list as the snake grows so self-collision becomes one loop over a list instead of repeated Me.Controls calls.

Additional tips: measure where time is spent with System.Diagnostics.Stopwatch to confirm hotspots. Exit collision loops early when a collision is found. Reuse lists and temporary Rectangle variables to avoid garbage churn. Reduce painting work (double buffering / optimized painting) so UI updates do not block the logic loop; Microsoft guidance on reducing flicker is helpful. For API details see Rectangle.IntersectsWith and Control.Bounds. For painting/flicker tips see .

Recommended Answers

All 2 Replies

1) To save a few, you can use indexes instead of keys when referencing controls.
2) Passing byvalue needs to copy the data to the stack. As larger the data is, slower response time.
3) Creating a new string each time you want to reference a control to use it as key for search is very time consuming. Maybe you can consider using string constants
4) The Me.Controls is just a generic collection. If you are aware of the controls in the collection (not dinamyc, or always under programmer control), you can create an array of controls, referencing the controls on the collection instead, or just manage the controls them selfs. Accessing an array element is faster than accesing it throug a collection. Accessing the control itself it the fastest.

Hope this helps.

Thanks lolafuertes

How Could I use Index and i'm only 13 i don't understand at all ?
Does it Has Anything detect Touch Code ?
Could You Explain Use Of Code ?
Thanks A lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.