Results 1 to 5 of 5

Thread: Snake

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    1

    Snake

    I'm attempting to make a second snake in my class currently. Yet it keeps on spawning immobilized snakes can anyone tell me why?

    vb.net Code:
    1. Public Class Form1
    2.     Structure player
    3.         Dim intXSpeed As Integer
    4.         Dim intYSpeed As Integer
    5.         Dim intScore As Integer            'I don't know what these do
    6.         Dim intLives As Integer
    7.         Dim blnBoost As Boolean
    8.     End Structure
    9.     Dim MySnake() As Snake
    10.     Dim MySnake2() As Snake
    11.     Dim blnDimensioned As Boolean
    12.     Dim lngPosition As Long
    13.     Dim intBody As Integer
    14.     Dim plSnake As player
    15.     Dim pl2Snake As player
    16.  
    17.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    18.         With Me
    19.             .BackColor = Color.Black
    20.             .DoubleBuffered = True
    21.             .KeyPreview = True
    22.             .MaximizeBox = True
    23.             .Text = "Snaked"
    24.             .WindowState = FormWindowState.Maximized
    25.         End With
    26.  
    27.         Dim intName As Integer
    28.         'ReDim MySnake(2)
    29.  
    30.         intName = InputBox("Type in your name.")
    31.        
    32.     End Sub
    33.  
    34.     Sub StartGame()
    35.         Randomize()
    36.  
    37.         ReDim MySnake(MySnake.Length)
    38.         ReDim MySnake2(MySnake2.Length)
    39.  
    40.         For IntRCount = 0 To MySnake2.Length - 1
    41.             MySnake2(IntRCount) = New Snake()
    42.             'AddHandler MySnake(IntRCount).Click, AddressOf AnyLand_click      ' if you click on a single snake piece
    43.             'AddHandler MySnake(IntRCount).KeyPress, AddressOf Form1_KeyPress
    44.             MySnake2(IntRCount).BackgroundImageLayout = ImageLayout.Stretch
    45.             MySnake2(IntRCount).FlatStyle = FlatStyle.Popup
    46.             MySnake2(IntRCount).Width = 15
    47.             MySnake2(IntRCount).Height = 15
    48.             MySnake2(IntRCount).Left = 15
    49.             MySnake2(IntRCount).Top = IntRCount * 15 + 100
    50.             Me.Controls.Add(MySnake2(IntRCount))
    51.             MySnake2(IntRCount).BackColor = Color.Red     'Change to a snake picture
    52.  
    53.         Next
    54.         For IntRCount = 0 To MySnake.Length - 1
    55.  
    56.  
    57.             MySnake(IntRCount) = New Snake()
    58.             'AddHandler MySnake(IntRCount).Click, AddressOf AnyLand_click      ' if you click on a single snake piece
    59.             'AddHandler MySnake(IntRCount).KeyPress, AddressOf Form1_KeyPress
    60.             MySnake(IntRCount).BackgroundImageLayout = ImageLayout.Stretch
    61.             MySnake(IntRCount).FlatStyle = FlatStyle.Popup
    62.             MySnake(IntRCount).Width = 15
    63.             MySnake(IntRCount).Height = 15
    64.             MySnake(IntRCount).Left = 115
    65.             MySnake(IntRCount).Top = IntRCount * 15 + 100
    66.             Me.Controls.Add(MySnake(IntRCount))
    67.             MySnake(IntRCount).BackColor = Color.Red     'Change to a snake picture
    68.  
    69.  
    70.  
    71.         Next
    72.  
    73.     End Sub
    74.  
    75.     Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
    76.         Call StartGame()
    77.         'ReDim Preserve MySnake(MySnake.Length - 1)
    78.         'ReDim Preserve MySnake2(MySnake2.Length - 1)
    79.         btnInstruct.Visible = False
    80.         btnStart.Visible = False
    81.         btnGrow.Visible = True
    82.         picGrow.Visible = True
    83.  
    84.     End Sub
    85.  
    86.     Private Sub btnInstruct_Click(sender As System.Object, e As System.EventArgs) Handles btnInstruct.Click
    87.         MessageBox.Show("To control snake, use the number pad. 4 is left, 6 is right, 5 is down, and 8 is up.")
    88.         MessageBox.Show("Must click Grow before moving.")
    89.         'Figure out how to make paragraph form
    90.     End Sub
    91.  
    92.     Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress, btnStart.KeyPress, btnGrow.KeyPress
    93.  
    94.         If tmrStart.Enabled = False Then
    95.             tmrStart.Enabled = True
    96.         End If
    97.  
    98.         If e.KeyChar = "s" Then
    99.             plSnake.intYSpeed = 6
    100.             plSnake.intXSpeed = 0
    101.         ElseIf e.KeyChar = "w" Then
    102.             plSnake.intYSpeed = -6
    103.             plSnake.intXSpeed = 0
    104.         ElseIf e.KeyChar = "d" Then
    105.             plSnake.intXSpeed = 6
    106.             plSnake.intYSpeed = 0
    107.         ElseIf e.KeyChar = "a" Then
    108.             plSnake.intXSpeed = -6
    109.             plSnake.intYSpeed = 0
    110.         End If
    111.         Me.Text = e.KeyChar
    112.         If e.KeyChar = "2" Then
    113.             pl2Snake.intYSpeed = 6
    114.             pl2Snake.intXSpeed = 0
    115.         ElseIf e.KeyChar = "8" Then
    116.             pl2Snake.intYSpeed = -6
    117.             pl2Snake.intXSpeed = 0
    118.         ElseIf e.KeyChar = "6" Then
    119.             pl2Snake.intXSpeed = 6
    120.             pl2Snake.intYSpeed = 0
    121.         ElseIf e.KeyChar = "4" Then
    122.             pl2Snake.intXSpeed = -6
    123.             pl2Snake.intYSpeed = 0
    124.         End If
    125.  
    126.  
    127.     End Sub
    128.  
    129.     Private Sub btnGrow_Click(sender As System.Object, e As System.EventArgs) Handles btnGrow.Click
    130.         'ReDim Preserve MySnake(MySnake.Length - 1)
    131.         'ReDim Preserve MySnake2(MySnake2.Length - 1)
    132.         btnGrow.Visible = False
    133.  
    134.     End Sub
    135.  
    136.     Private Sub tmrStart_Tick(sender As System.Object, e As System.EventArgs) Handles tmrStart.Tick, btnGrow.KeyPress, btnStart.KeyPress
    137.  
    138.         For intC As Integer = 0 To MySnake.Length - 1
    139.  
    140.             MySnake(intC).intLastX = MySnake(intC).Left        'This is where the error is coming from
    141.             MySnake(intC).intLastY = MySnake(intC).Top
    142.  
    143.             MySnake2(intC).intLastX = MySnake2(intC).Left        'This is where the error is coming from
    144.             MySnake2(intC).intLastY = MySnake2(intC).Top
    145.  
    146.             If intC = 0 Then
    147.                 MySnake(intC).Top += plSnake.intYSpeed
    148.                 MySnake(intC).Left += plSnake.intXSpeed
    149.  
    150.                 MySnake2(intC).Top += pl2Snake.intYSpeed
    151.                 MySnake2(intC).Left += pl2Snake.intXSpeed
    152.             Else
    153.                 MySnake(intC).Top = MySnake(intC - 1).intLastY
    154.                 MySnake(intC).Left = MySnake(intC - 1).intLastX
    155.  
    156.                 MySnake2(intC).Top = MySnake2(intC - 1).intLastY
    157.                 MySnake2(intC).Left = MySnake2(intC - 1).intLastX
    158.  
    159.             End If
    160.         Next
    161.  
    162.         If picGrow.Bounds.IntersectsWith(MySnake(0).Bounds) Then
    163.             picGrow.Top = getRandom(Me.Height, 1)
    164.             picGrow.Left = getRandom(Me.Width, 1)
    165.  
    166.  
    167.             ' Call StartGame()
    168.             ReDim Preserve MySnake(MySnake.Length)
    169.  
    170.             MySnake(MySnake.Length - 1) = New Snake()
    171.             'AddHandler MySnake(IntRCount).Click, AddressOf AnyLand_click      ' if you click on a single snake piece
    172.             'AddHandler MySnake(IntRCount).KeyPress, AddressOf Form1_KeyPress
    173.             MySnake(MySnake.Length - 1).BackgroundImageLayout = ImageLayout.Stretch
    174.             MySnake(MySnake.Length - 1).FlatStyle = FlatStyle.Popup
    175.             MySnake(MySnake.Length - 1).Width = 15
    176.             MySnake(MySnake.Length - 1).Height = 15
    177.             MySnake(MySnake.Length - 1).Left = 15
    178.             MySnake(MySnake.Length - 1).Top = (MySnake.Length - 1) * 15 + 100
    179.             Me.Controls.Add(MySnake(MySnake.Length - 1))
    180.             MySnake(MySnake.Length - 1).BackColor = Color.Green
    181.             'Change to a snake picture
    182.         End If
    183.  
    184.         If picGrow.Bounds.IntersectsWith(MySnake2(0).Bounds) Then
    185.             picGrow.Top = getRandom(Me.Height, 1)
    186.             picGrow.Left = getRandom(Me.Width, 1)
    187.  
    188.  
    189.             'Call StartGame()
    190.             ReDim Preserve MySnake2(MySnake2.Length)
    191.  
    192.             MySnake2(MySnake2.Length - 1) = New Snake()
    193.             'AddHandler MySnake(IntRCount).Click, AddressOf AnyLand_click      ' if you click on a single snake piece
    194.             'AddHandler MySnake(IntRCount).KeyPress, AddressOf Form1_KeyPress
    195.             MySnake2(MySnake2.Length - 1).BackgroundImageLayout = ImageLayout.Stretch
    196.             MySnake2(MySnake2.Length - 1).FlatStyle = FlatStyle.Popup
    197.             MySnake2(MySnake2.Length - 1).Width = 15
    198.             MySnake2(MySnake2.Length - 1).Height = 15
    199.             MySnake2(MySnake2.Length - 1).Left = 115
    200.             MySnake(MySnake2.Length - 1).Top = (MySnake2.Length - 1) * 15 + 100
    201.             Me.Controls.Add(MySnake(MySnake.Length - 1))
    202.             MySnake(MySnake.Length - 1).BackColor = Color.Green
    203.             Change to a snake picture
    204.         End If
    205.     End Sub
    206.     Function getRandom(ByVal intMax As Integer, ByVal intMin As Integer) As Integer
    207.         Return (Int((intMax - intMin + 1) * Rnd() + intMin))
    208.  
    209.     End Function
    210.     Sub moveboost(ByVal intx As Integer, ByVal inty As Integer)
    211.         picGrow.Top = intx
    212.         picGrow.Left = inty
    213.     End Sub
    214.  
    215. End Class
    Last edited by dday9; Mar 18th, 2015 at 10:01 AM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Snake

    I've moved the thread to graphics and game programming as well as added code tags.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Snake

    If you look at your for loop, where is the if statement that will only allow enabled snakes to spawn. Instead its cycling through all of the snakes.

    A little off topic but i should totally make a 3D snake game o.O

  4. #4
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: Snake

    you're spawning a new snake for every length of the first snake. = new snake() looks wrong to me
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Snake

    A few things:

    1) Get rid of the Randomize call. That was used to seed the random number generator in VB6. It shouldn't do anything at all in .NET, but it seems like it might do something bad based on some experiences others have had (though I haven't studied it).

    2) You should really be using a List(of Snake) rather than those arrays. That will get rid of the inefficient Redim Preserve statements. Lists, and other generics, tend to be overlooked in classes, but they were added in 2005, so they aren't new and are much better than messing with dynamic arrays.

    Neither of those address the problem, of course. However, you appear to have some interesting comments in the code. If the snakes are immobilized, then I would expect that there is something going wrong in the timer tick (or the timer is NOT ticking), and you seem to feel that way, too, because you have comments about "this is where the error is coming from." How did you determine that? Did you put a breakpoint in the code on one of those lines and have a look at what is happening? That would be the thing to do, because you pretty much know what SHOULD be happening with those variables, so the next step is to see what IS happening with the variables.
    My usual boring signature: Nothing

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