Jump to content

Snake Game Help

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Laglord

Laglord

    Newbie

  • Members
  • Pip
  • 1 posts
So I've been working on a snake game for VB.Net. I've read other forum posts for ideas and got the basic code that moves/creates the snake from youtube and am working on getting it to eat/grow etc. But right now I'm stuck on how to get it to grow. Here's what i got so far:

'Global declarations
Dim m As Integer = 1
Dim p(m) As Point
Dim k As Keys
Public iLength As Integer = 1
Dim x As Integer
Dim y As Integer
Dim draw As Boolean

'In the timer
'Create map
Dim bMap As New Bitmap(430, 313)

Dim g As Graphics = Graphics.FromImage(bMap)
'move the snake
If k = Keys.Left Then
p(0).X -= 10
For i As Integer = 1 To iLength
p(i).X -= 10
Next
End If
If k = Keys.Right Then
p(0).X += 10
For i As Integer = 1 To iLength
p(i).X += 10
Next
End If
If k = Keys.Up Then
p(0).Y -= 10
For i As Integer = 1 To iLength
p(i).Y -= 10
Next
End If
If k = Keys.Down Then
p(0).Y += 10
For i As Integer = 1 To iLength
p(i).Y += 10
Next
End If
g.FillRectangle(Brushes.Green, New Rectangle(p(0), New Size(10, 10)))

For i As Integer = iLength To 1 Step -1

g.FillRectangle(Brushes.DarkSeaGreen, New Rectangle(p(i), New Size(10, 10)))

p(i) = p(i - 1)
Next

p(0) = New Point(x, y) ' <---------------------------------------------- This is the problem i think because whenever the snake grows it just stops moving.

Me.BackgroundImage = bMap

Me.ClientSize = bMap.Size

Me.Refresh()

g.Dispose()

'In Form1_KeyDown
If e.KeyCode = Keys.Left Then
k = Keys.Left
End If
If e.KeyCode = Keys.Right Then
k = Keys.Right
End If
If e.KeyCode = Keys.Down Then
k = Keys.Down
End If
If e.KeyCode = Keys.Up Then
k = Keys.Up
End If

End Sub

#2
Blimp

Blimp

    Programmer

  • Members
  • PipPipPipPip
  • 154 posts
You really need to think about the algorithm. A snake game has quite a difficult algorithm to come by, because it involves checking the current direction of the leading object, to the current object. Remember when creating the new object, you need to tell the program which object it is following.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users