Results 1 to 6 of 6

Thread: setting the x and y of a point

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    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?

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

    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)))

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    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:
    1. Public Class Form1
    2.     Dim m As Integer = 4
    3.     Dim p(m) As Point
    4.     Dim collision As Boolean = False
    5.     Dim NowUp As Boolean = False
    6.     Dim NowDown As Boolean = False
    7.     Dim NowLeft As Boolean = False
    8.     Dim NowRight As Boolean = True
    9.     Dim foodeaten As Boolean = True
    10.     Dim rng As New Random
    11.     Dim fx, fy As Integer
    12.     Dim f As Integer = 1
    13.     Dim foodpoint As Point
    14.     Dim d As New Bitmap(512, 256)
    15.     Dim g2 As Graphics = Graphics.FromImage(d)
    16.     Private Enum Direction
    17.  
    18.         Up
    19.         Down
    20.         Right
    21.         Left
    22.  
    23.  
    24.     End Enum
    25.  
    26.     Dim currdirection As Direction = Direction.Right
    27. #Region "SnakeField"
    28.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    29.  
    30.         Dim b As New Bitmap(512, 256)
    31.         Dim g As Graphics = Graphics.FromImage(b)
    32.  
    33.         Select Case currdirection
    34.             Case Direction.Left
    35.                 If NowRight = False Then
    36.                     If p(m).X > PictureBox1.Bounds.Left Then
    37.                         p(m).X -= 16
    38.                         NowLeft = True
    39.                         NowUp = False
    40.                         NowDown = False
    41.                         NowRight = False
    42.                     Else
    43.                         collision = True
    44.                     End If
    45.                 Else
    46.                     p(m).X += 16
    47.                     NowRight = True
    48.                 End If
    49.             Case Direction.Right
    50.                 If NowLeft = False Then
    51.  
    52.                     If p(m).X < PictureBox1.Width - 16 Then
    53.                         p(m).X += 16
    54.                         NowLeft = False
    55.                         NowUp = False
    56.                         NowDown = False
    57.                         NowRight = True
    58.                     Else
    59.                         collision = True
    60.                     End If
    61.                 Else
    62.                     p(m).X -= 16
    63.                     NowLeft = True
    64.                 End If
    65.             Case Direction.Up
    66.                 If NowDown = False Then
    67.                     If p(m).Y > 0 Then
    68.                         p(m).Y -= 16
    69.                         NowLeft = False
    70.                         NowUp = True
    71.                         NowDown = False
    72.                         NowRight = False
    73.                     Else
    74.                         collision = True
    75.                     End If
    76.                 Else
    77.                     p(m).Y += 16
    78.                     NowDown = True
    79.                 End If
    80.             Case Direction.Down
    81.                 If NowUp = False Then
    82.                     If p(m).Y < PictureBox1.Bounds.Height - 16 Then
    83.                         p(m).Y += 16
    84.                         NowLeft = False
    85.                         NowUp = False
    86.                         NowDown = True
    87.                         NowRight = False
    88.                     Else
    89.                         collision = True
    90.                     End If
    91.                 Else
    92.                     p(m).Y -= 16
    93.                     NowUp = True
    94.                 End If
    95.  
    96.         End Select
    97.         If collision = False Then
    98.             For i As Integer = 0 To m - 1
    99.                 p(i) = p(i + 1)
    100.                 g.FillRectangle(Brushes.Black, New Rectangle(p(i), New Size(16, 16)))
    101.             Next
    102.             g.FillRectangle(Brushes.Black, New Rectangle(p(m), New Size(16, 16)))
    103.             g.Dispose()
    104.             PictureBox1.Image = b
    105.             Me.Refresh()
    106.             Me.Refresh()
    107.         End If
    108.  
    109.     End Sub
    110. #End Region
    111.     Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    112.  
    113.         If e.KeyCode = Keys.Left Then
    114.             currdirection = Direction.Left
    115.         End If
    116.  
    117.         If e.KeyCode = Keys.Right Then
    118.             currdirection = Direction.Right
    119.         End If
    120.  
    121.         If e.KeyCode = Keys.Up Then
    122.             currdirection = Direction.Up
    123.         End If
    124.  
    125.         If e.KeyCode = Keys.Down Then
    126.             currdirection = Direction.Down
    127.         End If
    128.  
    129.  
    130.     End Sub
    131.  
    132.     Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    133.         If foodeaten Then
    134.             Call PlaceNewFood()
    135.             foodeaten = False
    136.         End If
    137.  
    138.  
    139.         If foodpoint = p(m) Then
    140.          
    141.             foodeaten = True
    142.  
    143.         End If
    144.     End Sub
    145.     Public Sub PlaceNewFood(Optional ByVal EmptyCell As Boolean = False)
    146.  
    147.  
    148.         fx = rng.Next(0, PictureBox1.Width)
    149.         fx = fx / 16
    150.         fx = fx * 16
    151.         fy = rng.Next(0, PictureBox1.Height)
    152.         fy = fy / 16
    153.         fy = fy * 16
    154.  
    155.         foodpoint = New Point(fx, fy)
    156.  
    157.         For i As Integer = 0 To m - 1
    158.  
    159.             p(i) = p(i + 1)
    160.             g2.FillRectangle(Brushes.Black, New Rectangle(foodpoint, New Size(16, 16)))
    161.         Next
    162.         g2.FillRectangle(Brushes.Black, New Rectangle(foodpoint, New Size(16, 16)))
    163.         g2.Dispose()
    164.         PictureBox1.BackgroundImage = d
    165.         Me.Refresh()
    166.         Me.Refresh()
    167.     End Sub
    168.  
    169. End Class

  4. #4

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2012
    Posts
    142

    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

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

    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
  •  



Click Here to Expand Forum to Full Width