Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So im doing the game snake for my class project, and i need to use the picturebox, im now stuck on a part that i cant resolve, in the Region "comida", private sub creat_mouse() my .Top and .Left creat an error that says

An unhandled exception of type ;System.NullReferenceException; occurred in WindowsApplication2.exe

Additional information: Object reference not set to an instance of an object.

i need help, i dont know what is wrong.

Sry bad english








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
    Dim r As Random

    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()
        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
        cabeça()
        Timer1.Start()
        create_mouse()
    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()
    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
#End Region

#Region "comida"
    Dim mouse As PictureBox
    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)


        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
End Class
Posted

1 solution

Change
Dim r As Random
to
Dim r As Random = New Random
 
Share this answer
 
Comments
Member 12194573 12-Dec-15 13:09pm    
it still does the same error
Member 12194573 12-Dec-15 13:10pm    
Ohhh thx alot, its good now

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