我正在尝试在VB 2010中制作Pong游戏。我为paddles添加了两个RectangleShapes,并且为了让其中一个移动,我将使用箭头键。我认为KeyDown事件过程可以完成这项工作,但它没有按计划进行。有人可以帮帮我吗?代码摘录如下:
'Paddle Movement
Private Sub frmMyPong_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Dim redX As Integer = rectRed.Location.X
Dim redY As Integer = rectRed.Location.Y
Dim blueX As Integer = rectBlue.Location.X
Dim blueY As Integer = rectBlue.Location.Y
If Keys.Up Then 'Moves paddle up
redY -= 1
End If
If Keys.Down Then 'Moves paddle down
redY += 1
End If
If Keys.Right Then 'Moves paddle to the right
redX -= 1
End If
If Keys.Left Then 'Moves paddle to the left
redX += 1
End If
End Sub
答案 0 :(得分:0)
您将paddle的位置存储在变量(redX,redY)中,然后更新这些变量而不是实际的paddle。
它就像记住桨的位置,然后想象它们将会在哪里。
你应该直接增加桨的位置:
Private Sub frmMyPong_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If Keys.Up Then 'Moves paddle up
rectRed.Location = New Point(Label1.Location.X, Label1.Location.Y - 1)
End If
If Keys.Down Then 'Moves paddle down
rectRed.Location = New Point(Label1.Location.X, Label1.Location.Y + 1)
End If
If Keys.Right Then 'Moves paddle to the right
rectRed.Location = New Point(Label1.Location.X + 1, Label1.Location.Y)
End If
If Keys.Left Then 'Moves paddle to the left
rectRed.Location = New Point(Label1.Location.X - 1, Label1.Location.Y)
End If
End Sub
此外,一旦你开始工作,请看看:
Select Case
运算符 - 这将使您的条件更易于阅读答案 1 :(得分:0)
这样可以工作,,添加新的Sub并用箭头触发它
If Keys.Up Then 'Moves paddle up
redY -= 1
moveitem() ' this will fire new function
End If
If Keys.Down Then 'Moves paddle down
redY += 1
moveitem() ' this will fire new function
End If
If Keys.Right Then 'Moves paddle to the right
redX -= 1
moveitem() ' this will fire new function
End If
If Keys.Left Then 'Moves paddle to the left
redX += 1
moveitem() ' this will fire new function
End If
然后添加
Private sub moveitem()
rectRed.Location = new point(redX,redY)
End sub