Jump to content

VB Snake Game Help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
pilzheare

pilzheare

    Newbie

  • Members
  • Pip
  • 1 posts
Hello!

I am having trouble finding a way to complete my Snake Game in VB.
Here are my problems:
1. coding the randomly placed object for the snake to eat and then get longer
2. snake can run over itself and leave the screen

Any help at all in the next few days would be greatly appreciated.
thanks much, pilzheare

Here is my code sofar:
Code:

Public Class Form2

    Dim m As Integer = 10

    Dim p(m) As Point

    Dim k As Keys


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim b As New Bitmap(256, 256)

        Dim g As Graphics = Graphics.FromImage(b)


        If k = Keys.Left Then

            p(m).X -= 10

        End If

        If k = Keys.Right Then

            p(m).X += 10

        End If

        If k = Keys.Up Then

            p(m).Y -= 10

        End If

        If k = Keys.Down Then

            p(m).Y += 10

        End If


        For i As Integer = 0 To m - 1

            p(i) = p(i + 1)

            g.FillRectangle(Brushes.Blue, New Rectangle(p(i), New Size(8, 8)))

        Next


        g.FillRectangle(Brushes.Blue, New Rectangle(p(m), New Size(8, 8)))

        g.Dispose()

        Me.BackgroundImage = b

        Me.ClientSize = b.Size

        Me.Refresh()

    End Sub


    Private Sub Form2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.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.Up Then

            k = Keys.Up

        End If

        If e.KeyCode = Keys.Down Then

            k = Keys.Down

        End If

    End Sub

End Class

Edited by Roger, 09 January 2011 - 11:14 AM.
added code block


#2
Skippy

Skippy

    Programmer

  • Members
  • PipPipPipPip
  • 146 posts
Sorry I'm not too familiar with BASIC, but it's not the code you need help with, I think its the game theory.
1. coding the randomly placed object for the snake to eat and then get longer
from what I see on your code, I belive the playing area of the snake game is a 256x256 area. So in order to generate a randomly placed coordinate for your object to go, it needs to be within that area. The common or most practical way of doing this would be using a random number generator, I don't know any in basic, but they should be very common, even built into the system. you could do something like
int x = AbsoluteValue( random.generateInteger()%width);
int y = AbsoluteValue( random.generateInteger()%height);
then you need to check if the snake is already on that point, because That would be a glitch. so you should run through the snake's coordinates and make sure none of the coordinates are touching the point. maybe even make sure that the point is at least 2 pixils away or something. If that test failed, Just set the variables again until they finally pass the test. That could be in an ObjectGenerate() function, or whatever you want to call it.


2. snake can run over itself and leave the screen

you need to do checking on the snake every time it moves. in other words, if the snake's head is at coordinate -1, he just ran off the screen. Game over.
if the snakes head is at 256 ( one point past the max ) he just ran off the screen. Game over.
if the snake's head collides with his body at any place, he just killed himself, game over. ( by checking the head with each body coordinate to see if the coordinates are the same.

Hope this helps.

#3
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
I recall there was a VB-Snake thread recently where I contributed something in the discussion. Just a moment...

Okay, this is the link for the thread. I think we discussed that project quite thoroughly. The OP also posted the final source codes. Check it out and post anything you need more help with here.