Public Class frmMain
Dim i As Integer = -1 '沿X方向运动速度
Dim j As Integer = -1 '沿Y方向运动速度
Private Sub Timerbounce_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timerbounce.Tick
'小球开始运动
Button_Ball.Location = New Point(Button_Ball.Location.X + i, Button_Ball.Location.Y + j)
'判断小球是否到达边界,速度相应改变,达到反弹的目的
If Button_Ball.Location.Y = Button_Move.Location.Y - Button_Ball.Width And Button_Ball.Location.X <= (Button_Move.Location.X + Button_Move.Width) And Button_Ball.Location.X >= Button_Move.Location.X Then
j *= -1
End If
If Button_Ball.Location.Y = 0 Then
j *= -1
End If
If Button_Ball.Location.X = 0 Or Button_Ball.Location.X = Me.ClientSize.Width - Button_Ball.Width Then
i *= -1
End If
'判断小球是否被挡板接到,若未接到结束游戏
If Button_Ball.Location.Y = (Me.ClientSize.Height - Button_Ball.Width) Then
Me.Timerbounce.Stop()
MsgBox("YOU LOSE ! ^(oo)^", MsgBoxStyle.Exclamation, "game over")
i = 0
j = 0
End If
End Sub
'控制小球开始运动
Private Sub Button_Move_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Move.Click
If Me.Timerbounce.Enabled Then
Me.Timerbounce.Stop()
Button_Move.Text = "move"
Else
Me.Timerbounce.Start()
Button_Move.Text = "pause"
End If
End Sub
'控制按钮的左右移动
Private Sub Button_Move_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button_Move.KeyDown
If e.KeyCode = Keys.Right Then
ProcessDialogKey(Keys.Right)
End If
If e.KeyCode = Keys.Left Then
ProcessDialogKey(Keys.Left)
End If
End Sub
'判断键盘响应左键跟右键,使按钮移动
Protected Overloads Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
Select Case keyData
Case Keys.Left
If Button_Move.Location.X > 0 Then
Button_Move.Location = New Point(Button_Move.Location.X - 5, Button_Move.Location.Y)
End If
Exit Select
Case Keys.Right
If Button_Move.Location.X < (Me.ClientSize.Width - Button_Move.Width) Then
Button_Move.Location = New Point(Button_Move.Location.X + 5, Button_Move.Location.Y)
End If
Exit Select
End Select
Return True
End Function