如何做一个vb小球沿余弦曲线运动
(2)速度和曲线区间可以由用户自行设置
(3)运动过程可随时停止和启动
(4)能够将图片以图片形式保存 展开
你这里面,在复制代码的时候没法进行格式化,凑合看吧,按照下面的程序设计,把下面的代码赋值过去应该就能用了。
界面设计如下:
一个Picturebox,两个textbox,一个Timer,三个command按钮
运行效果:
代码如下:
Dim a() As String '存放区间的值
Dim b As Single '存放圆球的实时x坐标
Dim n As Boolean '区间改变时从起点开始移动
Private Sub Command1_Click()
Timer1.Interval = Val(Text1.Text) * 1000 '设置移动速度
Timer1.Enabled = True
a = Split(Text2.Text, ",")
n = True
If n Then
b = Val(a(0))
End If
Call DrawTheXY
Picture1.Circle (b, Cos(b)), (a(1) - a(0)) / 50, RGB(255, 0, 0)
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
Picture1.Circle (b, Cos(b)), (a(1) - a(0)) / 50, RGB(255, 0, 0)
End Sub
Private Sub Command3_Click() '保存图片
SavePicture Picture1.Image, "D:\1.jpg"
End Sub
Private Sub Form_Load()
Picture1.AutoRedraw = True
Picture1.FillStyle = 0
Picture1.FillColor = RGB(255, 0, 0)
End Sub
Function DrawTheXY() '自定义函数,根据给定的区间画出余弦曲线
Picture1.Cls
If Val(a(0)) < 0 And Val(a(1)) > 0 Then
Picture1.AutoRedraw = True
Picture1.Scale (Val(a(0)) * 1.1, 2)-(Val(a(1)) * 1.1, -2)
Picture1.Line (Val(a(0)) * 1.05, 0)-(Val(a(1)) * 1.05, 0)
Picture1.Line (0, 1.5)-(0, -1.5)
Picture1.Line (Val(a(1)) * 1.05, 0)-(Val(a(1)) * 1, 0.1)
Picture1.Line (Val(a(1)) * 1.05, 0)-(Val(a(1)) * 1, -0.1)
Picture1.Line (0, 1.5)-(-Val(a(1)) * 0.03, 1.3)
Picture1.Line (0, 1.5)-(Val(a(1)) * 0.04, 1.3)
Picture1.CurrentX = -Val(a(1)) * 0.1
Picture1.CurrentY = 1.5
Picture1.Print "Y"
Picture1.CurrentX = Val(a(1)) * 1.05
Picture1.CurrentY = -0.1
Picture1.Print "X"
Picture1.CurrentX = Val(a(1)) * 0.03
Picture1.CurrentY = -0.05
Picture1.Print "0"
For i = Val(a(0)) To Val(a(1)) Step 0.001
Picture1.PSet (i, Cos(i))
Next i
Else
If Val(a(1)) <= 0 Then
Picture1.AutoRedraw = True
Picture1.Scale (Val(a(0)) * 1.1, 2)-(Val(a(1)) * 0.9, -2)
Picture1.Line (Val(a(0)) * 1.05, 0)-(Val(a(1)) * 0.95, 0)
For i = Val(a(0)) To Val(a(1)) Step 0.001
Picture1.PSet (i, Cos(i))
Next i
End If
If Val(a(0)) >= 0 Then
Picture1.AutoRedraw = True
Picture1.Scale (Val(a(0)) * 0.9, 2)-(Val(a(1)) * 1.1, -2)
Picture1.Line (Val(a(0)) * 0.95, 0)-(Val(a(1)) * 1.05, 0)
For i = Val(a(0)) To Val(a(1)) Step 0.001
Picture1.PSet (i, Cos(i))
Next i
End If
End If
End Function
Private Sub Text2_Change()
n = True
End Sub
Private Sub Timer1_Timer() '控制小球的移动
b = b + (Val(a(1)) - Val(a(0))) * 0.001
If b >= Val(a(1)) Then
b = Val(a(0))
End If
n = False
Picture1.Cls
Call DrawTheXY
Picture1.Circle (b, Cos(b)), (a(1) - a(0)) / 50, RGB(255, 0, 0)
End Sub
2024-04-02 广告