I'm trying to program a game of Snake in VB 2010. So far it's been pretty easy, but right now I'm stuck on adding length to my snake when he eats an apple and detecting when his head runs into any other points in his body. I'm trying to work on adding length first because he starts off as only one square long and that means it's impossible for his head to intersect anything...
To create the snake I'm using an array of points and on the point with the highest index value I use a graphics class to paint a square black and for the rest of his body I paint them transparent so you can't see them. I'm trying to make it so when he eats an apple it'll paint the next square in his body black but I can't seem to figure out a way to do that. Here's my code for painting his body:
'Create rest of snake's body. For i As Integer = 0 To m - 1 g.FillRectangle(Brushes.Transparent, New Rectangle(p(i), New Size(10, 10))) p(i) = p(i + 1) Next 'Paint snake's head. (Highest array index value = snake's head). g.FillRectangle(Brushes.Black, New Rectangle(p(m), New Size(10, 10))) g.Dispose()
I dimensioned a variable named "m" to create an array of m points so I paint everything except the head transparent in the beginning and only the head of the snake is black. I tried using a for loop like the one above to paint the next square black but I think it doesn't work because it gets in the way of the first for loop...
Is there a way to paint the next square in the body black and so on for each time the snake eats an apple? After I figure that out I can try working on detecting intersecting points... Any help would be appreciated.
Thanks,
Bound


Sign In
Create Account


Back to top









