Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27311

[RESOLVED] Issue where a new instance isn't being declared

$
0
0
I'm working on the game snake and I'm currently doing this:
Code:

Option Strict On
Option Explicit On
Public Class Form1
    Private snake_head As Snake 'This will be the snake head and item(0) in snake_body
    Private snake_body As List(Of Snake) 'This will hold the snakes body
    Private r As New Random 'New instance at form level
    Private fruit As Snake 'Instance of a random fruit in the place_fruit sub
    Private playing As Boolean


    Private Sub new_game()

        game_timer.Stop()      'Stop the timer
        playing = False        'We aren't playing anymore
        Me.Controls.Clear()    'Get rid of any existing control
        snake_body.Clear()    'Get rid of the prior body parts
        snake_head = New Snake 'Declare a new instance of the head

        'Set some properties of the head
        With snake_head
            .BorderStyle = BorderStyle.FixedSingle
            .BackColor = Color.Honeydew
            .Location = New Point(CInt(Me.Width / 2), CInt(Me.Height / 2))
            .IsHead = True
            .MovingDirection = Snake.Direction.Left
            .Size = New Size(50, 50)
        End With

        snake_body.Add(snake_head)  'Add the head to the body list

        Me.Controls.Add(snake_head) 'Add the head to me

    End Sub

    Private Sub place_fruit()

        fruit = New Snake 'Declare a new instance of a snake

        'Set some properties
        With fruit
            .BackColor = Color.LightGreen
            .Location = New Point(r.Next(0, Me.Width - 50), r.Next(0, Me.Height - 50))
            .IsHead = False
            .MovingDirection = Snake.Direction.Left
            .Size = New Size(25, 25)
        End With

        Me.Controls.Add(fruit)
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'The controls are:
        'Up - Up Arrow or W
        'Down - Down Arrow or S
        'Left - Left Arrow or A
        'Right - Right Arrow or D
        'Start/Pause - Spacebar

        Select Case e.KeyCode
            Case Keys.Up Or Keys.W 'Up
                If playing Then 'Check to make sure we're plaing, then make the snake head go in that direction
                    snake_head.MovingDirection = Snake.Direction.Up
                End If
            Case Keys.Down Or Keys.S 'Down
                If playing Then
                    snake_head.MovingDirection = Snake.Direction.Down
                End If
            Case Keys.Left Or Keys.A 'Left
                If playing Then
                    snake_head.MovingDirection = Snake.Direction.Left
                End If
            Case Keys.Right Or Keys.D 'Right
                If playing Then
                    snake_head.MovingDirection = Snake.Direction.Right
                End If
            Case Keys.Space
                If Not (playing) Then 'If we aren't playing then start the timer
                    game_timer.Start()

                    Call place_fruit() 'To place a random fruit
                End If
        End Select
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Set up some form properties
        With Me
            .DoubleBuffered = True
            .KeyPreview = True
            .StartPosition = FormStartPosition.CenterScreen
            .Text = "Snake"
        End With

        'Game_Timer's property. The ideal intervals are:
        '
        game_timer.Interval = 100

        Call new_game()

    End Sub
End Class

Snake Class:
Code:

Option Strict On
Option Explicit On
Public Class Snake
    Inherits Panel

    Private head As Boolean
    Public Property IsHead() As Boolean
        Get
            Return head
        End Get
        Set(ByVal value As Boolean)
            head = value
        End Set
    End Property

    Public Enum Direction
        Up
        Down
        Left
        Right
    End Enum

    Private direct As Direction = Direction.Left
    Public Property MovingDirection() As Direction
        Get
            Return direct
        End Get
        Set(ByVal value As Direction)
            direct = value
        End Set
    End Property

End Class

My issue is in the 'new game' sub. I set breakpoints where I declare a new instance of the snake and where I set up some of it's properties, but when I go to debug my program those breakpoints don't ever come up. Everything prior like the clearing the list(of snake) and clearing me.controls fires, but everything after that doesn't. I was hoping someone could point out what I'm doing wrong.

Viewing all articles
Browse latest Browse all 27311

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>