|
|
|
|
|
|
|
|
|
|
|
2.演示质点的平抛运动。
基本原理如下: 小球用一个图形控件Shape1来实现。用定时器控件Timer1每隔一个单位时间在不同的位置显示该运动的小球就行了。问题的关键就在于小球的显示位置即x,y坐标。遵照平抛运动的规律:x=vt,y=gt2/2。t是一个表示时间变化的变量,每次显示小球后增加一个常量值就行了。 程序实例: l 在Form1中的控件:(如图2) (1)Shape1: BackStyle=0 (透明) FillColor=&H00FF0000& (蓝色) FillStyle=0 (Solid) Height=250 Left=30 Shape=3 (Circle) Top=210 Width=250 (2)Timer1: Interval=100 (0.1秒) (3)Text1: Text="0" (4)Command1 Caption="开 始" (5)Label1: Caption="水平速度" l 程序代码: Dim t As Integer, g As Single, v As Integer Dim x As Long, y As Long Private Sub Command1_Click() t = 0 g = 9.8 v = Val(Text1.Text) Shape1.Left = 0 '小球初始位置 Shape1.Top = 0 Timer1.Enabled = True '定时器开始工作 End Sub Private Sub Form_Load() Timer1.Enabled = False '定时器不工作,使小球静止 t = 0 g = 9.8 Shape1.Left = 0 Shape1.Top = 0 End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then Command1_Click '在文本框中按回车相当于按“开始”按钮 End Sub Private Sub Timer1_Timer() t = t + 1 y = g * t * t / 2 Shape1.Top = y '定小球的垂直位置 x = v * t '定小球的水平位置 Shape1.Left = x If y >= Height Or x >= Width Then Timer1.Enabled = False '如果小球已出窗体则停止 Circle (x + Shape1.Width / 2, y + Shape1.Height / 2), Shape1.Width / 2 + 50 '画出小球的运动轨迹 End Sub |
|
| |