none
Help with Snake Game

    Question

  • I am trying to make a simple Snake Game for my little sister, and got a source code and tutorial online, but I keep getting 'NullReferenceException was unhandled' at the block below, and the original poster has abandoned the thread already:

    snakeBody(1).Location = pbxSnake.Location

    These are the codes (I was messing around with it and some comment were deleted, sorry):

    Public Class Game
    
        Dim score As Integer = 0
        Dim time As Integer = 0
        Dim appleX As Integer
        Dim appleY As Integer
        Dim snakeUP As Integer = False
        Dim snakeDOWN As Integer = False
        Dim snakeLEFT As Integer = False
        Dim snakeRIGHT As Integer = True
        Dim extraBody As Integer = 5
        Dim x As Integer
        Dim snakeBody(200) As PictureBox
        Dim pause As Boolean = False
    
        Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    
            'Starts the game when the button is click.
    
            tmrTime.Enabled = True
            tmrMove.Enabled = True
    
            'START() is a sub routine that will trigger when the button is click.
            START()
        End Sub
    
        Private Sub START()
    
            'Make the pictureboxes visible, and the button hidden.
            pbxApple.Visible = True
            pbxSnake.Visible = True
            btnStart.Visible = False
    
            Me.Focus()
            MoveSnake()
            NewApple()
    
            'This code adds an extra body to our snake.
            For Me.x = 1 To extraBody
                'A new picturebox
                snakeBody(x) = New PictureBox()
                'The picture snake, which we'll be using for on the picturebox.
                snakeBody(x).Image = My.Resources.Snake
                'The width of the new picturebox.
                snakeBody(x).Width = 15
                'The height of the new picturebox.
                snakeBody(x).Height = 15
                'The backcolour of the picturebox.
                snakeBody(x).BackColor = Color.Transparent
                'The location of the new body from the left.
                snakeBody(x).Left = 120 - (15 * x)
                'The location of the new body from the top.
                snakeBody(x).Top = 180
                'The sizemode of our new picturebox.
                snakeBody(x).SizeMode = PictureBoxSizeMode.StretchImage
                'This tells the control to add it to our snakeBody.
                Controls.Add(snakeBody(x))
            Next
    
        End Sub
    
        Private Sub NewApple()
            'The Randomize calls the system's pseudorandom list. A sequence that exhibit statistical randomness while generating an entirely deterministic causal process.
            Randomize()
    
            'Gives the two variable random numbers between 0 to 24.
            appleY = Rnd() * 24
            appleX = Rnd() * 24
    
            'Gives the picturebox 'pbxApple' a new and random location on the form.
            'The multiplication by 15 puts the picturebox on the form in a random spot on a 25x25 grid.
            pbxApple.Location = New Point(appleX * 15, appleY * 15)
    
            For Me.x = 1 To extraBody
                If pbxApple.Location = snakeBody(x).Location Then
                    NewApple()
                End If
            Next
    
        End Sub
    
        Private Sub tmrTime_Tick(sender As Object, e As EventArgs) Handles tmrTime.Tick
    
            'Adds one to the variable 'time' each time the timer ticks.
            time += 1
    
        End Sub
    
        Private Sub tmrMove_Tick(sender As Object, e As EventArgs) Handles tmrMove.Tick
    
            'This code, MoveSnake() calls the private sub called MoveSnake.
            MoveSnake()
    
            'Me.text gives this form (Me.text) the this in quotes and the ampersand symbol allows us to go from string variable and so on.
            Me.Text = "Score:" & " " & score & " " & "Time:" & time & " " & "-Press P to Pause-"
    
        End Sub
    
        Private Sub MoveSnake()
    
    
            'This makes the extra body parts move towards the location of the first body part.
            snakeBody(1).Location = pbxSnake.Location
    
            'Moves the snake up.
            If snakeUP = True Then
                pbxSnake.Top += -15
            End If
    
            'Moves the snake down.
            If snakeDOWN = True Then
                pbxSnake.Top += 15
            End If
    
            'Moves the snake right.
            If snakeRIGHT = True Then
                pbxSnake.Left += 15
            End If
            'Moves the snake left.
            If snakeLEFT = True Then
                pbxSnake.Left += -15
            End If
    
            x = extraBody
    
            'Moves the new body part behind the existing ones.
            Do Until snakeBody(2).Location = snakeBody(1).Location
                snakeBody(x).Location = snakeBody(x - 1).Location
                x -= 1
            Loop
    
            snakeBody(extraBody).Visible = True
            checkApple()
            checkCollision()
    
        End Sub
    
        Private Sub Game_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    
            'Detects when the player press a key on the keyboard.
    
            If e.KeyValue = Keys.Right And snakeLEFT = False Then
                snakeRIGHT = True
                snakeUP = False
                snakeDOWN = False
            End If
    
            If e.KeyValue = Keys.Left And snakeRIGHT = False Then
                snakeLEFT = True
                snakeUP = False
                snakeDOWN = False
            End If
    
            If e.KeyValue = Keys.Up And snakeDOWN = False Then
                snakeUP = True
                snakeLEFT = False
                snakeRIGHT = False
            End If
    
            If e.KeyValue = Keys.Down And snakeUP = False Then
                snakeDOWN = True
                snakeLEFT = False
                snakeRIGHT = False
            End If
    
            If e.KeyValue = Keys.P And pause = False Then
                pause = True
                lblPause.Visible = True
                tmrTime.Enabled = False
                tmrMove.Enabled = False
                Me.Text = "Score:" & " " & score & " " & "Time:" & time & " " & "-Press SPACE to Continue-"
            End If
    
            If e.KeyValue = Keys.Space And pause = True Then
                Me.Text = "Score:" & " " & score & " " & "Time:" & time & " " & "-Press P to Pause-"
                pause = False
                lblPause.Visible = False
                tmrTime.Enabled = True
                tmrMove.Enabled = True
            End If
    
        End Sub
    
        Private Sub checkApple()
    
            If pbxSnake.Location = pbxApple.Location Then
                appleEaten()
            End If
    
        End Sub
    
        Private Sub appleEaten()
    
            NewApple()
            score += 1
            extraBody += 1
            snakeBody(extraBody) = New PictureBox()
            snakeBody(extraBody).Image = My.Resources.Snake
            snakeBody(extraBody).Width = 15
            snakeBody(extraBody).Height = 15
            snakeBody(extraBody).BackColor = Color.Transparent
            snakeBody(extraBody).SizeMode = PictureBoxSizeMode.StretchImage
            snakeBody(extraBody).Visible = False
            Controls.Add(snakeBody(extraBody))
    
        End Sub
    
        Private Sub checkCollision()
    
            For Me.x = 1 To extraBody
                If snakeBody(x).Location = pbxSnake.Location Then
                    hitBody()
                End If
            Next
    
            If pbxSnake.Left < Me.ClientRectangle.Left Or
            pbxSnake.Top < Me.ClientRectangle.Top Or
            pbxSnake.Top + pbxSnake.Right > Me.ClientRectangle.Top + Me.ClientRectangle.Right Or
            pbxSnake.Left + pbxSnake.Width > Me.ClientRectangle.Left + Me.ClientRectangle.Width Then
                hitWall()
            End If
        End Sub
    
        Private Sub hitBody()
    
            tmrTime.Enabled = False
            tmrMove.Enabled = False
            MessageBox.Show("You collided with your own body, you lose the game!", "Game Over")
            Application.Restart()
    
        End Sub
    
        Private Sub hitWall()
    
            tmrTime.Enabled = False
            tmrMove.Enabled = False
            MessageBox.Show("You collided with the wall, you lose the game!", "Game Over")
            Application.Restart()
    
        End Sub
    End Class
    Thursday, October 17, 2013 8:33 PM

Answers

  • The immediate problem is that the snake is moved before it is initialised.  You need to move these lines
            Me.Focus()
            MoveSnake
    ()
            NewApple
    ()
    to the end of the Start method so that the subs have a snake to work with.  But with such an elementary error in the code it is possible that that change is only the start of your debugging.

    Thursday, October 17, 2013 9:00 PM
  • Oh it work now! Thank you very much! :D

    EDIT: Yes, this is my first time debugging. I have only been using VB.NET for the pass one month and only have about 2-4 hours a week of learning something new.

    Here is the final code (I still haven't re-added all the comment though):

    Public Class Game
    
        Dim score As Integer = 0
        Dim time As Integer = 0
        Dim appleX As Integer
        Dim appleY As Integer
        Dim snakeUP As Boolean = False
        Dim snakeDOWN As Boolean = False
        Dim snakeLEFT As Boolean = False
        Dim snakeRIGHT As Boolean = True
        Dim extraBody As Integer = 5
        Dim x As Integer
        Dim snakeBody(200) As PictureBox
        Dim pause As Boolean = False
    
    
        Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    
            'Starts the game when the button is click.
    
            tmrTime.Enabled = True
            tmrMove.Enabled = True
    
            'START() is a sub routine that will trigger when the button is click.
            START()
        End Sub
    
        Private Sub START()
    
            'Make the pictureboxes visible, and the button hidden.
            pbxApple.Visible = True
            pbxSnake.Visible = True
            btnStart.Visible = False
    
            'This code adds an extra body to our snake.
            For Me.x = 1 To extraBody
                'A new picturebox
                snakeBody(x) = New PictureBox()
                'The picture snake, which we'll be using for on the picturebox.
                snakeBody(x).Image = My.Resources.Snake
                'The width of the new picturebox.
                snakeBody(x).Width = 15
                'The height of the new picturebox.
                snakeBody(x).Height = 15
                'The backcolour of the picturebox.
                snakeBody(x).BackColor = Color.Transparent
                'The location of the new body from the left.
                snakeBody(x).Left = 120 - (15 * x)
                'The location of the new body from the top.
                snakeBody(x).Top = 180
                'The sizemode of our new picturebox.
                snakeBody(x).SizeMode = PictureBoxSizeMode.StretchImage
                'This tells the control to add it to our snakeBody.
                Controls.Add(snakeBody(x))
            Next
    
            Me.Focus()
            MoveSnake()
            NewApple()
    
        End Sub
    
        Private Sub NewApple()
            'The Randomize calls the system's pseudorandom list. A sequence that exhibit statistical randomness while generating an entirely deterministic causal process.
            Randomize()
    
            'Gives the two variable random numbers between 0 to 24.
            appleY = CInt(Rnd() * 24)
            appleX = CInt(Rnd() * 24)
    
            'Gives the picturebox 'pbxApple' a new and random location on the form.
            'The multiplication by 15 puts the picturebox on the form in a random spot on a 25x25 grid.
            pbxApple.Location = New Point(appleX * 15, appleY * 15)
    
            For Me.x = 1 To extraBody
                If pbxApple.Location = snakeBody(x).Location Then
                    NewApple()
                End If
            Next
    
        End Sub
    
        Private Sub tmrTime_Tick(sender As Object, e As EventArgs) Handles tmrTime.Tick
    
            'Adds one to the variable 'time' each time the timer ticks.
            time += 1
    
        End Sub
    
        Private Sub tmrMove_Tick(sender As Object, e As EventArgs) Handles tmrMove.Tick
    
            'This code, MoveSnake() calls the private sub called MoveSnake.
            MoveSnake()
    
            'Me.text gives this form (Me.text) the this in quotes and the ampersand symbol allows us to go from string variable and so on.
            Me.Text = "Score:" & " " & score & " " & "Time:" & time & " " & "-Press P to Pause-"
    
        End Sub
    
        Private Sub MoveSnake()
    
    
            'This makes the extra body parts move towards the location of the first body part.
            snakeBody(1).Location = pbxSnake.Location
    
            'Moves the snake up.
            If snakeUP = True Then
                pbxSnake.Top += -15
            End If
    
            'Moves the snake down.
            If snakeDOWN = True Then
                pbxSnake.Top += 15
            End If
    
            'Moves the snake right.
            If snakeRIGHT = True Then
                pbxSnake.Left += 15
            End If
            'Moves the snake left.
            If snakeLEFT = True Then
                pbxSnake.Left += -15
            End If
    
            x = extraBody
    
            'Moves the new body part behind the existing ones.
            Do Until snakeBody(2).Location = snakeBody(1).Location
                snakeBody(x).Location = snakeBody(x - 1).Location
                x -= 1
            Loop
    
            snakeBody(extraBody).Visible = True
            checkApple()
            checkCollision()
    
        End Sub
    
        Private Sub Game_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    
            'Detects when the player press a key on the keyboard.
    
            If e.KeyValue = Keys.Right And snakeLEFT = False Then
                snakeRIGHT = True
                snakeUP = False
                snakeDOWN = False
            End If
    
            If e.KeyValue = Keys.Left And snakeRIGHT = False Then
                snakeLEFT = True
                snakeUP = False
                snakeDOWN = False
            End If
    
            If e.KeyValue = Keys.Up And snakeDOWN = False Then
                snakeUP = True
                snakeLEFT = False
                snakeRIGHT = False
            End If
    
            If e.KeyValue = Keys.Down And snakeUP = False Then
                snakeDOWN = True
                snakeLEFT = False
                snakeRIGHT = False
            End If
    
            If e.KeyValue = Keys.P And pause = False Then
                pause = True
                lblPause.Visible = True
                tmrTime.Enabled = False
                tmrMove.Enabled = False
                Me.Text = "Score:" & " " & score & " " & "Time:" & time & " " & "-Press SPACE to Continue-"
            End If
    
            If e.KeyValue = Keys.Space And pause = True Then
                Me.Text = "Score:" & " " & score & " " & "Time:" & time & " " & "-Press P to Pause-"
                pause = False
                lblPause.Visible = False
                tmrTime.Enabled = True
                tmrMove.Enabled = True
            End If
    
        End Sub
    
        Private Sub checkApple()
    
            If pbxSnake.Location = pbxApple.Location Then
                appleEaten()
            End If
    
        End Sub
    
        Private Sub appleEaten()
    
            NewApple()
            score += 1
            extraBody += 1
            snakeBody(extraBody) = New PictureBox()
            snakeBody(extraBody).Image = My.Resources.Snake
            snakeBody(extraBody).Width = 15
            snakeBody(extraBody).Height = 15
            snakeBody(extraBody).BackColor = Color.Transparent
            snakeBody(extraBody).SizeMode = PictureBoxSizeMode.StretchImage
            snakeBody(extraBody).Visible = False
            Controls.Add(snakeBody(extraBody))
    
        End Sub
    
        Private Sub checkCollision()
    
            For Me.x = 1 To extraBody
                If snakeBody(x).Location = pbxSnake.Location Then
                    hitBody()
                End If
            Next
    
            If pbxSnake.Left = Me.ClientRectangle.Left Or
            pbxSnake.Top = Me.ClientRectangle.Top Or
            pbxSnake.Top + pbxSnake.Height = Me.ClientRectangle.Top + Me.ClientRectangle.Height Or
            pbxSnake.Left + pbxSnake.Width > Me.ClientRectangle.Left + Me.ClientRectangle.Width Then
                hitWall()
            End If
        End Sub
    
        Private Sub hitBody()
    
            tmrTime.Enabled = False
            tmrMove.Enabled = False
            MessageBox.Show("You collided with your own body, you lose the game!", "Game Over")
            Application.Restart()
    
        End Sub
    
        Private Sub hitWall()
    
            tmrTime.Enabled = False
            tmrMove.Enabled = False
            MessageBox.Show("You collided with the wall, you lose the game!", "Game Over")
            Application.Restart()
    
        End Sub
    End Class



    Thursday, October 17, 2013 9:16 PM

All replies

  • The first step is switching Option Strict On and fixing the compile errors.

    Armin

    Thursday, October 17, 2013 8:39 PM
  • The immediate problem is that the snake is moved before it is initialised.  You need to move these lines
            Me.Focus()
            MoveSnake
    ()
            NewApple
    ()
    to the end of the Start method so that the subs have a snake to work with.  But with such an elementary error in the code it is possible that that change is only the start of your debugging.

    Thursday, October 17, 2013 9:00 PM
  • The first step is switching Option Strict On and fixing the compile errors.

    Armin

    Thanks! I was able to fix all the compile errors. I didn't know about this before, but now I do. Thanks again. :D


    Thursday, October 17, 2013 9:09 PM
  • Oh it work now! Thank you very much! :D

    EDIT: Yes, this is my first time debugging. I have only been using VB.NET for the pass one month and only have about 2-4 hours a week of learning something new.

    Here is the final code (I still haven't re-added all the comment though):

    Public Class Game
    
        Dim score As Integer = 0
        Dim time As Integer = 0
        Dim appleX As Integer
        Dim appleY As Integer
        Dim snakeUP As Boolean = False
        Dim snakeDOWN As Boolean = False
        Dim snakeLEFT As Boolean = False
        Dim snakeRIGHT As Boolean = True
        Dim extraBody As Integer = 5
        Dim x As Integer
        Dim snakeBody(200) As PictureBox
        Dim pause As Boolean = False
    
    
        Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    
            'Starts the game when the button is click.
    
            tmrTime.Enabled = True
            tmrMove.Enabled = True
    
            'START() is a sub routine that will trigger when the button is click.
            START()
        End Sub
    
        Private Sub START()
    
            'Make the pictureboxes visible, and the button hidden.
            pbxApple.Visible = True
            pbxSnake.Visible = True
            btnStart.Visible = False
    
            'This code adds an extra body to our snake.
            For Me.x = 1 To extraBody
                'A new picturebox
                snakeBody(x) = New PictureBox()
                'The picture snake, which we'll be using for on the picturebox.
                snakeBody(x).Image = My.Resources.Snake
                'The width of the new picturebox.
                snakeBody(x).Width = 15
                'The height of the new picturebox.
                snakeBody(x).Height = 15
                'The backcolour of the picturebox.
                snakeBody(x).BackColor = Color.Transparent
                'The location of the new body from the left.
                snakeBody(x).Left = 120 - (15 * x)
                'The location of the new body from the top.
                snakeBody(x).Top = 180
                'The sizemode of our new picturebox.
                snakeBody(x).SizeMode = PictureBoxSizeMode.StretchImage
                'This tells the control to add it to our snakeBody.
                Controls.Add(snakeBody(x))
            Next
    
            Me.Focus()
            MoveSnake()
            NewApple()
    
        End Sub
    
        Private Sub NewApple()
            'The Randomize calls the system's pseudorandom list. A sequence that exhibit statistical randomness while generating an entirely deterministic causal process.
            Randomize()
    
            'Gives the two variable random numbers between 0 to 24.
            appleY = CInt(Rnd() * 24)
            appleX = CInt(Rnd() * 24)
    
            'Gives the picturebox 'pbxApple' a new and random location on the form.
            'The multiplication by 15 puts the picturebox on the form in a random spot on a 25x25 grid.
            pbxApple.Location = New Point(appleX * 15, appleY * 15)
    
            For Me.x = 1 To extraBody
                If pbxApple.Location = snakeBody(x).Location Then
                    NewApple()
                End If
            Next
    
        End Sub
    
        Private Sub tmrTime_Tick(sender As Object, e As EventArgs) Handles tmrTime.Tick
    
            'Adds one to the variable 'time' each time the timer ticks.
            time += 1
    
        End Sub
    
        Private Sub tmrMove_Tick(sender As Object, e As EventArgs) Handles tmrMove.Tick
    
            'This code, MoveSnake() calls the private sub called MoveSnake.
            MoveSnake()
    
            'Me.text gives this form (Me.text) the this in quotes and the ampersand symbol allows us to go from string variable and so on.
            Me.Text = "Score:" & " " & score & " " & "Time:" & time & " " & "-Press P to Pause-"
    
        End Sub
    
        Private Sub MoveSnake()
    
    
            'This makes the extra body parts move towards the location of the first body part.
            snakeBody(1).Location = pbxSnake.Location
    
            'Moves the snake up.
            If snakeUP = True Then
                pbxSnake.Top += -15
            End If
    
            'Moves the snake down.
            If snakeDOWN = True Then
                pbxSnake.Top += 15
            End If
    
            'Moves the snake right.
            If snakeRIGHT = True Then
                pbxSnake.Left += 15
            End If
            'Moves the snake left.
            If snakeLEFT = True Then
                pbxSnake.Left += -15
            End If
    
            x = extraBody
    
            'Moves the new body part behind the existing ones.
            Do Until snakeBody(2).Location = snakeBody(1).Location
                snakeBody(x).Location = snakeBody(x - 1).Location
                x -= 1
            Loop
    
            snakeBody(extraBody).Visible = True
            checkApple()
            checkCollision()
    
        End Sub
    
        Private Sub Game_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    
            'Detects when the player press a key on the keyboard.
    
            If e.KeyValue = Keys.Right And snakeLEFT = False Then
                snakeRIGHT = True
                snakeUP = False
                snakeDOWN = False
            End If
    
            If e.KeyValue = Keys.Left And snakeRIGHT = False Then
                snakeLEFT = True
                snakeUP = False
                snakeDOWN = False
            End If
    
            If e.KeyValue = Keys.Up And snakeDOWN = False Then
                snakeUP = True
                snakeLEFT = False
                snakeRIGHT = False
            End If
    
            If e.KeyValue = Keys.Down And snakeUP = False Then
                snakeDOWN = True
                snakeLEFT = False
                snakeRIGHT = False
            End If
    
            If e.KeyValue = Keys.P And pause = False Then
                pause = True
                lblPause.Visible = True
                tmrTime.Enabled = False
                tmrMove.Enabled = False
                Me.Text = "Score:" & " " & score & " " & "Time:" & time & " " & "-Press SPACE to Continue-"
            End If
    
            If e.KeyValue = Keys.Space And pause = True Then
                Me.Text = "Score:" & " " & score & " " & "Time:" & time & " " & "-Press P to Pause-"
                pause = False
                lblPause.Visible = False
                tmrTime.Enabled = True
                tmrMove.Enabled = True
            End If
    
        End Sub
    
        Private Sub checkApple()
    
            If pbxSnake.Location = pbxApple.Location Then
                appleEaten()
            End If
    
        End Sub
    
        Private Sub appleEaten()
    
            NewApple()
            score += 1
            extraBody += 1
            snakeBody(extraBody) = New PictureBox()
            snakeBody(extraBody).Image = My.Resources.Snake
            snakeBody(extraBody).Width = 15
            snakeBody(extraBody).Height = 15
            snakeBody(extraBody).BackColor = Color.Transparent
            snakeBody(extraBody).SizeMode = PictureBoxSizeMode.StretchImage
            snakeBody(extraBody).Visible = False
            Controls.Add(snakeBody(extraBody))
    
        End Sub
    
        Private Sub checkCollision()
    
            For Me.x = 1 To extraBody
                If snakeBody(x).Location = pbxSnake.Location Then
                    hitBody()
                End If
            Next
    
            If pbxSnake.Left = Me.ClientRectangle.Left Or
            pbxSnake.Top = Me.ClientRectangle.Top Or
            pbxSnake.Top + pbxSnake.Height = Me.ClientRectangle.Top + Me.ClientRectangle.Height Or
            pbxSnake.Left + pbxSnake.Width > Me.ClientRectangle.Left + Me.ClientRectangle.Width Then
                hitWall()
            End If
        End Sub
    
        Private Sub hitBody()
    
            tmrTime.Enabled = False
            tmrMove.Enabled = False
            MessageBox.Show("You collided with your own body, you lose the game!", "Game Over")
            Application.Restart()
    
        End Sub
    
        Private Sub hitWall()
    
            tmrTime.Enabled = False
            tmrMove.Enabled = False
            MessageBox.Show("You collided with the wall, you lose the game!", "Game Over")
            Application.Restart()
    
        End Sub
    End Class



    Thursday, October 17, 2013 9:16 PM