Advertisement
ambosdavid

Snake VS Transcript w/ Comments

Mar 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. Public Class Form1
  2. Dim foodX As Integer = 400, foodY As Integer = 400 ' Food location
  3. Dim speed As Integer = 20, score As Integer = 0 ' Speed and score
  4. Dim direction As Integer, tails As Integer ' 0 is up, 3 is right, 6 is down, and 9 is left - Why?
  5. Dim picHead As PictureBox, picFood As PictureBox
  6. Dim picTail(100) As PictureBox ' Snake may have up to 99 tail segments
  7. Private Sub ExitToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem1.Click
  8. Application.Exit()
  9. End Sub
  10.  
  11. Private Sub tmrHead_Tick(sender As Object, e As EventArgs) Handles tmrHead.Tick
  12. For i = tails To 1 Step -1 ' Tail inches forward like an inchworm
  13. picTail(i).Location = picTail(i - 1).Location ' Move a tail segment
  14. Next
  15. picTail(0).Location = picHead.Location ' Move the first tail segment
  16. Select Case direction
  17. Case 0 : picHead.Top -= speed ' Move snake head up
  18. If picHead.Top < 0 Then Wall("Ceiling") ' Ouch
  19. Case 3 : picHead.Left += speed ' Move snake head right
  20. If picHead.Left + 60 > Me.Width Then Wall("Wall") ' Ouch
  21. Case 6 : picHead.Top += speed ' Move snake head down
  22. If picHead.Top + 80 > Me.Height Then Wall("Floor") ' Ouch
  23. Case 9 : picHead.Left -= speed ' Move snake head left
  24. If picHead.Left < 0 Then Wall("Wall") ' Ouch
  25. End Select
  26. If picHead.Bounds.IntersectsWith(picFood.Bounds) Then ' Snake head overlaps food
  27. score += 1 ' Add to score
  28. tails += 1 ' Add a tail segment
  29. If tails > 99 Then tails = 99 ' But don't add too many tail segments
  30. tmrFood.Interval = 1 ' Move the food immediately
  31. If tmrHead.Interval > 100 Then tmrHead.Interval -= 5 ' Make the snake move faster
  32. End If
  33. For i = 1 To tails ' Check each tail segment
  34. If picHead.Bounds.IntersectsWith(picTail(i).Bounds) Then Wall("Tail") ' Snake eats own tail
  35. Next ' Ouch
  36. End Sub
  37.  
  38. Private Sub CopyrightToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CopyrightToolStripMenuItem.Click
  39. Dim str As String = "© David Ambos, 12/18/2017" & vbNewLine
  40. str &= "Control Snake Direction with Arrow Keys" & vbNewLine
  41. str &= "Try not to hit any walls or your own tail" & vbNewLine
  42. str &= "File new game to start over"
  43. MsgBox(str)
  44. End Sub
  45.  
  46. Private Sub NewGameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewGameToolStripMenuItem.Click
  47. startNew() ' When the user wants to restart
  48. End Sub
  49.  
  50. Private Sub tmrFood_Tick(sender As Object, e As EventArgs) Handles tmrFood.Tick
  51. tmrFood.Interval = 10000
  52. foodX = Math.Floor(Rnd() * 25) * 20 ' We may have to tweak this a little
  53. foodY = Math.Floor(Rnd() * 25) * 20 ' Otherwise the food may appear off the board
  54. picFood.Location = New Point(foodX, foodY) ' Show the food
  55. End Sub
  56.  
  57. Private Sub SlowToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SlowToolStripMenuItem.Click
  58. tmrHead.Interval = 150
  59. End Sub
  60.  
  61. Private Sub HellInAProgramToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HellInAProgramToolStripMenuItem.Click
  62. tmrHead.Interval = 30
  63. End Sub
  64.  
  65. Private Sub UltraFastToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UltraFastToolStripMenuItem.Click
  66. tmrHead.Interval = 45
  67. End Sub
  68.  
  69. Private Sub FastToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FastToolStripMenuItem.Click
  70. tmrHead.Interval = 75
  71. End Sub
  72.  
  73. Private Sub MediumToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MediumToolStripMenuItem.Click
  74. tmrHead.Interval = 125
  75. End Sub
  76.  
  77. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  78. picHead = New PictureBox ' Add the snake head
  79. picHead.Size = New Size(20, 20) ' Set the size
  80. picHead.Image = My.Resources.snakeHead ' Set the image
  81. picHead.Location = New Point(100, 100) ' Set the location
  82. Me.Controls.Add(picHead) ' Add the control to the form
  83. picFood = New PictureBox ' Add the food image
  84. picFood.Size = New Size(20, 20) ' Set the size
  85. picFood.Image = My.Resources.snakeFood ' Set the image
  86. picFood.Location = New Point(400, 400) ' Set the location
  87. Me.Controls.Add(picFood) ' Add the control to the form
  88. For i = 0 To 99
  89. picTail(i) = New PictureBox ' Add each tail segment
  90. picTail(i).Location = New Point(-100, -100) ' Set the location
  91. picTail(i).Image = My.Resources.snakeTail ' Set the image
  92. If i Mod 2 = 0 Then picTail(i).Image = My.Resources.snakeTail ' I am alternating tail segment images
  93. picTail(i).Size = New Size(20, 20) ' Set the size
  94. Me.Controls.Add(picTail(i)) ' Add the control to the form
  95. picTail(i).BringToFront() ' Move in front of food
  96. Next
  97. picFood.Location = New Point(foodX, foodY) ' Put the food on the form
  98. picHead.BringToFront() ' Bring the snake head to the front of everything else
  99. startNew() ' Reset everything
  100. End Sub
  101. Private Sub startNew()
  102. tmrHead.Interval = 200 ' 200 is five times each second
  103. tmrHead.Enabled = False ' Start the game with snake not moving
  104. tmrFood.Enabled = False ' No food shows until you press [P] to start the game
  105. direction = 3 ' Start moving right
  106. tails = 5 ' Start with 5 tail segments
  107. score = 0 ' No score yet
  108. For i = 0 To 99 ' Put all tail segments off the screen
  109. picTail(i).Location = New Point(-100, -100)
  110. Next
  111. picHead.Location = New Point(100, 100) ' Put the snake head on the screen
  112. ' picTail(i) and picHead will show errors until you add them in the Form1_Load() event
  113. End Sub
  114.  
  115. Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
  116. Select Case e.KeyCode ' Determine which key was pressed
  117. Case Keys.Escape : Application.Exit() ' Quit the game on [Escape] keypress
  118. Case Keys.P ' Pause or Play moving the snake
  119. tmrHead.Enabled = Not tmrHead.Enabled ' Start or stop head
  120. tmrFood.Enabled = Not tmrFood.Enabled ' Start or stop food
  121. Case Keys.Up
  122. direction = 0 ' Set direction Up
  123. Case Keys.Right
  124. direction = 3 ' Set direction right
  125. Case Keys.Down
  126. direction = 6 ' Set direction down
  127. Case Keys.Left
  128. direction = 9 ' Set direction left
  129. End Select
  130. End Sub
  131. Private Sub Wall(s)
  132. tmrHead.Enabled = False ' Stop the snake move
  133. Select Case s ' Which happened?
  134. Case "Tail" : MsgBox("You ate part of your own tail")
  135. Case "Wall" : MsgBox("You hit a wall")
  136. Case "Ceiling" : MsgBox("You hit the ceiling")
  137. Case "Floor" : MsgBox("You hit the floor")
  138. End Select
  139. End Sub
  140. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement