如何在KeyDown PrivateSub

时间:2017-08-16 15:51:25

标签: vb.net visual-studio-2017

我正在使用visual basic编写一个基本的pong游戏,并需要if语句的帮助。当球击中边缘时,它会重置到屏幕中间,并且" resetBall"设置为true,这反过来应该让我按下" R"并让它再次移动。

Private Sub Timer1_Tick(sender As Object, e As KeyEventArgs) Handles Timer1.Tick

    If PictureBox3.Bounds.IntersectsWith(PictureBox2.Bounds) Then
        direction = 1
    End If

    If PictureBox3.Bounds.IntersectsWith(PictureBox1.Bounds) Then
        direction = 0
    End If

    If direction = 0 Then
        PictureBox3.Left += 15
    End If

    If direction = 1 Then
        PictureBox3.Left -= 15
    End If

    If PictureBox3.Bounds.IntersectsWith(leftBumper.Bounds) Then
        PictureBox3.SetBounds(325, 165, 0, 0, BoundsSpecified.X Or BoundsSpecified.Y)
        direction = 2
        resetBall0 = True
    End If

    If PictureBox3.Bounds.IntersectsWith(rightBumper.Bounds) Then
        PictureBox3.SetBounds(325, 165, 0, 0, BoundsSpecified.X Or BoundsSpecified.Y)
        direction = 2
        resetBall1 = True
    End If

End Sub

但是当我使用" If"在代码的下一部分中声明,它不起作用。

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

    Select Case e.KeyCode

        Case Keys.W
            PictureBox1.Top -= 8

        Case Keys.S
            PictureBox1.Top += 8

        Case Keys.Up
            PictureBox2.Top -= 8

        Case Keys.Down
            PictureBox2.Top += 8

            If resetBall1 = True Then
                Select Case e.KeyCode
                    Case Keys.R
                        direction = 1
                End Select
            End If

            If resetBall0 = True Then
                Select Case e.KeyCode
                    Case Keys.R
                        direction = 0
                End Select
            End If

    End Select
End Sub

1 个答案:

答案 0 :(得分:0)

你在Case Keys.Down内,所以Keycode永远不会是R ......它已经失败了。将您的逻辑移动到自己的case语句中:

    Select Case e.KeyCode    
        Case Keys.W
            PictureBox1.Top -= 8

        Case Keys.S
            PictureBox1.Top += 8

        Case Keys.Up
            PictureBox2.Top -= 8

        Case Keys.Down
            PictureBox2.Top += 8

       Case Keys.R
            If resetBall1 = True Then
                direction = 1
            End If

            If resetBall0 = True Then
                direction = 0
            End If
    End Select