蛇游戏重置按钮不起作用?

时间:2017-06-12 11:23:52

标签: vb.net visual-studio

在Visual Basic中创建一个蛇形游戏作为学校作业。我几乎完成了我的操作但遇到了一些问题,我的主屏幕形式与我的游戏/游戏场形式的交互以及我的#34;你死了#34;形成。我可以通过一轮蛇。我的#34;你死了#34;弹出屏幕,但是当我点击重试时,它会在我离开它或崩溃的状态下重新加载表单。如果我单击“主页”按钮重新打开主屏幕然后再次单击“播放”,则会发生同样的事情,它会将我带回到我死亡的地方。 的 frmPlayField

Public Class frmPlayfield


Public PFSize As Integer = SnakeHS.myPFSize 'To change the size of the play field depending on what difficulty the user selects.
Public PfPbSize As Integer = SnakeHS.myPfPbSize
Public tmrDiff As Integer = SnakeHS.mytmrDiff 'To change the interval of the timer depending on what difficulty the user selects.
Public scorePos As Integer = SnakeHS.myscorePos
'Food Creating and Grow Snake Variables
Dim randF As New Random

    Dim foodPointX As Integer = randF.Next(0, PFSize)
    Dim foodPointY As Integer = randF.Next(0, PFSize)

    'Play Field Variables
    Dim playMaxWidth As Integer = PFSize
    Dim playMaxHeight As Integer = PFSize
    Dim boxSize As Integer = PfPbSize                             'Size of PictureBox
Dim boxArray(,) As PictureBox 'PictureBox Array
Dim scoreSnake As New Label
'Snake Stuff Variable
Public playerScore As Integer = 0
Dim snake(0) As Point                                      'Snake array
Dim direction As New Point(1, 0)                           'Direction for snake movement

Public Sub Reset()
    Refresh()
End Sub
Private Sub frmPlayfield_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ReDim boxArray(playMaxWidth, playMaxHeight)
    For x As Integer = 0 To playMaxWidth
        For y As Integer = 0 To playMaxHeight
            boxArray(x, y) = New PictureBox
            boxArray(x, y).Width = boxSize
            boxArray(x, y).Height = boxSize
            boxArray(x, y).Top = y * boxSize
            boxArray(x, y).Left = x * boxSize
            boxArray(x, y).Visible = True
            boxArray(x, y).BackColor = Color.White
            boxArray(x, y).BorderStyle = BorderStyle.FixedSingle
            Me.Controls.Add(boxArray(x, y))
        Next
    Next
    Me.ClientSize = New Size((playMaxWidth + 10) * boxSize, (playMaxHeight + 1) * boxSize)


    scoreSnake.AutoSize = False
    scoreSnake.Width = 120
    scoreSnake.Height = 40
    scoreSnake.ForeColor = Color.Red
    scoreSnake.TextAlign = ContentAlignment.MiddleLeft
    scoreSnake.Visible = True
    scoreSnake.BorderStyle = BorderStyle.FixedSingle
    scoreSnake.Location = New Point(20, 20)
    scoreSnake.Text = 0
    scoreSnake.Name = "lblScore"
    scoreSnake.Font = New Font("Papyrus", 16, FontStyle.Bold)
    scoreSnake.Left = playMaxWidth + scorePos
    scoreSnake.Top = 10

    Me.Controls.Add(scoreSnake)

    snake(0) = New Point(1, 1) 'Creates snake head
    boxArray(foodPointX, foodPointY).BackColor = Color.Red
    timGameTick.Interval = tmrDiff
    timGameTick.Enabled = True
    My.Computer.Audio.Play(My.Resources.Soundtrack_In_Game,
   AudioPlayMode.BackgroundLoop)
End Sub

Private Function createBox(x As Integer, y As Integer, bSize As Integer) As PictureBox
    Dim tempBox As New PictureBox With {
        .Width = bSize,
        .Height = bSize,
        .Top = y * bSize,
        .Left = x * bSize,
        .Visible = True,
        .BackColor = Color.White,
        .BorderStyle = BorderStyle.FixedSingle
    }
    Me.Controls.Add(tempBox)
    Return tempBox
End Function

Private Sub Died()
    If snake.Length > 1 Then
        For f = 1 To snake.GetUpperBound(0)
            If snake(0).X = snake(f).X And snake(0).Y = snake(f).Y Then
                My.Computer.Audio.Play(My.Resources.Game_Over, AudioPlayMode.WaitToComplete)

                timGameTick.Enabled = False

                Hide()
                You_Died.Show()
                My.Computer.Audio.Stop()
                Call GameMusic()
                Call Reset() 'removes all the controls on the form

            End If
        Next

    End If

End Sub


Private Sub GameMusic()

    My.Computer.Audio.Play(My.Resources.Pause_Music, AudioPlayMode.BackgroundLoop)
End Sub

Private Sub Food()
    For i As Integer = snake.GetLowerBound(0) To snake.GetUpperBound(0)
        If snake(i).X = foodPointX And snake(i).Y = foodPointY Then
            ReDim Preserve snake(snake.Length)
            foodPointX = randF.Next(0, PFSize)
            foodPointY = randF.Next(0, PFSize)
            boxArray(foodPointX, foodPointY).BackColor = Color.Red
            playerScore += 10



        End If

    Next
    scoreSnake.Text = playerScore
End Sub

Private Sub CheckBoundsAndMovement()
        For i As Integer = 0 To snake.GetUpperBound(0)
        boxArray(snake(i).X, snake(i).Y).BackColor = Color.White 'Loop to change the whole snake white
        boxArray(snake(i).X, snake(i).Y).Update()
        Next
        If snake.Length > 1 Then
            For i As Integer = snake.GetUpperBound(0) To 1 Step -1
                snake(i) = snake(i - 1)
            Next
        End If
    snake(0) = snake(0) + direction

    If snake(0).X > playMaxWidth Then
            snake(0).X -= (playMaxWidth + 1)
        End If
        If snake(0).X < 0 Then
            snake(0).X += (playMaxWidth + 1)
        End If                                                       'Four If statements to check if the snake has gone outside the play area.
        If snake(0).Y > playMaxWidth Then
            snake(0).Y -= (playMaxWidth + 1)
        End If
    If snake(0).Y < 0 Then
        snake(0).Y += (playMaxWidth + 1)
    End If



    For k As Integer = 0 To snake.GetUpperBound(0)
        boxArray(snake(k).X, snake(k).Y).BackColor = Color.Black 'Loop to make the whole snake black

    Next

End Sub



Private Sub timGameTick_Tick(sender As Object, e As EventArgs) Handles timGameTick.Tick
    Food()
    Died()
    CheckBoundsAndMovement()
End Sub

Private Sub frmPlayfield_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown 'Subroutine for direction
    Select Case (e.KeyCode)
        Case Keys.Up
            direction = New Point(0, -1)
        Case Keys.Down
            direction = New Point(0, 1)
        Case Keys.Left
            direction = New Point(-1, 0)
        Case Keys.Right
            direction = New Point(1, 0)
    End Select
End Sub


End Class

Snake Home Screen

Public Class SnakeHS
Public myPFSize As Integer
Public myPfPbSize As Integer
Public mytmrDiff As Integer
Public myscorePos As Integer
Private Sub btnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
    frmPlayfield.Refresh()
    If rbEasy.Checked Then
        myPFSize = 32
        myPfPbSize = 16
        mytmrDiff = 110
        myscorePos = 500
    End If
    If rbMedium.Checked Then
        myPFSize = 22
        myPfPbSize = 16
        mytmrDiff = 80
        myscorePos = 350
    End If
    If rbHard.Checked Then
        myPFSize = 12
        myPfPbSize = 16
        mytmrDiff = 50
        myscorePos = 200
    End If

    frmPlayfield.PfPbSize = myPfPbSize
    frmPlayfield.PFSize = myPFSize
    frmPlayfield.tmrDiff = mytmrDiff
    frmPlayfield.scorePos = myscorePos

    Hide()
    frmPlayfield.Show()





End Sub

Private Sub SnakeHS_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    rbEasy.Checked = True
End Sub
End Class

你死了屏幕

Public Class You_Died
Private Sub btnHome_Click(sender As Object, e As EventArgs) Handles btnHome.Click

    Close()
    SnakeHS.Refresh()
    SnakeHS.Show()
End Sub

Private Sub btnSaveScore_Click(sender As Object, e As EventArgs) Handles btnSaveScore.Click

End Sub

Private Sub btnRetry_Click(sender As Object, e As EventArgs) Handles btnRetry.Click

    Call frmPlayfield.Reset()
    Close()
    frmPlayfield.Show()

End Sub

Private Sub You_Died_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    lblScore.Text = frmPlayfield.playerScore


End Sub
End Class

感谢能帮助我的人,无法联系IT老师寻求帮助。

0 个答案:

没有答案