📄 frmmain.vb
字号:
' cool death sequence or something.
Private Sub KillSnake()
ShowMsg("Press Enter to play again or Esc to quit.")
MsgBox("Oops! Snakey go bye bye!", vbOKOnly, "Dead Snake")
' re-initialize the state of the game
InitializeGame()
End Sub
' This sub finds a random location on the playing field
' for Snakey's food.
Public Sub PlaceFood()
Dim foodpt As Point
' The loop is needed in case the new food location
' ends up on the snake. We need to keep trying new
' locations until an open spot is found.
Do
foodpt = GetRandomPoint()
' Make sure Snakey exists first
If Not (m_snake Is Nothing) Then
' Check if the new food location is not on the snake
' We have to obey the basic laws of physics here.
If Not m_snake.PointOnSnake(foodpt) Then Exit Do
Else
Exit Do
End If
Loop
' Change the location of the rectangle for the food
' to our new-found random spot
m_foodrec.Location = foodpt
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Enter ' Start/continue game
HideMsg()
Case Keys.Escape ' pause/quit game
If m_running Then
ShowMsg("Press Enter to continue or Esc again to quit.")
Else
Me.Close()
End If
End Select
End Sub
' The keydown event is where we let the user control Snakey
' Try using the '[' and ']' keys to control Snakey for a more
' challenging game.
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Right, Keys.NumPad6 ' head Right
m_control.Direction = SnakeControl.SnakeDirection.Right
Case Keys.Down, Keys.NumPad2 ' head to Down
m_control.Direction = SnakeControl.SnakeDirection.Down
Case Keys.Left, Keys.NumPad4 ' head Left
m_control.Direction = SnakeControl.SnakeDirection.Left
Case Keys.Up, Keys.NumPad8 ' head up
m_control.Direction = SnakeControl.SnakeDirection.Up
Case Keys.OemOpenBrackets ' Turn left relative to snake's current direction
m_control.TurnLeft()
Case Keys.OemCloseBrackets ' Turn right relative to snake's current direction
m_control.TurnRight()
End Select
End Sub
Private Sub InitializeGame()
' Reset the score
m_score = 0
' Initialize the food rectangle
m_foodrec = New Rectangle(0, 0, SNAKE_WIDTH, SNAKE_WIDTH)
' Randomly place the food on the playing surface
PlaceFood()
' Get the starting point for the snake (middle of the playing surface)
Dim startpt As New Point( _
CInt(PictureBox1.ClientSize.Width / 2 / SNAKE_WIDTH + 0.5) * SNAKE_WIDTH, _
CInt(PictureBox1.ClientSize.Height / 2 / SNAKE_WIDTH + 0.5) * SNAKE_WIDTH)
' Initialize the snake with just one segment
m_snake = New Snake(startpt, SNAKE_WIDTH, 1)
' Initialize the controller
m_control = New SnakeControl(SNAKE_WIDTH, m_snake.Head.Location, SnakeControl.SnakeDirection.Right)
' The snake grows when the game starts (it kinda makes it look like the
' snake is slithering out of a hole in the ground or something)
m_growing = True
End Sub
Private Sub UpdateGame()
' Number of segments the snake needs to add while growing
Static targetgrowth As Integer = GROWTH_FACTOR
' Number of segments that have been added during growth
Static segmentsadded As Integer
' Check if the game is currently running
If Not m_running Then Exit Sub
' Move the controller
m_control.Move(PictureBox1.ClientRectangle)
' Check if the snake bit itself
If m_snake.PointOnSnake(m_control.Location) Then
' Snake gnoshed itself, reset our static
' variables...
targetgrowth = 0
segmentsadded = 0
' ... and kill poor Snakey
KillSnake()
Return
' Check if the snake ate some food
ElseIf m_foodrec.Contains(m_control.Location) Then
' Snake ate food, let the snake grow
targetgrowth += GROWTH_FACTOR
m_growing = True
' Replenish the food supply. We don't want
' Snakey to starve!!!
PlaceFood()
' and increase the score
m_score += 10
Text = "SnakeDotNet - Score: " + m_score.ToString
End If
' Check if the snake is growing
If m_growing Then
' Just in case the target growth has been reset to 0...
If targetgrowth < GROWTH_FACTOR Then targetgrowth = GROWTH_FACTOR
' Check if the snake has grown enough
If segmentsadded >= targetgrowth Then
' Reset the variables for growing
m_growing = False
segmentsadded = 0
targetgrowth = 0
' The snake isn't growing anymore,
' so slither the snake instead.
m_snake.Slither(m_control.Location)
Else
' Add a new segment in the new location
m_snake.Add(m_control.Location)
' Increment the number of segments added
segmentsadded += 1
End If
Else
' Slither the snake to the new location
m_snake.Slither(m_control.Location)
End If
End Sub
' This sub pauses the game and displays
' the specified message in the middle of
' the playing field.
Private Sub ShowMsg(ByVal msg As String)
Label1.Text = msg
Label1.Visible = True
m_running = False
Timer1.Enabled = False
End Sub
' This hides the previous message and
' continues the game.
Private Sub HideMsg()
Me.Text = "SnakeDotNet - Score: " + m_score.ToString
Label1.Visible = False
m_running = True
Timer1.Enabled = True
End Sub
' This returns a random point within the limits
' of the playing field. The point is clamped to
' an imaginary grid with the same spacing as the
' snake's width.
Public Function GetRandomPoint() As Point
' Initialize the random generator
Dim r As New Random(Now.Second)
' Get the width and height of the field as multiples of the snake's width
Dim fieldwidth As Integer = ((ClientRectangle.Width \ SNAKE_WIDTH) - 2) * SNAKE_WIDTH
Dim fieldheight As Integer = ((ClientRectangle.Height \ SNAKE_WIDTH) - 2) * SNAKE_WIDTH
' Get random values for the x & y values
Dim randx As Integer = r.Next(0, fieldwidth)
Dim randy As Integer = r.Next(0, fieldheight)
' Clamp the x & y values to a multiple of the snake's width
randx = (randx \ SNAKE_WIDTH) * SNAKE_WIDTH
randy = (randy \ SNAKE_WIDTH) * SNAKE_WIDTH
Return New Point(randx, randy)
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -