Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- Dim foodX As Integer = 400, foodY As Integer = 400 ' Food location
- Dim speed As Integer = 20, score As Integer = 0 ' Speed and score
- Dim direction As Integer, tails As Integer ' 0 is up, 3 is right, 6 is down, and 9 is left - Why?
- Dim picHead As PictureBox, picFood As PictureBox
- Dim picTail(100) As PictureBox ' Snake may have up to 99 tail segments
- Private Sub ExitToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem1.Click
- Application.Exit()
- End Sub
- Private Sub tmrHead_Tick(sender As Object, e As EventArgs) Handles tmrHead.Tick
- For i = tails To 1 Step -1 ' Tail inches forward like an inchworm
- picTail(i).Location = picTail(i - 1).Location ' Move a tail segment
- Next
- picTail(0).Location = picHead.Location ' Move the first tail segment
- Select Case direction
- Case 0 : picHead.Top -= speed ' Move snake head up
- If picHead.Top < 0 Then Wall("Ceiling") ' Ouch
- Case 3 : picHead.Left += speed ' Move snake head right
- If picHead.Left + 60 > Me.Width Then Wall("Wall") ' Ouch
- Case 6 : picHead.Top += speed ' Move snake head down
- If picHead.Top + 80 > Me.Height Then Wall("Floor") ' Ouch
- Case 9 : picHead.Left -= speed ' Move snake head left
- If picHead.Left < 0 Then Wall("Wall") ' Ouch
- End Select
- If picHead.Bounds.IntersectsWith(picFood.Bounds) Then ' Snake head overlaps food
- score += 1 ' Add to score
- tails += 1 ' Add a tail segment
- If tails > 99 Then tails = 99 ' But don't add too many tail segments
- tmrFood.Interval = 1 ' Move the food immediately
- If tmrHead.Interval > 100 Then tmrHead.Interval -= 5 ' Make the snake move faster
- End If
- For i = 1 To tails ' Check each tail segment
- If picHead.Bounds.IntersectsWith(picTail(i).Bounds) Then Wall("Tail") ' Snake eats own tail
- Next ' Ouch
- End Sub
- Private Sub CopyrightToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CopyrightToolStripMenuItem.Click
- Dim str As String = "© David Ambos, 12/18/2017" & vbNewLine
- str &= "Control Snake Direction with Arrow Keys" & vbNewLine
- str &= "Try not to hit any walls or your own tail" & vbNewLine
- str &= "File new game to start over"
- MsgBox(str)
- End Sub
- Private Sub NewGameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewGameToolStripMenuItem.Click
- startNew() ' When the user wants to restart
- End Sub
- Private Sub tmrFood_Tick(sender As Object, e As EventArgs) Handles tmrFood.Tick
- tmrFood.Interval = 10000
- foodX = Math.Floor(Rnd() * 25) * 20 ' We may have to tweak this a little
- foodY = Math.Floor(Rnd() * 25) * 20 ' Otherwise the food may appear off the board
- picFood.Location = New Point(foodX, foodY) ' Show the food
- End Sub
- Private Sub SlowToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SlowToolStripMenuItem.Click
- tmrHead.Interval = 150
- End Sub
- Private Sub HellInAProgramToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HellInAProgramToolStripMenuItem.Click
- tmrHead.Interval = 30
- End Sub
- Private Sub UltraFastToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UltraFastToolStripMenuItem.Click
- tmrHead.Interval = 45
- End Sub
- Private Sub FastToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FastToolStripMenuItem.Click
- tmrHead.Interval = 75
- End Sub
- Private Sub MediumToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MediumToolStripMenuItem.Click
- tmrHead.Interval = 125
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- picHead = New PictureBox ' Add the snake head
- picHead.Size = New Size(20, 20) ' Set the size
- picHead.Image = My.Resources.snakeHead ' Set the image
- picHead.Location = New Point(100, 100) ' Set the location
- Me.Controls.Add(picHead) ' Add the control to the form
- picFood = New PictureBox ' Add the food image
- picFood.Size = New Size(20, 20) ' Set the size
- picFood.Image = My.Resources.snakeFood ' Set the image
- picFood.Location = New Point(400, 400) ' Set the location
- Me.Controls.Add(picFood) ' Add the control to the form
- For i = 0 To 99
- picTail(i) = New PictureBox ' Add each tail segment
- picTail(i).Location = New Point(-100, -100) ' Set the location
- picTail(i).Image = My.Resources.snakeTail ' Set the image
- If i Mod 2 = 0 Then picTail(i).Image = My.Resources.snakeTail ' I am alternating tail segment images
- picTail(i).Size = New Size(20, 20) ' Set the size
- Me.Controls.Add(picTail(i)) ' Add the control to the form
- picTail(i).BringToFront() ' Move in front of food
- Next
- picFood.Location = New Point(foodX, foodY) ' Put the food on the form
- picHead.BringToFront() ' Bring the snake head to the front of everything else
- startNew() ' Reset everything
- End Sub
- Private Sub startNew()
- tmrHead.Interval = 200 ' 200 is five times each second
- tmrHead.Enabled = False ' Start the game with snake not moving
- tmrFood.Enabled = False ' No food shows until you press [P] to start the game
- direction = 3 ' Start moving right
- tails = 5 ' Start with 5 tail segments
- score = 0 ' No score yet
- For i = 0 To 99 ' Put all tail segments off the screen
- picTail(i).Location = New Point(-100, -100)
- Next
- picHead.Location = New Point(100, 100) ' Put the snake head on the screen
- ' picTail(i) and picHead will show errors until you add them in the Form1_Load() event
- End Sub
- Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
- Select Case e.KeyCode ' Determine which key was pressed
- Case Keys.Escape : Application.Exit() ' Quit the game on [Escape] keypress
- Case Keys.P ' Pause or Play moving the snake
- tmrHead.Enabled = Not tmrHead.Enabled ' Start or stop head
- tmrFood.Enabled = Not tmrFood.Enabled ' Start or stop food
- Case Keys.Up
- direction = 0 ' Set direction Up
- Case Keys.Right
- direction = 3 ' Set direction right
- Case Keys.Down
- direction = 6 ' Set direction down
- Case Keys.Left
- direction = 9 ' Set direction left
- End Select
- End Sub
- Private Sub Wall(s)
- tmrHead.Enabled = False ' Stop the snake move
- Select Case s ' Which happened?
- Case "Tail" : MsgBox("You ate part of your own tail")
- Case "Wall" : MsgBox("You hit a wall")
- Case "Ceiling" : MsgBox("You hit the ceiling")
- Case "Floor" : MsgBox("You hit the floor")
- End Select
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement