Results 1 to 16 of 16

Thread: [RESOLVED] Help for a noob

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Resolved [RESOLVED] Help for a noob

    Hey, i'm pretty new to visual basic, and we just our first programming assinment for school, we have to make some sort of vb game. Using 2005 express edition and i'm atempting to make a multiplayer snake game. I have the movment controls down and now i'm attempting to make the blue circle i'm using for a player leave a trail of blue color, so that if the player touched it the player dies ect. I've tried spawning invisable picture boxes after it, drawing rectanges and lines and, well i've tried a lot of stuff, so after scouring the internet and local help for about 5 hours and fidling and getting no where i'm deciding to ask for help. Any and all help would be GREATLY appriciated.

    Cheers

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Help for a noob

    Hey,

    Given that this is a homework assignment, people on this forum will be happy to help, but we have to see some effort on your part.

    Can you show some of the code that you are currently using?

    Also, if you search the CodeBank's for "snake" you will find lots of threads.

    Gary

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Re: Help for a noob

    This is the code i am surrently using:

    Code:
    Public Class Form1
        Dim x, y As Integer
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            Select Case e.KeyData
                Case Keys.Up
                    TimerDown.Stop()
                    TimerLeft.Stop()
                    TimerRight.Stop()
                    TimerUp.Start()
                Case Keys.Down
                    TimerLeft.Stop()
                    TimerRight.Stop()
                    TimerUp.Stop()
                    TimerDown.Start()
                Case Keys.Left
                    TimerRight.Stop()
                    TimerUp.Stop()
                    TimerDown.Stop()
                    TimerLeft.Start()
                Case Keys.Right
                    TimerUp.Stop()
                    TimerDown.Stop()
                    TimerLeft.Stop()
                    TimerRight.Start()
                Case Keys.Space
                    TimerUp.Stop()
                    TimerDown.Stop()
                    TimerLeft.Stop()
                    TimerRight.Stop()
            End Select
        End Sub
    
        Private Sub Timerup_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerUp.Tick
            y += -1
            PictureBox1.Top = y
            createtrail()
            End Sub
    
        Private Sub Timerdown_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerDown.Tick
            y += 1
            PictureBox1.Top = y
            createtrail()
            End Sub
    
        Private Sub TimerLeft_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerLeft.Tick
            x += -1
            PictureBox1.Left = x
            createtrail()
            End Sub
    
        Private Sub TimerRight_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerRight.Tick
            x += 1
            PictureBox1.Left = x
            createtrail()
        End Sub
        Sub createtrail()
            Dim trail As New PictureBox
            Dim part As Integer
            part += 1
            With trail
                .Name = .Name & part
                .Height = 23
                .Width = 22
                .Image = My.Resources.Blue_Player
                .Left = PictureBox1.Left
                .Top = PictureBox1.Top
            End With
            me.controls.add(trail)
    
        End Sub
    End Class
    I've searched a lot of online code and tutorials but a lot of the code is umfamiliar and i'm having trouble making sense of it all...
    Last edited by BrighTide; Feb 12th, 2010 at 04:26 AM.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Help for a noob

    Hey,

    Did you look in the CodeBank? There are complete examples of the technique in there.

    Gary

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Re: Help for a noob

    CodeBank?

  6. #6

  7. #7

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Re: Help for a noob

    Sorry, i looked at the code sample, and a lot of the code, i have no idea what it means or more importantly how to implement it, could you look at my code and tell me what i'm doing wrong, or is it more compicated than that?

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Help for a noob

    Hey,

    What you are trying to do, is quite a simple task, but some of the concepts will appear quite strange to you, if you are used to game programming.

    The sample that Lord Orwell put in the code bank is a good one, and from what I can remember, is well documented. If I were you, I would spend some time looking at the code, and stepping into it in the debugger to see what is happening, things will start to become clear.

    Gary

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Re: Help for a noob

    K, i'll try that, thx for the help

  11. #11

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Re: Help for a noob

    Sorry, i tried, really, but, i've tried using his code and it didn't really work, would his code being in 2008, and me having 2005 have anything to do with that?

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Help for a noob

    Hey,

    Can you provide some more details as to why exactly it isn't working? Are you saying that the project didn't open at all?

    If you download and install the following:

    http://www.microsoft.com/express/dow...8-Visual-Basic

    You should be able to open it up to have a look at.

    Gary

  13. #13

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Re: Help for a noob

    Ummm, soz, i can'y really do big downloads atm, the .zip file with project in it, the unzip went fine, but the project wouldn;t open at all yeah. If it helps the game i;m trying to make is more like Tron than snake.

    sorry i'm such a nub

  14. #14
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Help for a noob

    We'll need a form, a picturebox, called picDisp and a button called btnStart (see pic):


    First of all we'll need a game field and its contents:

    Code:
    Public Enum SquareContents
            Empty = 0
            Food = 1
            Obstacle = 2
            Body = 3
    End Enum
    
    Public Const FIELD_WIDTH As Integer = 30
    Public Const FIELD_HEIGHT As Integer = 30
    Public Shared GameField(0 To FIELD_WIDTH - 1, 0 To FIELD_HEIGHT - 1) As SquareContents
    
    Public Enum Directions
            Left
            Right
            Up
            Down
    End Enum
    Now, let's create our snake:

    vb.net Code:
    1. Public Class Snake
    2.         ' His body is simply a queue of points
    3.         ' A queue works by the FIFO principle - First In - First Out
    4.         Public BodyCells As New Queue(Of Point)
    5.        
    6.         ' The direction of his movement
    7.         Public Direction As Directions
    8.  
    9.         ' The location of his head
    10.         Public HeadLocation As Point
    11.  
    12.         ' We'll need two events
    13.         Public Event PainfulDeath() ' Game over
    14.         Public Event YumYum() ' Snake eats
    15.  
    16. Public Sub New()
    17.             HeadLocation = New Point(FIELD_WIDTH \ 2, FIELD_HEIGHT \ 2) ' We place him in the center
    18.             BodyCells.Enqueue(HeadLocation) ' We add his only cell to his body
    19.             Direction = Directions.Right  ' ... and make him look to the right
    20. End Sub
    21.  
    22. ' Now, what will our snake do with each heatbeat?
    23.    Public Sub Tick()
    24.             Dim NextCell As Point = HeadLocation  ' Where is the next cell
    25.             Select Case Direction
    26.                 Case Directions.Up
    27.                     NextCell.Y -= 1  
    28.                     If NextCell.Y < 0 Then
    29.                         RaiseEvent PainfulDeath()  ' ... kill the poor beast.
    30.                         Exit Sub
    31.                     End If
    32.                 Case Directions.Down
    33.                     NextCell.Y += 1
    34.                     If NextCell.Y = FIELD_HEIGHT Then
    35.                         RaiseEvent PainfulDeath()
    36.                         Exit Sub
    37.                     End If
    38.                 Case Directions.Left
    39.                     NextCell.X -= 1
    40.                     If NextCell.X < 0 Then
    41.                         RaiseEvent PainfulDeath()
    42.                         Exit Sub
    43.                     End If
    44.                 Case Directions.Right
    45.                     NextCell.X += 1
    46.                     If NextCell.X = FIELD_WIDTH Then
    47.                         RaiseEvent PainfulDeath()
    48.                         Exit Sub
    49.                     End If
    50.             End Select
    51.  
    52.             ' What do we have in this cell
    53.             Select Case GameField(NextCell.X, NextCell.Y)            
    54.                 Case SquareContents.Body, SquareContents.Obstacle  ' bumped into an obstacle
    55.                     RaiseEvent PainfulDeath()                                  
    56.                     Exit Sub
    57.                 Case SquareContents.Food  '  some food here
    58.                     BodyCells.Enqueue(NextCell) '  add another cell to body
    59.                     HeadLocation = NextCell  
    60.                     GameField(NextCell.X, NextCell.Y) = SquareContents.Body  ' Delete eaten food
    61.                     RaiseEvent YumYum()     ' Gimme more food
    62.  
    63.                 Case SquareContents.Empty  
    64.                     BodyCells.Enqueue(NextCell) ' The tricky part - we add a body cell here..
    65.                     HeadLocation = NextCell        
    66.                     Dim Tail As Point = BodyCells.Dequeue()  '  ... and cut off his tail
    67.                     GameField(Tail.X, Tail.Y) = SquareContents.Empty  
    68.                     GameField(NextCell.X, NextCell.Y) = SquareContents.Body
    69.             End Select
    70.         End Sub
    71. End Class


    Something that would drive our game - a timer and some other helpers at the class level:

    vb.net Code:
    1. Public TICKS_FOR_FOOD As Integer  
    2.                                                            
    3. Public Bob As Snake ' Here's our Bob
    4. Public CellSize As Size ' This is a size in pixels of one field cell (we'll determine it later)
    5. Public tmr As Timer ' Main timer
    6. Public FoodAt As Point ' Where the food is
    7. Public KeepFood As Integer = 0 ' Ticks remaining before the food is spoiled
    8.  
    9. ' Colors:
    10. Public pGrass As Color
    11. Public pFood As Color
    12. Public pBody As Color
    13. Public pObstacle As Color
    14.  
    15. Public GameOver As Boolean = False

    A few preparations:

    vb.net Code:
    1. Private Sub frmSnake_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.         Me.KeyPreview = True
    3.         tmr = New Timer
    4.         tmr.Interval = 100 ' Setting timer interval (this value actually controls the speed of the game)
    5. ' the less it is the faster the game
    6.         tmr.Enabled = False
    7.  
    8. ' Colors:
    9.         pGrass = Color.DarkGreen
    10.         pFood = Color.Red
    11.         pBody = Color.Yellow
    12.         pObstacle = Color.White
    13.  
    14.         AddHandler tmr.Tick, AddressOf tmr_Tick   ' Making timer work
    15. End Sub
    16.  
    17. ' Update the size of the game field cell in pixels:
    18. Private Sub frmSnake_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    19.         CellSize.Width = CInt(picDisp.Width / FIELD_WIDTH)        
    20.         CellSize.Height = CInt(picDisp.Height / FIELD_HEIGHT)
    21. End Sub

    Clicking the start button:

    vb.net Code:
    1. Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    2.         Bob = New Snake
    3.         ReDim GameField(0 To FIELD_WIDTH - 1, 0 To FIELD_HEIGHT - 1) ' Clear the game field
    4.         AddHandler Bob.PainfulDeath, AddressOf EndGame ' Show 'Game Over' if snake dies
    5.         AddHandler Bob.YumYum, AddressOf PlaceNewFood ' For placing food
    6.  
    7.         tmr.Enabled = True
    8.         btnStart.Enabled = False
    9.         GameOver = False
    10. End Sub
    11.  
    12. ' Tell Bob where to go:
    13. Private Sub frmSnake_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    14.         Select Case e.KeyCode
    15.             Case Keys.Left
    16.                 If Bob.Direction = Directions.Right Then Exit Sub
    17.                 Bob.Direction = Directions.Left
    18.             Case Keys.Right
    19.                 If Bob.Direction = Directions.Left Then Exit Sub
    20.                 Bob.Direction = Directions.Right
    21.             Case Keys.Up
    22.                 If Bob.Direction = Directions.Down Then Exit Sub
    23.                 Bob.Direction = Directions.Up
    24.             Case Keys.Down
    25.                 If Bob.Direction = Directions.Up Then Exit Sub
    26.                 Bob.Direction = Directions.Down
    27.         End Select
    28. End Sub
    29.  
    30. ' Bob dies:
    31. Public Sub EndGame()
    32.         GameOver = True
    33.         tmr.Stop()
    34.         btnStart.Enabled = True
    35.         picDisp.Invalidate()
    36. End Sub
    37.  
    38. ' Game tick:
    39. Public Sub tmr_Tick(ByVal sender As Object, ByVal e As EventArgs)
    40.  
    41.         If KeepFood = 0 Then
    42.               PlaceNewFood(EmptyCell:=True) ' We need to remove the old food and place a new one
    43.               Dim rng As New Random
    44.               TICKS_FOR_FOOD = rng.Next(10, 40)
    45.         End If
    46.  
    47.         KeepFood += 1  
    48.         If KeepFood > TICKS_FOR_FOOD Then KeepFood = 0   ' Food is spolied if KeepFood = 0
    49.         Bob.Tick()  ' Kick Bob to move
    50.         picDisp.Invalidate()  ' Redraw screen
    51. End Sub

    Providing food is an important business:

    vb.net Code:
    1. Public Sub PlaceNewFood(Optional ByVal EmptyCell As Boolean = False)
    2.         Dim rng As New Random
    3.         Dim fx, fy As Integer
    4.  
    5.         Do    
    6.             fx = rng.Next(0, FIELD_WIDTH)
    7.             fy = rng.Next(0, FIELD_HEIGHT)
    8.         Loop While GameField(fx, fy) <> SquareContents.Empty
    9.  
    10.         If EmptyCell Then   ' If we need to remove the old food we do that:
    11.             GameField(FoodAt.X, FoodAt.Y) = SquareContents.Empty
    12.         End If
    13.  
    14.         GameField(fx, fy) = SquareContents.Food       '  New food is placed
    15.         FoodAt.X = fx   ' We should remember where it is to remove it later
    16.         FoodAt.Y = fy
    17.     End Sub
    18.  
    19. ' Drawing the game screen:
    20. Private Sub DrawGameField(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picDisp.Paint
    21.        
    22.         If GameOver Then
    23.             Dim fnt As New Font(Me.Font.FontFamily, 42, FontStyle.Bold, GraphicsUnit.Pixel)
    24.             Dim txt As String = "GAME OVER"
    25.             Dim TextSize As SizeF = e.Graphics.MeasureString(txt, fnt, New SizeF(picDisp.Width, fnt.Height))
    26.             e.Graphics.DrawString(txt, fnt, Brushes.Red, _
    27.                                   CSng(picDisp.Width / 2 - TextSize.Width / 2), _
    28.                                   CSng(picDisp.Height / 2 - TextSize.Height / 2))
    29.             Exit Sub
    30.         End If
    31.  
    32.         Dim x, y As Integer
    33.  
    34.         ' Draw each game square them:
    35.         For y = 0 To FIELD_HEIGHT - 1
    36.             For x = 0 To FIELD_WIDTH - 1
    37.                 Dim cellRect As New Rectangle(x * CellSize.Width, y * CellSize.Height, CellSize.Width, CellSize.Height)
    38.  
    39.                 Select Case GameField(x, y)
    40.                     Case SquareContents.Empty
    41.                         e.Graphics.FillRectangle(New SolidBrush(pGrass), cellRect)
    42.                     Case SquareContents.Body
    43.                         e.Graphics.FillRectangle(New SolidBrush(pGrass), cellRect)
    44.                         e.Graphics.FillEllipse(New SolidBrush(pBody), cellRect)
    45.                     Case SquareContents.Food
    46.                         e.Graphics.FillRectangle(New SolidBrush(pGrass), cellRect)
    47.                         e.Graphics.FillEllipse(New SolidBrush(pFood), cellRect)
    48.                     Case SquareContents.Obstacle
    49.                         e.Graphics.FillRectangle(New SolidBrush(pObstacle), cellRect)
    50.                 End Select
    51.             Next
    52.         Next
    53.     End Sub

    That's all. Complete source code is attached.
    Attached Files Attached Files

  15. #15

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    8

    Re: Help for a noob

    Oh my goodness thank you all so much, i finally got it, man, its go frustrating pressing that little green triangle like 6 Kazillion times and what your sure was flawless code not working. YOUR ALL AWESOME! =D

  16. #16
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Help for a noob

    Hey,

    But that is part of understanding code. Debugging is an essential tool that you are going to need to become familiar with. You will become more proficient at it as you gain experience.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width