代码如下:(需要加一个time控件) 有兴趣加我QQ,大家一起探讨一下!77177503。
Const p = 3.1415926 / 180 '椭圆运动
Dim Xo As Integer, Yo As Integer
Dim x As Integer, y As Integer
Dim x1 As Single, y1 As Single
Dim Rx As Single, Ry As Single
Dim a() As Single, b() As Single
Dim N As Integer
Private Sub Form_Load()
Dim i As Integer, j As Integer
Randomize
Me.Move 0, 0, Screen.Width, Screen.Height '当前窗体全屏显示
N = HScroll1.Value '显示HScroll代表的数量
Label1.Caption = N
ReDim a(N), b(N)
For i = 1 To N
a(i) = 180 / N * i
b(i) = 360 * Rnd * p
Load S(i)
S(i).Visible = True
S(i).FillColor = QBColor(Rnd * 6 + 9) '颜色码,见书P240
Next i
Xo = Me.ScaleWidth / 2 '屏幕宽度的一半
Yo = Me.ScaleHeight / 2 '屏幕高度的一半
Rx = Xo / 2
Ry = Yo / 5
Shape1.Move Xo - Shape1.Width / 2, Yo - Shape1.Height / 2
Cls
For j = 1 To N
For i = 0 To 360
x = Rx * Cos(i * p)
y = Ry * Sin(i * p)
x1 = x * Cos(a(j) * p) - y * Sin(a(j) * p)
y1 = x * Sin(a(j) * p) + y * Cos(a(j) * p)
PSet (Xo + x1, Yo + y1)
Next i
Next j
End Sub
Private Sub HScroll1_Change()
For i = 1 To N
Unload S(i)
Next i
Form_Load
End Sub
Private Sub Timer1_Timer()
Static i As Integer, j As Single
j = j + 2
Shape1.FillColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255) '设置shape1的填充颜色
For i = 1 To N
x = Rx * Cos(j * p + b(i))
y = Ry * Sin(j * p + b(i))
x1 = x * Cos(a(i) * p) - y * Sin(a(i) * p)
y1 = x * Sin(a(i) * p) + y * Cos(a(i) * p)
S(i).Move Xo + x1 - S(i).Width / 2, Yo + y1 - S(i).Height / 2
Next i
End Sub
VB编写的小球做椭圆运动,非常漂亮!