按照代码添加响应控件控件:
Option Explicit
Dim x_step As Integer
Dim y_step As Integer
Dim CurSuc As Integer
Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
'启动或停止定时器
If Command1.Caption = "暂停" Then
Command1.Caption = "继续"
Else
Command1.Caption = "暂停"
End If
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
Label2.Caption = 0
x_step = 250
y_step = 250
End Sub
Private Sub Picture1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37 '如果按下左箭头,使板子向左移动
If Line1.X1 <= 0 Then
Line1.X1 = 0
Else
Line1.X1 = Line1.X1 - 100
Line1.X2 = Line1.X2 - 100
End If
Case 39 '如果按下右箭头,使板子向右移动
If Line1.X2 >= Picture1.Width Then
Line1.X2 = Picture1.Width
Else
Line1.X1 = Line1.X1 + 100
Line1.X2 = Line1.X2 + 100
End If
End Select
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim LineLen As Integer
LineLen = Line1.X2 - Line1.X1
If X > 0 And X < Picture1.Width - LineLen And Y > Line1.Y2 Then
Line1.X1 = X
Line1.X2 = LineLen + Line1.X1
End If
End Sub
Private Sub Timer1_Timer()
If Shape1.Top <= 0 Then Shape1.Top = 0: y_step = -y_step
'如果小球出了上边界便弹回
If Shape1.Left <= 0 Then Shape1.Left = 0: x_step = -x_step
'如果小球出了左边界便弹回
If Shape1.Left >= Picture1.Width - Shape1.Width Then Shape1.Left = Picture1.Width - Shape1.Width: x_step = -x_step
'如果小球出了右边界便弹回
If Shape1.Top >= Line1.Y1 - Shape1.Height And Shape1.Left >= Line1.X1 And Shape1.Left <= Line1.X2 Then
'如果小球撞在板子上,便弹回
Shape1.Top = Line1.Y1 - Shape1.Height
y_step = -y_step
x_step = x_step
Label2.Caption = Val(Label2.Caption) + 1
CurSuc = CurSuc + 1
End If
If CurSuc >= 5 Then
CurSuc = 0
If Timer1.Interval > 25 Then Timer1.Interval = Timer1.Interval - 25
If Line1.X2 - Line1.X1 > 300 Then Line1.X2 = Line1.X2 - 300
End If
Shape1.Top = Shape1.Top + y_step
'使小球移动
Shape1.Left = Shape1.Left + x_step
If Shape1.Top >= Picture1.Height - Shape1.Height Then
MsgBox "你输了!"
Timer1.Enabled = False
Command1.Caption = "开始"
Shape1.Top = 1000
Label2.Caption = 0
End If
End Sub