Vb.net阻止蛇向后转,跟随网格

时间:2016-08-14 02:36:22

标签: vb.net windows vb.net-2010

我在vb.net上制作了一个基本的蛇游戏,我试图阻止蛇向后转身。我是初学者,不知道代码会是什么?

另外,我如何更改我的代码以便我的蛇会跟随网格,因为在蛇收集“通电”的那一刻,当我希望它与通电一致时,它有时会通过一半的通电盒。

以下是代码:

    Public Class Form3

#Region "Snake Declarations"
    Dim snake(1000) As PictureBox
    Dim length_of_snake As Integer = -1
    Dim left_right_mover As Integer = 0
    Dim up_down_mover As Integer = 0
    Dim lastKey As Integer

    Dim r As New Random
    Private Sub create_head()

        length_of_snake += 1

        snake(length_of_snake) = New PictureBox
        With snake(length_of_snake)
            .Height = 10
            .Width = 10
            .BackColor = Color.Blue
            .Top = (Gridfield.Top + Gridfield.Bottom) / 2
            .Left = (Gridfield.Left + Gridfield.Right) / 2
        End With
        Me.Controls.Add(snake(length_of_snake))
        snake(length_of_snake).BringToFront()

        lengthensnake()
        lengthensnake()

    End Sub

    Private Sub lengthensnake()
        length_of_snake += 1
        snake(length_of_snake) = New PictureBox
        With snake(length_of_snake)
            .Height = 10
            .Width = 10
            .BackColor = Color.Blue
            .Top = snake(length_of_snake - 1).Top
            .Left = snake(length_of_snake - 1).Left + 10
        End With
        Me.Controls.Add(snake(length_of_snake))
        snake(length_of_snake).BringToFront()
    End Sub
    Private Sub Form3_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        Tm_snakemover.Start()
        Select Case e.KeyChar
            Case "a"
                left_right_mover = -10
                up_down_mover = 0
            Case "d"
                left_right_mover = 10
                up_down_mover = 0
            Case "w"
                up_down_mover = -10
                left_right_mover = 0
            Case "s"
                up_down_mover = 10
                left_right_mover = 0
        End Select
    End Sub
    Private Sub Tm_snakemover_Tick(sender As System.Object, e As System.EventArgs) Handles Tm_snakemover.Tick

        For i = length_of_snake To 1 Step -1
            snake(i).Top = snake(i - 1).Top
            snake(i).Left = snake(i - 1).Left
        Next

        snake(0).Top += up_down_mover
        snake(0).Left += left_right_mover

        wall_collision()
        powerup_collision()
        selfcollision()
    End Sub
#End Region

#Region "Collisions"
    Private Sub wall_collision()
        If snake(0).Left < Gridfield.Left Then
            Tm_snakemover.Stop()
            MsgBox("You have lost!")
        End If

        If snake(0).Right > Gridfield.Right Then
            Tm_snakemover.Stop()
            MsgBox("You have lost!")
        End If

        If snake(0).Bottom > Gridfield.Bottom Then
            Tm_snakemover.Stop()
            MsgBox("You have lost!")
        End If

        If snake(0).Top < Gridfield.Top Then
            Tm_snakemover.Stop()
            MsgBox("You have lost!")
        End If


    End Sub

    'This sub checks the collision between the player and the powerup
    Private Sub powerup_collision()
        If snake(0).Bounds.IntersectsWith(powerup.Bounds) Then
            lengthensnake()
            powerup.Top = r.Next(Gridfield.Top, Gridfield.Bottom - 10)
            powerup.Left = r.Next(Gridfield.Left, Gridfield.Right - 10)
        End If
    End Sub

    'This sub checks to see if the snake will collide with itself
    Private Sub selfcollision()
        For i = 1 To length_of_snake
            If snake(0).Bounds.IntersectsWith(snake(i).Bounds) Then
                Tm_snakemover.Stop()
                MsgBox("Haha, You have lost because you ran into yourself!")
            End If
        Next
    End Sub

#End Region

#Region "powerup"
    Dim powerup As PictureBox
    Private Sub create_powerup()
        powerup = New PictureBox
        With powerup
            .Width = 10
            .Height = 10
            .BackColor = Color.Red
            .Top = r.Next(Gridfield.Top, Gridfield.Bottom - 10)
            .Left = r.Next(Gridfield.Left, Gridfield.Right - 10)
        End With
        Me.Controls.Add(powerup)
        powerup.BringToFront()
    End Sub
#End Region


    Private Sub Form3_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        create_head()
        create_powerup()
        ' Tm_snakemover.Start()
    End Sub


End Class

1 个答案:

答案 0 :(得分:0)

你可以试试这个

Select Case e.KeyChar
            Case "a"
                If(left_right_mover <> 10) then
                    left_right_mover = -10
                    up_down_mover = 0
                End If
            Case "d"
                If(left_right_mover <> -10) then
                    left_right_mover = 10
                    up_down_mover = 0
                End If
            Case "w"
                If(up_down_mover <> 10) then
                    up_down_mover = -10
                    left_right_mover = 0
                End If
            Case "s"
                If(up_down_mover <> -10) then
                    up_down_mover = 10
                    left_right_mover = 0
                End If
        End Select