Click here to Skip to main content
15,887,296 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
when i start the program the snake shold move but is dosent and i dont know why!

Public Class Form1

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

    Private Sub cabeça()
        comprimento_da_cobra += 1
        snake(comprimento_da_cobra) = New PictureBox
        With snake(comprimento_da_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(comprimento_da_cobra))
        snake(comprimento_da_cobra).BringToFront()

    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 tsnake_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        snake(0).Top += cima_baixo
        snake(0).Left += esquerda_direita
    End Sub
#End Region

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        cabeça()
        Timer1.Start()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


    End Sub
End Class


this is the code
and the grafic code is

<global.microsoft.visualbasic.compilerservices.designergenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <system.diagnostics.debuggernonusercode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <system.diagnostics.debuggerstepthrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.pb1 = New System.Windows.Forms.PictureBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.Label7 = New System.Windows.Forms.Label()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        CType(Me.pb1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'pb1
        '
        Me.pb1.BackColor = System.Drawing.Color.White
        Me.pb1.Location = New System.Drawing.Point(22, 22)
        Me.pb1.Name = "pb1"
        Me.pb1.Size = New System.Drawing.Size(750, 358)
        Me.pb1.TabIndex = 0
        Me.pb1.TabStop = False
        Me.pb1.WaitOnLoad = True
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(612, 423)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(51, 15)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Pontos"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(31, 401)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(112, 24)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "Instruçoes:"
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 72.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.Location = New System.Drawing.Point(201, 410)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(375, 108)
        Me.Label3.TabIndex = 3
        Me.Label3.Text = "SNAKE"
        '
        'Button1
        '
        Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button1.Location = New System.Drawing.Point(643, 476)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(129, 48)
        Me.Button1.TabIndex = 5
        Me.Button1.Text = "Começar"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Label6
        '
        Me.Label6.BackColor = System.Drawing.Color.White
        Me.Label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Label6.Location = New System.Drawing.Point(687, 410)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(85, 28)
        Me.Label6.TabIndex = 7
        '
        'Label7
        '
        Me.Label7.BackColor = System.Drawing.Color.Red
        Me.Label7.Location = New System.Drawing.Point(32, 425)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(76, 50)
        Me.Label7.TabIndex = 8
        Me.Label7.Text = "Teclas W,S,A,D para mover"
        '
        'Button2
        '
        Me.Button2.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button2.Location = New System.Drawing.Point(14, 478)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(129, 46)
        Me.Button2.TabIndex = 9
        Me.Button2.Text = "Sair"
        Me.Button2.UseVisualStyleBackColor = True
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.BackColor = System.Drawing.Color.Red
        Me.ClientSize = New System.Drawing.Size(789, 546)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Label7)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.pb1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.pb1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents pb1 As System.Windows.Forms.PictureBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Timer1 As System.Windows.Forms.Timer

End Class
Posted
Updated 9-Dec-15 0:12am
v4
Comments
Sergey Alexandrovich Kryukov 8-Dec-15 21:37pm    
You just need to write code accurately. Get rid of immediate constants like "d", "a", use arrow keys, get rid of a timer, create a separate thread running game scenario. Get rid of PictureBox, render graphics on some custom control developers specially for this purpose. Than we would have something to discuss. Fixing your code with all the problems I listed and elementary mix-up of logic and UI, and so on, just doesn't worth the effort. Start it all from scratch, this time in a neat and accurate way, then everything will work.
—SA
Ralf Meier 9-Dec-15 0:32am    
What is the sense of the method "cabeça" ?
For what do you need the 1000 PictureBoxes called snake ?
Member 12194573 9-Dec-15 5:29am    
i need to do it on a picturebox, its a school project and im a begginer
Ralf Meier 9-Dec-15 6:09am    
Then I would suggest that you first explain what should happen and then we see how it could be done.
But ... in my opinion you don't need 1000 Instances of Snake ... you need only the one you allready have and you should calculate the Location for this object.

What about answering my questions ?
Member 12194573 9-Dec-15 12:21pm    
The cabeça its the 1st part of the snake the "head"
the 2nd its because of the points she will get and the lenght of the snake.
I talked with my friend that is alot better then me in this and he didnt knew what was going on, then i deleted the buttons and it worked

1 solution

Are you seriously trying to create up to 1,000 PictureBoxes?? DO NOT DO THAT!! Your code will get slower and slower with the more boxes you create, to the point where you're app will be unusable.

You REALLY have to scrap this code and rewrite. I suggest dropping a Panel control on the form and subscribing to its Paint event. You have to Draw the boxes for your snake yourself using the Graphics object that is passed in by the Paint event args.

The reason your snake doesn't move when the app starts is your velocity variables are both 0 when the app starts.

In the future, wrap your code snippets in <PRE> ... code </PRE> tags to keep the formatting and keep the code readable. Also, you don't have to include all the designer generated code.
 
Share this answer
 
Comments
Member 12194573 9-Dec-15 5:23am    
OK, thx, about the 1st part, its a school project and ive not enough knowledge to that like u said, the snake dosnet move because the variables are 0 but i want them like that, it should move when i press my kyes (wasd) but it dosent.

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