|
-
Apr 24th, 2013, 12:44 PM
#1
[RESOLVED] Issue where a new instance isn't being declared
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.
-
Apr 24th, 2013, 12:52 PM
#2
Re: Issue where a new instance isn't being declared
Could you show us exactly where the breakpoints are in the code? It's a bit ambiguous.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 24th, 2013, 12:59 PM
#3
Re: Issue where a new instance isn't being declared
Yeah I told you, they're where I declare a new instance of the snake and where I set up some of it's properties so these:
Code:
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
so everything that's after snake_body.Clear() won't fire at all.
-
Apr 24th, 2013, 01:00 PM
#4
Re: Issue where a new instance isn't being declared
Windows 7? 64-bit maybe?
there's a part of me that hopes the answer is yes and yes... because that would "make" things "easier".
-tg
-
Apr 24th, 2013, 01:01 PM
#5
Re: Issue where a new instance isn't being declared
 Originally Posted by techgnome
Windows 7? 64-bit maybe?
there's a part of me that hopes the answer is yes and yes... because that would "make" things "easier".
-tg
yes and yes. Uh oh did I miss something!
-
Apr 24th, 2013, 01:16 PM
#6
Re: Issue where a new instance isn't being declared
Because there is an error, and it is in the Load event. 64 bit versions errors in the load event aren't captured (thrown?) I moved your load code to the Shown handler to find the error.
Add new to
Code:
Private snake_body As New List(Of Snake) 'This will hold the snakes body
-
Apr 24th, 2013, 01:22 PM
#7
Re: Issue where a new instance isn't being declared
Ahh, okie dokie. Yeah, I added it as a new instance and now it works. I should've caught that and not rely on VS to catch it for me!
-
Apr 26th, 2013, 07:56 AM
#8
Re: Issue where a new instance isn't being declared
You want to be very careful of code in the load event on 64 bit machines. See
http://social.msdn.microsoft.com/For...e-b2959b1827e9
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|