Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 蛇游戏重置按钮不工作?_Vb.net_Visual Studio - Fatal编程技术网

Vb.net 蛇游戏重置按钮不工作?

Vb.net 蛇游戏重置按钮不工作?,vb.net,visual-studio,Vb.net,Visual Studio,在Visual Basic中创建蛇游戏作为学校作业。我几乎完成了,但在主屏幕窗体与游戏/操场窗体以及“你死了”窗体的交互方面遇到了一些问题。我能通过一轮蛇行。我的“你死了”屏幕弹出,但当我单击“重试”时,它会以我离开表单或崩溃的状态重新加载表单。同样的事情也会发生,如果我点击我的主页按钮重新打开主屏幕,然后我再次点击播放,它会带我回到我死去的地方。 frmPlayField Public Class frmPlayfield Public PFSize As Integer = SnakeH

在Visual Basic中创建蛇游戏作为学校作业。我几乎完成了,但在主屏幕窗体与游戏/操场窗体以及“你死了”窗体的交互方面遇到了一些问题。我能通过一轮蛇行。我的“你死了”屏幕弹出,但当我单击“重试”时,它会以我离开表单或崩溃的状态重新加载表单。同样的事情也会发生,如果我点击我的主页按钮重新打开主屏幕,然后我再次点击播放,它会带我回到我死去的地方。 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
你死了屏幕

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老师寻求帮助。

VBA
是另一种语言-建议您编辑标签,以便您的问题能够打动.Net用户。此外,我的蛇变长后,它开始闪烁一点。我认为这与我的
checkbounds和movement
sub中的代码有关,我认为你的snake会闪烁,因为你必须更新这么多图片框。WinForms的GDI+并不是最快的渲染机制。而且,不需要使用。此:
callgamemusic()
与此完全相同:
GameMusic()
。在
Reset()
内部调用
Refresh()
。正如您所知:这不会删除任何控件,它只是强制窗体重新绘制自身(请参阅)对于snake闪烁,请尝试从
checkboundsandmovation()
方法中删除
boxArray(snake(i).X,snake(i.Y).Update()
行。这只是对每个图片框不必要的重画。当您修改一个属性以直观方式对其进行更改时(例如
BackColor
),控件通常会自动重新绘制自身。