Click here to Skip to main content
15,887,346 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So when i start the game and press the button 1, the snake dies immediately, why is that so?

Ps: ive to do it on picturebox
Public Class Form1

#Region "Snake"
    Dim snake(1000) As PictureBox
    Dim comp_cobra As Integer = -1
    Dim esquerda_direita As Integer = 0
    Dim cima_baixo As Integer = 0


    Private Sub cabeça()
        comp_cobra += 1
        snake(comp_cobra) = New PictureBox
        With snake(comp_cobra)
            .Height = 10
            .Width = 10
            .BackColor = Color.Black
            .Top = (pb1.Top + pb1.Bottom) / 2
            .Left = (pb1.Left + pb1.Right) / 2
        End With
        Me.Controls.Add(snake(comp_cobra))
        snake(comp_cobra).BringToFront()

        compi_cobra()
    End Sub

    Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress

        Select Case e.KeyChar
            Case "d"
                esquerda_direita = 10
                cima_baixo = 0
            Case "a"
                esquerda_direita = -10
                cima_baixo = 0
            Case "w"
                esquerda_direita = 0
                cima_baixo = -10
            Case "s"
                esquerda_direita = 0
                cima_baixo = 10

        End Select
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        
    End Sub

    Private Sub compi_cobra()
        comp_cobra += 1
        snake(comp_cobra) = New PictureBox
        With snake(comp_cobra)
            .Height = 10
            .Width = 10
            .BackColor = Color.Black
            .Top = snake(comp_cobra - 1).Top
            .Left = snake(comp_cobra - 1).Left + 10
        End With
        Me.Controls.Add(snake(comp_cobra))
        snake(comp_cobra).BringToFront()
    End Sub
    Private Sub tsnake_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        For i = comp_cobra To 1 Step -1
            snake(i).Top = snake(i - 1).Top
            snake(i).Left = snake(i - 1).Left
        Next
        snake(0).Top += cima_baixo
        snake(0).Left += esquerda_direita
        colide_parede()
        comer_comida()
        colide_cobra()
    End Sub
#End Region

#Region "Colisão"
    Private Sub colide_parede()
        If snake(0).Left < pb1.Left Then
            Timer1.Stop()
            MsgBox("Perdeste Fagg")
        End If
        If snake(0).Right > pb1.Right Then
            Timer1.Stop()
            MsgBox("Perdeste Fagg")
        End If
        If snake(0).Bottom > pb1.Bottom Then
            Timer1.Stop()
            MsgBox("Perdeste Fagg")
        End If
        If snake(0).Top < pb1.Top Then
            Timer1.Stop()
            MsgBox("Perdeste Fagg")
        End If
    End Sub
    Private Sub colide_cobra()
        For i = 1 To comp_cobra
            If snake(0).Bounds.IntersectsWith(snake(i).Bounds) Then
                Timer1.Stop()
                MsgBox("Perdeste")
            End If
        Next
    End Sub
#End Region

#Region "comida"
    Dim mouse As PictureBox
    Dim r As Random = New Random
    Private Sub create_mouse()
        mouse = New PictureBox
        With mouse
            .Width = 10
            .Height = 10
            .BackColor = Color.Red
            .Top = r.Next(pb1.Top, pb1.Left)
            .Left = r.Next(pb1.Left, pb1.Right)
        End With
        Me.Controls.Add(mouse)
        mouse.BringToFront()


    End Sub
    Private Sub comer_comida()
        If snake(0).Bounds.IntersectsWith(mouse.Bounds) Then
            compi_cobra()
            mouse.Top = r.Next(pb1.Top, pb1.Bottom - 10)
            mouse.Left = r.Next(pb1.Left, pb1.Right - 10)
            Label2.Text = Label2.Text + 10


        End If

    End Sub
#End Region

    Private Sub Button1_Click(sender As Object, e As EventArgs)


    End Sub

    Private Sub pb1_Click(sender As Object, e As EventArgs) Handles pb1.Click

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        cabeça()
        Timer1.Start()
        create_mouse()
    End Sub

    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
        Label2.Text = 0
    End Sub
End Class
Posted
Comments
PIEBALDconsult 12-Dec-15 13:40pm    
Lack of oxygen?
Member 12194573 12-Dec-15 13:41pm    
rly man? im a noob here and im comfused, dont coment if its not to help
PIEBALDconsult 12-Dec-15 13:50pm    
You don't get to say what types of comment may and may not be made.
Humor is meant to relieve stress; many times a developer gets bogged down in some issue and a little shot of humor allows the brain to see things differently.
Member 12194573 12-Dec-15 13:58pm    
well, thats true but im rly mad, because the program is trolling me :/, but u are right ive not the right to say what type of comments people post, was just that i was confused and got happy because someone responded to my post and then was just a joke
PIEBALDconsult 12-Dec-15 14:01pm    
I suspect you have a great many years of programs trolling you to look forward to -- twice in the last week a WinForms app I'm writing just wouldn't do what I wanted.

1 solution

Please see my comment to the question. You need to use some set of facilities. 1) to express parallelism in behavior of actors, such as the user/player and characters like snakes, 2) calculate change in states expressing the scenario of game, 3) calculate "physics", such as collisions, 4) calculate graphical scene, 5) render it.

In most or all of the aspects, you are choosing wrong or inappropriate facility, or miss some. Below, I'll try to cover just some.

Using PictureBox does not give you any help, only adds problems, uses some resources you don't need to use and wastes your development time. It happens each time you try to use this purely redundant control to anything except rendering static images. The problem is easily solved by rendering graphics from scratch. I explained it all in my past answers:
Append a picture within picturebox[^],
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
rendering:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^],
with threads: How to speed up my vb.net application?[^].

See also:
How to avoid Red Cross in DatagridView C#[^],
any way to change a picture at run time with animation[^],
how to make a file from the content of e.graphics[^].

Some of the links above already explain how to work with threads, but here you need to take a step back and get to using them in general. First of all, avoid timers. Haven't you already face such situation where you have a timer event triggered when the previous event is not yet handled? With timers, sooner or later, you will. And so on… With threads, there are no such problems. It's important that you should not rely on any fixed period of time. You should take real time value from System.DateTime.Now or, with better accuracy, relative timing with System.Diagnostics.Stopwatch:
https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx[^].

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke(),
Problem with Treeview Scanner And MD5.

See also more references on threading:
.NET event on main thread,
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading.

Also, please see this answer where I explain how can you smoothly exchange data between threads and encapsulate it in a thread-safe way:
How to pass ref parameter to the thread[^],
Change parameters of thread (producer) after it is started[^],
MultiThreading in C#[^],
Passing arguments to a threaded LongRunningProcess[^],
Running exactly one job/thread/process in webservice that will never get terminated (asp.net)[^],
Making Code Thread Safe[^].

Long live snakes!

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900