|
-
Apr 6th, 2012, 05:01 AM
#1
Thread Starter
Addicted Member
setting the x and y of a point
I have this code for placing food in my snake game, but it is not working.
It says the structure 'system.drawing.point' has no default property.
Code:
Public Sub PlaceNewFood(Optional ByVal EmptyCell As Boolean = False)
Dim rng As New Random
Dim fx, fy As Integer
Dim f As Integer = 1
Dim foodpoint As Point
Dim b As New Bitmap(512, 256)
Dim g As Graphics = Graphics.FromImage(b)
fx = rng.Next(0, PictureBox1.Width)
fy = rng.Next(0, PictureBox1.Height)
foodpoint.X = fx
foodpoint.Y = fy
For i As Integer = 0 To m - 1
p(i) = p(i + 1)
g.FillRectangle(Brushes.Black, New Rectangle(foodpoint(fx, fy), New Size(16, 16)))
Next
g.FillRectangle(Brushes.Black, New Rectangle(foodpoint(fx, fy), New Size(16, 16)))
g.Dispose()
PictureBox1.Image = b
Me.Refresh()
Me.Refresh()
End Sub
How can I solve this?
-
Apr 6th, 2012, 05:23 AM
#2
Re: setting the x and y of a point
Code:
fx = rng.Next(0, PictureBox1.Width)
fy = rng.Next(0, PictureBox1.Height)
foodpoint.X = New Point(fx, fy)
Code:
g.FillRectangle(Brushes.Black, New Rectangle(foodpoint, New Size(16, 16)))
-
Apr 6th, 2012, 06:16 AM
#3
Thread Starter
Addicted Member
Re: setting the x and y of a point
You meant foodpoint = new point (fx, fy) and not foodpoint.x, thank you
But now I have the problem if I go over the food with the snake, it crashes saysing the parameter of
g.FillRectangle(Brushes.Black, New Rectangle(foodpoint, New Size(16, 16)))
is not valid
heres my full code, I only use a picturebox size 512; 256 in my form.
vb Code:
Public Class Form1 Dim m As Integer = 4 Dim p(m) As Point Dim collision As Boolean = False Dim NowUp As Boolean = False Dim NowDown As Boolean = False Dim NowLeft As Boolean = False Dim NowRight As Boolean = True Dim foodeaten As Boolean = True Dim rng As New Random Dim fx, fy As Integer Dim f As Integer = 1 Dim foodpoint As Point Dim d As New Bitmap(512, 256) Dim g2 As Graphics = Graphics.FromImage(d) Private Enum Direction Up Down Right Left End Enum Dim currdirection As Direction = Direction.Right #Region "SnakeField" Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim b As New Bitmap(512, 256) Dim g As Graphics = Graphics.FromImage(b) Select Case currdirection Case Direction.Left If NowRight = False Then If p(m).X > PictureBox1.Bounds.Left Then p(m).X -= 16 NowLeft = True NowUp = False NowDown = False NowRight = False Else collision = True End If Else p(m).X += 16 NowRight = True End If Case Direction.Right If NowLeft = False Then If p(m).X < PictureBox1.Width - 16 Then p(m).X += 16 NowLeft = False NowUp = False NowDown = False NowRight = True Else collision = True End If Else p(m).X -= 16 NowLeft = True End If Case Direction.Up If NowDown = False Then If p(m).Y > 0 Then p(m).Y -= 16 NowLeft = False NowUp = True NowDown = False NowRight = False Else collision = True End If Else p(m).Y += 16 NowDown = True End If Case Direction.Down If NowUp = False Then If p(m).Y < PictureBox1.Bounds.Height - 16 Then p(m).Y += 16 NowLeft = False NowUp = False NowDown = True NowRight = False Else collision = True End If Else p(m).Y -= 16 NowUp = True End If End Select If collision = False Then For i As Integer = 0 To m - 1 p(i) = p(i + 1) g.FillRectangle(Brushes.Black, New Rectangle(p(i), New Size(16, 16))) Next g.FillRectangle(Brushes.Black, New Rectangle(p(m), New Size(16, 16))) g.Dispose() PictureBox1.Image = b Me.Refresh() Me.Refresh() End If End Sub #End Region Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If e.KeyCode = Keys.Left Then currdirection = Direction.Left End If If e.KeyCode = Keys.Right Then currdirection = Direction.Right End If If e.KeyCode = Keys.Up Then currdirection = Direction.Up End If If e.KeyCode = Keys.Down Then currdirection = Direction.Down End If End Sub Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick If foodeaten Then Call PlaceNewFood() foodeaten = False End If If foodpoint = p(m) Then foodeaten = True End If End Sub Public Sub PlaceNewFood(Optional ByVal EmptyCell As Boolean = False) fx = rng.Next(0, PictureBox1.Width) fx = fx / 16 fx = fx * 16 fy = rng.Next(0, PictureBox1.Height) fy = fy / 16 fy = fy * 16 foodpoint = New Point(fx, fy) For i As Integer = 0 To m - 1 p(i) = p(i + 1) g2.FillRectangle(Brushes.Black, New Rectangle(foodpoint, New Size(16, 16))) Next g2.FillRectangle(Brushes.Black, New Rectangle(foodpoint, New Size(16, 16))) g2.Dispose() PictureBox1.BackgroundImage = d Me.Refresh() Me.Refresh() End Sub End Class
-
Apr 6th, 2012, 06:23 AM
#4
Re: setting the x and y of a point
Try
Code:
New Rectangle(foodpoint.X, foodpoint.Y, 16, 16)
-
Apr 6th, 2012, 06:28 AM
#5
Thread Starter
Addicted Member
Re: setting the x and y of a point
I don't know if it works, because now it gives this error for some reason:
Index was outside the bounds of the array.
at this statement: If p(m).X < PictureBox1.Width - 16 Then
I don't get it
-
Apr 6th, 2012, 07:43 AM
#6
Re: setting the x and y of a point
m has been initialized with 4 (m = 4)
Dim p(m) As Point means that you allocated 5 memory slots big enough to hold a point structure.
But each of it is still has the value of Nothing. You should assign a new Point to each of these elements before referring to them.
It's like you have 5 boxes for 5 pairs of shoes. Having the boxes doesn't always mean that you actually have the shoes. In fact, all your boxes are empty. Before you could use each box, you should place a new pair of shoes into each one.
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
|