N
nmwink1207
Guest
I am building a Snake game for a class, but I keep getting the "A first chance exception of type 'System.NullReferenceException' occurred in frmSnake.exe" in the Immediate Window.
Any advice or opinions on what is wrong would be much apperciated!
Public Class frmSnake
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
Dim x As Integer
Dim snakeBody(200) As PictureBox
Dim pause As Boolean = False
Private Sub frmSnake_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
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
End Sub
Private Sub cmdBegin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBegin.Click
Begin()
tmrTime.Enabled = True
tmrMoveSnake.Enabled = True
End Sub
Private Sub Begin()
picApple.visible = True
picSnakeHead.visible = True
cmdBegin.Visible = False
Me.Focus()
Newapple()
For Me.x = 1 To extraBody
snakeBody(x).Image = My.Resources.Snake
snakeBody(x).Width = 15
snakeBody(x).Height = 15
snakeBody(x).BackColor = Color.Transparent
snakeBody(x).Left = 120 - (15 * x)
snakeBody(x).Top = 180
snakeBody(x).SizeMode = PictureBoxSizeMode.StretchImage
Controls.Add(snakeBody(x))
Next
End Sub
Private Sub NewApple()
Randomize()
appleY = Rnd() * 24
appleX = Rnd() * 24
picApple.Location = New Point(appleX * 15, appleY * 15)
For Me.x = 1 To extraBody
If picApple.Location = snakeBody(x).Location Then NewApple()
Next
End Sub
Private Sub tmrTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTime.Tick
time += 1
End Sub
Private Sub tmrMoveSnake_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMoveSnake.Tick
moveSnake()
Me.Text = "Score: " & score & ", Time: " & time & ", Press P to Pause"
End Sub
Private Sub movesnake()
snakeBody(1).Location = picSnakeHead.Location
If snakeUp = True Then
picSnakeHead.Top += -15
End If
If snakeDown = True Then
picSnakeHead.Top += 15
End If
If snakeRight = True Then
picSnakeHead.Left += 15
End If
If snakeLeft = True Then
picSnakeHead.Left += -15
End If
x = extraBody
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 checkapple()
If picSnakeHead.Location = picApple.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 = picSnakeHead.Location Then
Hitbody()
End If
Next
If picSnakeHead.Left = Me.ClientRectangle.Left Or picSnakeHead.Top = Me.ClientRectangle.Top Or picSnakeHead.Top + picSnakeHead.Height = Me.ClientRectangle.Top + Me.ClientRectangle.Height Or picSnakeHead.Left + picSnakeHead.Width = Me.ClientRectangle.Left + Me.ClientRectangle.Width Then
Hitwall()
End If
End Sub
Private Sub Hitbody()
tmrTime.Enabled = False
tmrMoveSnake.Enabled = False
MessageBox.Show("You Collided With Your Own Body, You Lose!", "Game Over")
Application.Restart()
End Sub
Private Sub HitWall()
tmrTime.Enabled = False
tmrMoveSnake.Enabled = False
MessageBox.Show("You Collided With The Wall, You Lose!", "Game Over")
Application.Restart()
End Sub
End Class
Continue reading...
Any advice or opinions on what is wrong would be much apperciated!
Public Class frmSnake
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
Dim x As Integer
Dim snakeBody(200) As PictureBox
Dim pause As Boolean = False
Private Sub frmSnake_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
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
End Sub
Private Sub cmdBegin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBegin.Click
Begin()
tmrTime.Enabled = True
tmrMoveSnake.Enabled = True
End Sub
Private Sub Begin()
picApple.visible = True
picSnakeHead.visible = True
cmdBegin.Visible = False
Me.Focus()
Newapple()
For Me.x = 1 To extraBody
snakeBody(x).Image = My.Resources.Snake
snakeBody(x).Width = 15
snakeBody(x).Height = 15
snakeBody(x).BackColor = Color.Transparent
snakeBody(x).Left = 120 - (15 * x)
snakeBody(x).Top = 180
snakeBody(x).SizeMode = PictureBoxSizeMode.StretchImage
Controls.Add(snakeBody(x))
Next
End Sub
Private Sub NewApple()
Randomize()
appleY = Rnd() * 24
appleX = Rnd() * 24
picApple.Location = New Point(appleX * 15, appleY * 15)
For Me.x = 1 To extraBody
If picApple.Location = snakeBody(x).Location Then NewApple()
Next
End Sub
Private Sub tmrTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTime.Tick
time += 1
End Sub
Private Sub tmrMoveSnake_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMoveSnake.Tick
moveSnake()
Me.Text = "Score: " & score & ", Time: " & time & ", Press P to Pause"
End Sub
Private Sub movesnake()
snakeBody(1).Location = picSnakeHead.Location
If snakeUp = True Then
picSnakeHead.Top += -15
End If
If snakeDown = True Then
picSnakeHead.Top += 15
End If
If snakeRight = True Then
picSnakeHead.Left += 15
End If
If snakeLeft = True Then
picSnakeHead.Left += -15
End If
x = extraBody
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 checkapple()
If picSnakeHead.Location = picApple.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 = picSnakeHead.Location Then
Hitbody()
End If
Next
If picSnakeHead.Left = Me.ClientRectangle.Left Or picSnakeHead.Top = Me.ClientRectangle.Top Or picSnakeHead.Top + picSnakeHead.Height = Me.ClientRectangle.Top + Me.ClientRectangle.Height Or picSnakeHead.Left + picSnakeHead.Width = Me.ClientRectangle.Left + Me.ClientRectangle.Width Then
Hitwall()
End If
End Sub
Private Sub Hitbody()
tmrTime.Enabled = False
tmrMoveSnake.Enabled = False
MessageBox.Show("You Collided With Your Own Body, You Lose!", "Game Over")
Application.Restart()
End Sub
Private Sub HitWall()
tmrTime.Enabled = False
tmrMoveSnake.Enabled = False
MessageBox.Show("You Collided With The Wall, You Lose!", "Game Over")
Application.Restart()
End Sub
End Class
Continue reading...